forked from qpdf/qpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_check
executable file
·273 lines (258 loc) · 6.31 KB
/
performance_check
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
#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
use File::Basename;
use Time::HiRes qw(gettimeofday tv_interval);
use File::Path qw(make_path);
my $whoami = basename($0);
$| = 1;
# [ name, [ args ] ]
my @tests = (
['no arguments', []],
['generate object streams', ['--object-streams=generate']],
['disable object streams', ['--object-streams=disable']],
['split pages', ['--split-pages', '--remove-unreferenced-resources=no']],
['shared resource check', ['--split-pages', '--remove-unreferenced-resources=auto']],
['linearize', ['--linearize']],
['encrypt', ['--encrypt', 'u', 'o', '256', '--']],
['json-output', ['--json-output']],
['json-input', ['--json-input']],
);
# If arg is not found in help output, look here. If not here, skip test.
# { new => old } -- if new is not found, replace with old; if old is
# if old is empty, remove argument
my %arg_compat = (
'--remove-unreferenced-resources=no' => '--preserve-unreferenced-resources',
'--remove-unreferenced-resources=yes' => '',
'--remove-unreferenced-resources=auto' => undef,
);
my $executable = undef;
my $test_dir = undef;
my $test_file = undef;
my $workdir = undef;
my $maxtime = undef;
my $iterations = undef;
my $default_executable = 'build/qpdf/qpdf';
my $default_test_dir = '../performance-test-files';
my $default_test_file = undef;
my $default_workdir = 'build/perf';
my $default_maxtime = 20;
my $default_iterations = 20;
sub usage
{
die "
Usage: $whoami [ args ]
--dir dir test on all files in dir (default: $default_test_dir)
--file file test only on the named file
--executable qpdf use the specified qpdf (default: $default_executable)
--workdir where to write output pdfs (default: $default_workdir)
--maxtime maximum time for a test; 0 means unlimited (default: $default_maxtime)
--iterations number of iterations (default: $default_iterations)
";
}
while (@ARGV)
{
my $arg = shift(@ARGV);
if ('--dir' eq $arg)
{
usage() unless @ARGV;
$test_dir = shift(@ARGV);
$test_file = undef;
}
elsif ('--file' eq $arg)
{
usage() unless @ARGV;
$test_file = shift(@ARGV);
$test_dir = undef;
}
elsif ('--executable' eq $arg)
{
usage() unless @ARGV;
$executable = shift(@ARGV);
}
elsif ('--workdir' eq $arg)
{
usage() unless @ARGV;
$workdir = shift(@ARGV);
}
elsif ('--maxtime' eq $arg)
{
usage() unless @ARGV;
$maxtime = shift(@ARGV);
}
elsif ('--iterations' eq $arg)
{
usage() unless @ARGV;
$iterations = shift(@ARGV);
}
else
{
usage();
}
}
if ((! defined $test_dir) && (! defined $test_file))
{
$test_dir = $default_test_dir;
}
if (! defined $executable)
{
$executable = $default_executable;
}
if (! defined $workdir)
{
$workdir = $default_workdir;
}
if (! defined $maxtime)
{
$maxtime = $default_maxtime;
}
if (! defined $iterations)
{
$iterations = $default_iterations;
}
my @test_files = ();
my @json_test_files = ();
{ # private scope
my @tmp = ();
if (defined $test_file)
{
push(@tmp, $test_file);
}
else
{
opendir(D, $test_dir) or
die "$whoami: can't open directory $test_dir: $!\n";
my @entries = readdir(D);
closedir(D);
for (sort @entries)
{
push(@tmp, "$test_dir/$_") unless (('.' eq $_) || ('..' eq $_));
}
}
foreach my $i (@tmp)
{
if ($i =~ m/.json$/)
{
push(@json_test_files, $i);
}
else
{
push(@test_files, $i);
}
}
}
run_tests();
print "\n";
sub filter_args
{
my $args = shift;
my $help = `$executable --help=all`;
my $new_args = [];
foreach my $arg (@$args)
{
my $to_check = $arg;
$to_check =~ s/=.*$//;
if (index($help, $to_check) == -1)
{
my $new_arg = $arg_compat{$arg};
if (! defined $new_arg)
{
return undef;
}
if ($new_arg ne '')
{
print " replacing $arg with $new_arg\n";
push(@$new_args, $new_arg);
}
}
else
{
push(@$new_args, $arg);
}
}
$new_args;
}
sub run_tests
{
my $args = shift;
chomp(my $commit = `git describe @`);
print "commit: $commit\n";
make_path($workdir);
foreach my $test (@tests)
{
my ($name, $args) = @$test;
print " test: $name\n";
$args = filter_args($args);
if (! defined $args)
{
print " skipping (unknown arguments)\n";
next;
}
my $test_files = \@test_files;
foreach my $arg (@$args)
{
if ($arg eq '--json-input')
{
$test_files = \@json_test_files;
last;
}
}
foreach my $file (@$test_files)
{
my $time = run_test($file, $args);
if (defined $time)
{
print " $time " . basename($file) ."\n";
}
else
{
print " $file skipped\n";
}
}
}
}
sub run_test
{
my ($file, $args) = @_;
my $outfile = "out.pdf";
foreach my $arg (@$args)
{
if ($arg eq '--json-output')
{
$outfile = "out.json";
last;
}
}
my @cmd = ($executable, @$args, $file, "$workdir/$outfile");
# Run once and discard to update caches
system("sync");
system(@cmd);
my $i = 0;
my $total = 0;
while ($i < $iterations)
{
my $start = [gettimeofday];
my $r = system(@cmd);
if ($r == 2)
{
# interrupt
exit(2);
}
my $end = [gettimeofday];
if ($r != 0)
{
print " command failed; ignoring results\n";
return undef;
}
my $elapsed = tv_interval($start, $end);
$total += $elapsed;
++$i;
if (($maxtime > 0) && ($total >= $maxtime) && ($i >= 3))
{
# This is taking too long, so take what we have
last;
}
}
return sprintf("%0.4f", $total / $i);
}