|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use v5.16; |
| 6 | + |
| 7 | +############################################################################### |
| 8 | +# Used to benchmark optimizations to CLI scripts |
| 9 | +# |
| 10 | +# Usage: cli_bench [--num 50] 'cat /tmp/diff.patch | diff-so-fancy' |
| 11 | +############################################################################### |
| 12 | + |
| 13 | +use Time::HiRes qw(time); |
| 14 | +use Getopt::Long; |
| 15 | + |
| 16 | +############################################################################### |
| 17 | +############################################################################### |
| 18 | + |
| 19 | +my $num = 50; |
| 20 | +my $ignore = 1; |
| 21 | + |
| 22 | +my $ok = GetOptions( |
| 23 | + 'num=i' => \$num, |
| 24 | + 'ignore=i' => \$ignore, |
| 25 | +); |
| 26 | + |
| 27 | +my $cmd = join(" ", @ARGV); |
| 28 | + |
| 29 | +$| = 0; # Disable output buffering |
| 30 | + |
| 31 | +my @res; |
| 32 | +my $out; |
| 33 | +my $exit = 0; |
| 34 | +for (my $i = 0; $i < ($num + $ignore); $i++) { |
| 35 | + my $start = time(); |
| 36 | + $out = `$cmd`; |
| 37 | + $exit = $? >> 8; |
| 38 | + |
| 39 | + my $total = int((time() - $start) * 1000); |
| 40 | + push(@res, $total); |
| 41 | + |
| 42 | + print "."; |
| 43 | +} |
| 44 | + |
| 45 | +print "\n"; |
| 46 | + |
| 47 | +# Throw away the first X to give things time to warm up and be cached |
| 48 | +@res = splice(@res, $ignore); |
| 49 | + |
| 50 | +# Remove the top and bottom 10% |
| 51 | +my $outlier = $num / 10; |
| 52 | +@res = splice(@res, $outlier, $num - $outlier * 2); |
| 53 | + |
| 54 | +my $avg = int(average(@res)); |
| 55 | + |
| 56 | +print "Ran '$cmd' $num times with average completion time of $avg ms\n"; |
| 57 | + |
| 58 | +if ($exit != 0) { |
| 59 | + print $out; |
| 60 | +} |
| 61 | + |
| 62 | +############################################################################### |
| 63 | +############################################################################### |
| 64 | + |
| 65 | +sub average { |
| 66 | + my $ret = 0; |
| 67 | + |
| 68 | + foreach (@_) { |
| 69 | + $ret += $_; |
| 70 | + } |
| 71 | + |
| 72 | + my $count = scalar(@_); |
| 73 | + $ret = $ret / $count; |
| 74 | + |
| 75 | + return $ret; |
| 76 | +} |
| 77 | + |
| 78 | +sub random_int { |
| 79 | + my $ret = rand() * 90 + 10; |
| 80 | + $ret = int($ret); |
| 81 | + |
| 82 | + return $ret; |
| 83 | +} |
| 84 | + |
| 85 | +sub round { |
| 86 | + my $num = shift(); |
| 87 | + |
| 88 | + #https://stackoverflow.com/questions/178539/how-do-you-round-a-floating-point-number-in-perl |
| 89 | + #my $ret = int($num + $num/abs($num * 2 || 1)); |
| 90 | + |
| 91 | + my $ret; |
| 92 | + if ($num < 0) { |
| 93 | + $ret = int($num - 0.5); |
| 94 | + } else { |
| 95 | + $ret = int($num + 0.5); |
| 96 | + } |
| 97 | + |
| 98 | + return $ret; |
| 99 | +} |
| 100 | + |
| 101 | +sub trim { |
| 102 | + my $s = shift(); |
| 103 | + if (!defined($s) || length($s) == 0) { return ""; } |
| 104 | + $s =~ s/^\s*//; |
| 105 | + $s =~ s/\s*$//; |
| 106 | + |
| 107 | + return $s; |
| 108 | +} |
| 109 | + |
| 110 | +# String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue' |
| 111 | +sub color { |
| 112 | + my $str = shift(); |
| 113 | + |
| 114 | + # If we're NOT connected to a an interactive terminal don't do color |
| 115 | + if (-t STDOUT == 0) { return ''; } |
| 116 | + |
| 117 | + # No string sent in, so we just reset |
| 118 | + if (!length($str) || $str eq 'reset') { return "\e[0m"; } |
| 119 | + |
| 120 | + # Some predefined colors |
| 121 | + my %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0); |
| 122 | + $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg; |
| 123 | + |
| 124 | + # Get foreground/background and any commands |
| 125 | + my ($fc,$cmd) = $str =~ /^(\d{1,3})?_?(\w+)?$/g; |
| 126 | + my ($bc) = $str =~ /on_(\d{1,3})$/g; |
| 127 | + |
| 128 | + # Some predefined commands |
| 129 | + my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7); |
| 130 | + my $cmd_num = $cmd_map{$cmd // 0}; |
| 131 | + |
| 132 | + my $ret = ''; |
| 133 | + if ($cmd_num) { $ret .= "\e[${cmd_num}m"; } |
| 134 | + if (defined($fc)) { $ret .= "\e[38;5;${fc}m"; } |
| 135 | + if (defined($bc)) { $ret .= "\e[48;5;${bc}m"; } |
| 136 | + |
| 137 | + return $ret; |
| 138 | +} |
| 139 | + |
| 140 | +sub file_get_contents { |
| 141 | + my $file = shift(); |
| 142 | + open (my $fh, "<", $file) or return undef; |
| 143 | + |
| 144 | + my $ret; |
| 145 | + while (<$fh>) { $ret .= $_; } |
| 146 | + |
| 147 | + return $ret; |
| 148 | +} |
| 149 | + |
| 150 | +sub file_put_contents { |
| 151 | + my ($file, $data) = @_; |
| 152 | + open (my $fh, ">", $file) or return undef; |
| 153 | + |
| 154 | + print $fh $data; |
| 155 | + return length($data); |
| 156 | +} |
| 157 | + |
| 158 | +# Debug print variable using either Data::Dump::Color (preferred) or Data::Dumper |
| 159 | +# Creates methods k() and kd() to print, and print & die respectively |
| 160 | +BEGIN { |
| 161 | + if (eval { require Data::Dump::Color }) { |
| 162 | + *k = sub { Data::Dump::Color::dd(@_) }; |
| 163 | + } else { |
| 164 | + require Data::Dumper; |
| 165 | + *k = sub { print Data::Dumper::Dumper(\@_) }; |
| 166 | + } |
| 167 | + |
| 168 | + sub kd { |
| 169 | + k(@_); |
| 170 | + |
| 171 | + printf("Died at %2\$s line #%3\$s\n",caller()); |
| 172 | + exit(15); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +# vim: tabstop=4 shiftwidth=4 autoindent softtabstop=4 |
| 177 | + |
0 commit comments