forked from gdevenyi/zlib-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.pl
executable file
·410 lines (331 loc) · 11.6 KB
/
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/perl -w
=pod
=head1 NAME
bench.pl - Benchmark zlib implementations
=head1 SYNOPSIS
bench.pl [options]
Options:
--compress-iters=... Number of times each file is compressed in one
benchmark run
--compress-levels=... Comma-separated list of compression levels to use
--decompress-iters=... Number of times each file is compressed in one
benchmark run
--help Print a help message
--output-file=... File (- for stdout) where results are printed to
--output-format=... Format to output results in (pretty, json)
--read-json=... Don't run benchmarks, but read results from this file
--recompile If passed, recompile all zlib versions before test
--runs=... Number of runs for each benchmark
--quiet Don't print progress reports to STDERR
=cut
use strict;
use BSD::Resource qw(times);
use Fatal qw(open);
use File::Slurp;
use File::Temp qw(tempfile);
use Getopt::Long;
use JSON;
use List::Util qw(sum);
use Pod::Usage;
use Statistics::Descriptive;
# Versions to test
my @versions = (
{ id => 'baseline', repository => 'https://github.com/madler/zlib.git', commit_or_branch => '50893291621658f355bc5b4d450a8d06a563053d' },
{ id => 'cloudflare', repository => 'https://github.com/rordenlab/zlib.git', commit_or_branch => 'b3064e0a1059ea5b9a6c78a889bdf55637ccd1d2'},
{ id => 'ng', repository => 'https://github.com/zlib-ng/zlib-ng.git', commit_or_branch => '550f98395c8677ae9b08ec39433f5137e5cea2c8'},
);
# Compression levels to benchmark
my @compress_levels = qw(1 6 9);
# Number of iterations of each benchmark to run (in addition to a single
# warmup run).
my $runs = 5;
# Number of compressions / decompressions to do in each run
my $compress_iters = 10;
my $decompress_iters = 20;
# If true, recompile all the zlib versions before running benchmark
my $recompile = 0;
# pprint or json
my $output_format = "pretty";
# If true, don't print progress info to STDERR
my $quiet = 0;
sub trace {
local $| = 1;
print STDERR @_;
}
sub checkout {
my ($id, $repository, $commit_or_branch) = @_;
my $dir = "zlib.$id";
if (-d $dir) {
if (system "cd $dir && git fetch && git reset --hard $commit_or_branch") {
die "'git checkout' of '$commit_or_branch' in $dir failed\n";
}
} else {
if (system "git clone $repository $dir") {
die "'git clone' of $id failed\n";
}
checkout(@_);
}
$dir;
}
sub compile {
my ($dir, $config) = @_;
system "cd $dir && ./configure $config->{CONFIGURE_FLAGS} && make";
}
sub init {
for my $version (@versions) {
$version->{dir} = "zlib.$version->{id}";
}
}
sub fetch_and_compile_all {
for my $version (@versions) {
if ($recompile or
!-f "$version->{dir}/minigzip") {
trace "Checking out $version->{id}\n";
checkout $version->{id}, $version->{repository}, $version->{commit_or_branch};
trace "Compiling $version->{id}\n";
compile $version->{dir}, $version;
}
}
}
sub benchmark_command {
my ($command, $iters) = @_;
my (@start_times) = times;
my $size;
for (1..$iters) {
$size = length qx"$command";
}
my (@end_times) = times;
{ output_size => $size,
time => sum(@end_times[2,3]) - sum(@start_times[2,3])}
}
sub benchmark_compress {
my ($zlib_dir, $input, $level, $iters) = @_;
benchmark_command "$zlib_dir/minigzip -$level < $input", $iters;
}
sub benchmark_decompress {
my ($zlib_dir, $input, $iters) = @_;
my $res = benchmark_command "$zlib_dir/minigzip -d < $input > /dev/null", $iters;
delete $res->{size};
return $res;
}
sub benchmark_all {
my %results = ();
# Compression benchmarks
for my $level (@compress_levels) {
for my $input (glob "corpus/[a-z]*") {
$input =~ m{.*/(.*)} or next;
my $id = "compress $1 -$level ($compress_iters iterations)";
trace "Testing '$id' ";
$results{$id}{input}{size} = (-s $input);
$results{$id}{level} = $level;
for (1..$runs) {
for my $version (@versions) {
trace ".";
# Warm up
benchmark_compress $version->{dir}, $input, $level, 1, 1;
my $result = benchmark_compress $version->{dir}, $input, $level, $compress_iters;
push @{$results{$id}{output}{"$version->{id}"}}, $result;
}
}
trace "\n";
}
}
# Decompression benchmarks.
# First create compressed files.
my %compressed = ();
for my $input (glob "corpus/[a-z]*") {
my ($fh) = File::Temp->new();
$compressed{$input}{fh} = $fh;
$compressed{$input}{tmpfile} = $fh->filename;
print $fh qx"$versions[0]{dir}/minigzip < $input";
close $fh;
}
for my $input (glob "corpus/[a-z]*") {
$input =~ m{.*/(.*)} or next;
my $id = "decompress $1 ($decompress_iters iterations)";
trace "Testing '$id' ";
$results{$id}{level} = '0';
for (1..$runs) {
for my $version (@versions) {
trace ".";
# Warm up
benchmark_decompress $version->{dir}, $compressed{$input}{tmpfile}, 1;
my $result = benchmark_decompress $version->{dir}, $compressed{$input}{tmpfile}, $decompress_iters;
push @{$results{$id}{output}{"$version->{id}"}}, $result;
}
}
trace "\n";
}
for my $input_results (values %results) {
for my $version_results (values %{$input_results->{output}}) {
my $processed = {};
for my $field (qw(output_size time)) {
my $stat = Statistics::Descriptive::Full->new();
for my $result (@{$version_results}) {
$stat->add_data($result->{$field});
}
$processed->{$field} = {
mean => $stat->mean(),
error => $stat->standard_deviation() / sqrt($stat->count()),
};
}
$version_results = $processed;
}
}
return {
versions => [
map {
{ id => $_->{id},
commit => qx(cd $_->{dir} && git rev-parse HEAD)
}
} @versions ],
results => \%results
}
}
sub pprint_text {
my ($output_file, $input) = @_;
my @versions = @{$input->{versions}};
my %results = %{$input->{results}};
local *STDOUT = $output_file;
printf "%20s ", '';
for my $version (@versions) {
printf "%-22s ", $version->{id};
}
my $prev_level = undef;
for my $key (sort {
($results{$a}{level} // 0) <=> ($results{$b}{level} // 0) or $a cmp $b
} keys %results) {
my %benchmark = %{$results{$key}};
my $level = $benchmark{level} // 0;
if (!defined $prev_level or
$level != $prev_level) {
print "\n";
}
$prev_level = $level;
printf "\n%s", $key;
if ($benchmark{input}{size}) {
printf "\n%20s ", "Compression ratio:";
for my $version (@versions) {
my $id = $version->{id};
my $output_size = $benchmark{output}{$id}{output_size}{mean};
my $input_size = $benchmark{input}{size};
printf("%5.2f %17s",
$output_size / $input_size,
'');
}
}
printf "\n%20s ", "Execution time [s]:";
for my $version (@versions) {
my $id = $version->{id};
my $time = $benchmark{output}{$id}{time}{mean};
my $error = $benchmark{output}{$id}{time}{error};
my $basetime = $benchmark{output}{'baseline'}{time}{mean};
printf("%5.2f (%6.2f%%) ", $time, $time / $basetime * 100);
}
}
printf "\n";
}
sub pprint_html {
my ($output_file, $input) = @_;
my @versions = @{$input->{versions}};
my %results = %{$input->{results}};
local *STDOUT = $output_file;
sub print_table_header {
print "\n<table>";
print " <tr>";
print " <td>";
for my $version (@versions) {
print "<td colspan=2>$version->{id}";
}
}
my $prev_level = 999;
for my $key (sort {
($results{$a}{level} // 0) <=> ($results{$b}{level} // 0) or $a cmp $b
} keys %results) {
my %benchmark = %{$results{$key}};
if (($benchmark{level} // 0) != $prev_level) {
if ($benchmark{level}) {
print "</table>\n<h4>Compression level $benchmark{level}</h4>";
} else {
print "</table>\n<h4>Decompression</h4>";
}
print_table_header;
$prev_level = ($benchmark{level} // 0);
}
my $key2 = $key;
$key2 =~ s/x (\d+)/$1 iterations/;
print " <tr><td colspan=4><b>$key2</b>";
if ($benchmark{input}{size}) {
print " <tr>";
print " <td style='padding-left: 2ex;'>Compression ratio</td>";
for my $version (@versions) {
my $id = $version->{id};
my $output_size = $benchmark{output}{$id}{output_size}{mean};
my $input_size = $benchmark{input}{size};
printf("<td colspan=2>%.2f",
$output_size / $input_size);
}
}
print " <tr>";
print " <td style='padding-left: 2ex; width: 20ex;'>Execution time</td>";
for my $version (@versions) {
my $id = $version->{id};
my $time = $benchmark{output}{$id}{time}{mean};
my $error = $benchmark{output}{$id}{time}{error};
my $basetime = $benchmark{output}{'baseline'}{time}{mean};
printf("<td>%.2fs±%.2f<td style='padding-right: 2ex'>(%d%%)",
$time,
$error,
($time / $basetime) * 100);
}
}
printf "</table>\n";
}
sub main {
my $help = 0;
my $output_file;
my $read_json = '';
Getopt::Long::Configure('auto_help');
if (!GetOptions(
"compress-levels=s" => sub {
@compress_levels = split /,/, $_[1];
for (@compress_levels) {
die "Invalid compression level $_\n" if /\D/;
}
},
"output-file=s" => sub {
if ($_[1] ne '-') {
open $output_file, ">", $_[1];
}
},
"output-format=s" => \$output_format,
"read-json=s" => \$read_json,
"recompile" => \$recompile,
"compress-iters=i" => \$compress_iters,
"decompress-iters=i" => \$decompress_iters,
"runs=i" => \$runs,
"quiet" => \$quiet,
)) {
exit(1);
}
die "--output-format should be 'pretty' or 'json'" if $output_format !~ /^(pretty|json|html)$/;
$output_file //= *STDOUT;
binmode($output_file, ":utf8");
my $results;
if ($read_json) {
my $data = read_file $read_json;
$results = decode_json $data;
} else {
init;
fetch_and_compile_all;
$results = benchmark_all;
}
if ($output_format eq 'pretty') {
pprint_text $output_file, $results;
} elsif ($output_format eq 'html') {
pprint_html $output_file, $results;
} else {
print $output_file encode_json $results;
}
}
main;