-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtc-bench.pl
executable file
·272 lines (224 loc) · 7.26 KB
/
mtc-bench.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env perl
# PODNAME: mtc-bench
#
# ABSTRACT: Benchmark commands using `hyperfine` and `psrecord`. Look into CPU, memory and time.
BEGIN { $ENV{PERL_TEXT_CSV}='Text::CSV_PP'; }
use strict;
use warnings;
use Pod::Usage;
use feature 'say';
use Text::CSV qw/csv/;
use Data::Dump qw/dd pp/;
use Getopt::Long qw(GetOptionsFromArray);
use File::Path qw(make_path);
use POSIX qw(strftime);
use File::Temp qw/ tempdir /;
#
# Get a timestamp in the format "[YYYY-MM-DD HH:MM:SS]"
sub ts {
my @time = localtime();
return sprintf("[%04d-%02d-%02d %02d:%02d:%02d]",
$time[5] + 1900, $time[4] + 1, $time[3],
$time[2], $time[1], $time[0]);
}
################################
# Parse command line arguments
################################
# Define default values for variables
my $bench_cmds = [];
my $args = [];
my $help = 0;
my $show_output = 0;
my $verbose = 0;
my $cmd_file = "";
my $global_prep = "";
my $psrec_cmds = [];
my $dry_run = 0;
my $quiet = 0;
Getopt::Long::Configure("pass_through");
GetOptions(
'help|h' => \$help,
'show-output|s' => \$show_output,
'verbose|v' => \$verbose,
'file|f=s' => \$cmd_file,
'dry-run|d' => \$dry_run,
'quiet|q' => \$quiet,
) or pod2usage(2);
Getopt::Long::Configure("no_pass_through");
my $prep_count = 0;
for my $arg (@ARGV) {
$prep_count++ if $arg eq "--prepare";
}
my $i=1;
if ($prep_count == 1){
GetOptions( 'prepare|p=s' => \$global_prep) or pod2usage(2);
$bench_cmds = [];
for my $arg (@ARGV) {
push @$bench_cmds, { cmd => $arg, label => $i };
$i++;
}
} else {
while (my $arg = shift @ARGV) {
if ($arg eq "--prepare"){
push @$bench_cmds, {
prepare => shift @ARGV,
cmd => shift @ARGV,
label => $i
};
} else {
push @$bench_cmds, {
cmd => $arg,
label => $i
}
}
$i++;
}
}
###########################################################
# Validate and process arguments
###########################################################
if ($help) {
say STDERR "Usage: $0 [options]";
say STDERR "Options:";
say STDERR " -h, --help Print this help message";
say STDERR " -v, --verbose Print verbose output";
say STDERR " -s, --show-output Show output of commands";
say STDERR " -f, --file Read commands from file";
say STDERR " -d, --dry-run Just print the command that would be executed";
say STDERR " -p, --prepare Prepare commands to run before benchmarking. See hyperfine --help";
exit 0;
}
if ($quiet) {
$verbose = 0;
}
# Check for conflicting options
if ($cmd_file and scalar @$bench_cmds > 0) {
say STDERR ts(), " Error: Cannot specify both --file ($cmd_file) and commands (@$bench_cmds)";
exit 1;
}
# Check for missing options
if (not $cmd_file and scalar @$bench_cmds == 0) {
say STDERR ts(), " Error: Must specify either --file or commands";
exit 1;
}
# Check for file existence
if ($cmd_file and not -e $cmd_file) {
say STDERR ts(), " Error: File not found: $cmd_file";
exit 1;
}
###########################################################
# Read commands
###########################################################
if ($cmd_file) {
say STDERR ts(), " Reading commands from file: $cmd_file" if $verbose;
my $csv = csv(in => $cmd_file, headers => 'auto', quote_char => '\\', allow_whitespace => 1);
my $i=1;
for my $row (@$csv) {
push @$bench_cmds, {
prepare => $row->{prepare},
cmd => $row->{command},
label => $row->{label} || $i,
};
$i++;
}
} else {
say STDERR ts(), " Commands read from command line" if $verbose;
}
###########################################################
# Print benchmark information
###########################################################
my $RESULTS_BASE_DIR = 'mtc-results';
my $TS = strftime("%Y%m%d-%H%M%S", localtime);
my $RES_DIR = $ENV{RES_DIR} || "$RESULTS_BASE_DIR/$TS";
my $WARMUP = $ENV{WARMUP} || 1;
my $RUNS = $ENV{RUNS} || 3;
my $TMP_RES_DIR = tempdir( CLEANUP => 1 );
if (not -d $RES_DIR) {
make_path($RES_DIR);
}
say STDERR ts(), " Running benchmark with $RUNS repetitions and $WARMUP warm up rounds on these commands:" if ! $quiet;
for (my $i = 0; $i < scalar @$bench_cmds; $i++) {
my $cmd = $bench_cmds->[$i];
my $l = $cmd->{label} || $i+1;
say STDERR " [$l]: $cmd->{cmd}" if ! $quiet;
}
say STDERR ts(), " Saving results to '$RES_DIR'.\n";
###########################################################
# Cleanup function
###########################################################
my $cleanup_fn_name = "mtc_cleanup_files";
my $cleanup_fn = <<"CLEANUP";
function $cleanup_fn_name() {
if [[ "$verbose" == true ]]; then
>&2 echo "Cleaning up..."
fi
file=\$(find "$TMP_RES_DIR" -maxdepth 1 -name "*.log" | head -n 1)
if [[ -z "\$file" ]]; then
if [[ "$verbose" == true ]]; then
>&2 echo " no log file found for cleanup"
fi
return
fi
label="\${file##*/}"
label="\${label%.*}"
if [[ -z "\$label" ]]; then
>&2 echo "Error: Could not determine label for cleanup"
fi
counter=1
for ext in log png; do
while [[ -f "$RES_DIR/\$label-\$counter.\$ext" ]]; do
counter=\$((counter + 1))
done
if [[ "$verbose" == true ]]; then
>&2 echo " moving $TMP_RES_DIR/\$label.\$ext to $RES_DIR/\$label-\$counter.\$ext"
fi
mv "$TMP_RES_DIR/\$label.\$ext" "$RES_DIR/\$label-\$counter.\$ext"
done
}
CLEANUP
###########################################################
# hyperfine options
###########################################################
my $hf_flags = "";
$hf_flags .= "--show-output" if ($show_output);
$hf_flags .= " --prepare '$global_prep'" if ($global_prep);
$hf_flags .= " --shell=bash --runs $RUNS --warmup $WARMUP";
$hf_flags .= " --export-json $RES_DIR/hyperfine.json";
$hf_flags .= " --export-csv $RES_DIR/hyperfine.csv";
$hf_flags .= " --export-markdown $RES_DIR/hyperfine.md";
$hf_flags .= " --export-asciidoc $RES_DIR/hyperfine.txt";
if ($verbose) {
say STDERR ts(), " hyperfine flags:\n\t". $hf_flags;
}
###########################################################
# Run hyperfine
###########################################################
my $interval = 0.1;
my $hf_cmds = [];
for my $cmd (@$bench_cmds) {
my $psr_cmd = "psrecord --interval $interval ";
$psr_cmd .= "--plot $TMP_RES_DIR/$cmd->{label}.png ";
$psr_cmd .= "--log $TMP_RES_DIR/$cmd->{label}.log ";
$psr_cmd .= "--include-children ";
$psr_cmd .= "\"$cmd->{cmd}\"";
$cmd->{psrec_cmd} = $psr_cmd;
if ($cmd->{prepare}) {
push @$hf_cmds, " --prepare '$cleanup_fn_name && $cmd->{prepare}' '$psr_cmd'";
} else {
push @$hf_cmds, " '$cmd->{cmd}'";
}
}
my $hf_cmd = "$cleanup_fn\n";
$hf_cmd .= "export -f $cleanup_fn_name\n";
$hf_cmd .= "hyperfine $hf_flags ".join(" ", @$hf_cmds);
$hf_cmd .= "\n$cleanup_fn_name\n";
$hf_cmd .= ">&2 echo -e \"\nDone. Results saved to '$RES_DIR'.\"";
if ($dry_run){
say ts(), ' ', $hf_cmd;
exit 0;
} elsif ($verbose) {
say STDERR ts(), " COMMAND:\n\t$hf_cmd";
}
exec $hf_cmd;
__END__
=head1 NAME