Debian Codegraph @

src/testpattern/compress-checksums.in

raw
1 #!@PERL@
2 ## Copyright (C) 2016-2017 Robert Krawitz
3 ##
4 ## This program is free software; you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2, or (at your option)
7 ## any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 # Compress Gutenprint regression test results
18 #
19 # The Gutenprint regression test uses run-testpattern-2 to generate
20 # hashes for each test case it runs. It currently runs about 370,000
21 # cases and generates 75 MB of output (if we used a less...verbose
22 # hash than SHA-512, we could save a fair bit of that, but it would
23 # still be something in the range of 40 MB). This is too big to store
24 # for very long, or to store in the repo.
25 #
26 # But there's a lot of compression we can do, some of it lossy and
27 # some lossless, to greatly reduce the data volume. Obviously, we can
28 # use conventional lossless compression (such as bzip2), but by
29 # understanding what we're trying to accomplish we can do much better.
30 #
31 # The three important points are:
32 #
33 # 1) There's a lot of duplication (a lot of test cases generate the
34 # same output). Some of this is because a lot of printers actually
35 # use the same driver, some of it is because in some cases
36 # different options don't affect the output, and some of it is
37 # because under many conditions different drivers generate the same
38 # output. Of the 369195 test cases as of 5.3.0-pre1, there are
39 # only 17701 distinct outputs, as measured by counting the number
40 # of distinct hash values.
41 #
42 # 2) We don't need the entire hash value, only enough to determine
43 # with reasonable reliability that a particular test case's output
44 # has not changed between runs. If we keep, say, 32 bits of hash
45 # value, we're going to miss a change in output only once in every
46 # 4 billion cases. For this purpose, that's good enough. So we
47 # keep only enough hash to distinguish between all of the hash
48 # values in the test run. That's usually between 7 and 9 bytes
49 # (28-36 bits). If we miss a change in output once in 256 million
50 # cases, that should be OK. And for that matter, if we sort the
51 # hashes, we only need to keep the delta between adjacent hash
52 # value; leading hex digits in common can be omitted.
53 #
54 # 3) We don't care about preserving the order of the test runs. It
55 # doesn't matter at all if the test runs show up in different
56 # order; all we need to do is match them up case by case to see
57 # what has changed. The cases are all independent.
58 #
59 # We also know about the structure of the test data. Each test result
60 # is stored as <hash, name>. The names themselves are structured as
61 # <printer_option0_value0_option1_value1...>. Since a lot of
62 # components are preserved from case to case, we can do a lot of
63 # common component elimination and prefix/suffix removal.
64 #
65 # It turns out that we can achieve better compression by putting the
66 # printer name on the end of the case rather than the beginning, since
67 # in many cases many printers share the same information. The
68 # original format puts the printer name first, but no reason we can't
69 # reorder it for storage, as long as we can undo it later when we
70 # compare the results.
71 #
72 # Surprisingly (perhaps) after all of these machinations the data
73 # still doesn't have lot of entropy; bzip2 compresses even our best
74 # work by 90%. And bzip is far from the best compression out there.
75 # The best that I have found is zpaq
76 # (http://mattmahoney.net/dc/zpaq.html). It's very slow but very,
77 # very effective. The zpaq utility is set up for archiving rather
78 # than file compression, but that's nothing that a little scripting
79 # can't get around. For our test data, zpaq gets another 35-45%,
80 # depending upon compression level (initial completely uncompressed
81 # data is another story; zpaq is about 70% better!).
82
83 # Compression levels (cumulative):
84 #
85 # 0 - none
86 # 1 - elide duplicate hashes (LOSSY -- reorders data)
87 # 2 - use global minimum hash length prefix (LOSSY -- reduces hash data)
88 # 3 - eliminate common second level groups (run encoding)
89 # 4 - use pairwise minimum hash length prefix
90 # 5 - use substitution table for common option names
91 # 6 - use pre-substitution table for certain value names (LOSSY -- reorder)
92 # 7 - common prefix/suffix elimination for chunks
93 # - pack multiple consecutive identical chunks
94 # - eliminate BW/color prefix
95 # 8 - replace space (inter-record gaps) with high bit set on last byte
96 # 9 - compact subexpression elimination (7a)
97
98 # There are 9 defined compression levels to match usage of other
99 # compression programs.
100
101 # 369195 test cases in the sample run
102 # CPU: Intel Xeon E3-1505M, Lenovo P70
103
104 # Method Size Packed Time Ratio Packed bzip2 Bits/case
105 # 0 75512435 1970522 0.08 1.0 38.3 6998640 42.69
106 # 1 11066295 1316628 2.82 6.8 57.3 1620995 28.52
107 # 2 8924474 228519 2.94 8.4 330.4 453281 4.95
108 # 3 5925516 216037 3.16 11.3 349.5 383805 4.68
109 # 4 5874446 205435 3.22 11.4 367.5 354349 4.45
110 # 5 5306533 195594 3.61 14.2 386.0 316775 4.24
111 # 6 4788102 182921 3.88 15.7 412.8 290248 3.96
112 # 7b 3266449 180295 23.1 417.3 221237 3.91
113 # 7a 2401276 126804 31.4 548.7 200778 2.75
114 # 7 2312771 126487 4.67 32.6 595.5 200371 2.74
115 # 8 2110942 122762 5.12 35.7 615.1 197529 2.66
116 # 9 1891341 121970 5.30 39.9 619.1 187040 2.64
117
118 # Currently reserved special characters:
119 #
120 # ( ( ) & ? ) reserved for level 3.
121 # ; is reserved for level 5.
122 # Braces {} are reserved for level 6.
123 # (* + \ /) are reserved for level 7.
124 # (= ! " , ' % >) are reserved for level 7 (7a)
125 # High bit set is reserved for level 8.
126 # Non-printable ASCII (0-31) is reserved for level 9.
127 # (- _) are used as parts of tokens and token separators.
128 # | is reserved for use as a temporary.
129
130 # Non-alphanumeric characters currently unreserved are:
131 # ` ~ @ # $ ^ [ ] <
132
133 use strict;
134 use Getopt::Long;
135 Getopt::Long::Configure("bundling", "no_ignore_case", "pass_through");
136
137 my ($comp_level) = 9;
138
139 my (%checksums);
140
141 my (@ptable) =
142 (["=\\+", pack("C", 27)],
143 ["!\\+", pack("C", 26)],
144 ['"\+', pack("C", 25)],
145 [",\\+", pack("C", 24)],
146 ["'\\+", pack("C", 23)],
147 ["%\\+", pack("C", 22)],
148 [">\\+", pack("C", 21)],
149 ["20\\+", pack("C", 20)],
150 ["19\\+", pack("C", 19)],
151 ["18\\+", pack("C", 18)],
152 ["17\\+", pack("C", 17)],
153 ["16\\+", pack("C", 16)],
154 ["15\\+", pack("C", 15)],
155 ["14\\+", pack("C", 14)],
156 ["13\\+", pack("C", 13)],
157 ["12\\+", pack("C", 12)],
158 ["11\\+", pack("C", 11)],
159 ["10\\+", pack("C", 9)], # 0xa is newline!
160 ["9\\+", pack("C", 8)],
161 ["8\\+", pack("C", 7)],
162 ["7\\+", pack("C", 6)],
163 ["6\\+", pack("C", 5)],
164 ["5\\+", pack("C", 4)],
165 ["4\\+", pack("C", 3)],
166 ["3\\+", pack("C", 2)],
167 ["2\\+", pack("C", 1)]);
168
169 my (%asc_table);
170 map { $asc_table{pack("C", $_)} = pack("C", $_ + 128); } (0..127);
171
172 my $psub_code = <<\EOF;
173 sub psub($) {
174 my ($out) = @_;
175 EOF
176 foreach my $psub (@ptable) {
177 $psub_code .= " \$out =~ s/$$psub[0]/$$psub[1]/go;\n";
178 }
179 $psub_code .= <<\EOF;
180 return $out;
181 }
182 EOF
183
184 eval $psub_code;
185
186 my (%sub_table) = (
187 "PrintingMode" => "P;",
188 "PageSize" => "S;",
189 "DitherAlgorithm" => "Z;",
190 "Resolution" => "R;",
191 "ColorCorrection" => "C;",
192 "Color" => "c;",
193 "MediaType" => "M;",
194 "InputSlot" => "I;"
195 );
196
197 my ($spc) = unpack("C", " ");
198
199 sub precompress($) {
200 my ($run) = @_;
201 # This one really doesn't belong here. This is to work around the
202 # (very domain-specifid) fact that most of the runs specify a dither
203 # algorithm, except for the ones testing the dither algorithms per se.
204 # The common group elimination code needs to have everything to the right
205 # of the second option to be fixed except for the printer name.
206 # If dither algorithm sorts between other options, it often
207 # breaks up a run of option names, since if dither algorithm is the
208 # key option it won't appear to the right, breaking up a run of fixed
209 # right hand components.
210 #
211 # This has a significant effect on compression; at level 10 the
212 # 5.3.0-pre1 test dataset consumes 1938695 bytes without this
213 # hack, but 1891341 with it -- about 2.5% savings. The zpaq archive
214 # is 123645 bytes without and 121970 with, or 1.3%.
215 $run =~ s/DitherAlgorithm/Z;/go;
216 $run =~s/600x600dpi_(draft|high|mono|photo|std|tshirt)([^?&_]*)/{600|$1|$2}/go;
217 $run =~s/720x720dpi_(draft|high|mono|photo|std|tshirt)([^?&_]*)/{720|$1|$2}/go;
218 $run =~s/300x300dpi_(draft|high|mono|photo|std|tshirt)([^?&_]*)/{300|$1|$2}/go;
219 $run =~s/360x360dpi_(draft|high|mono|photo|std|tshirt)([^?&_]*)/{360|$1|$2}/go;
220 $run =~s/\{([0-9]*)\|([^|])[^|]*\|([^?&_]*)\}/{$1$2$3}/go;
221 return $run;
222 }
223
224 sub find_min_nonunique_prefix(@) {
225 my (@sums) = sort @_;
226 my ($last) = shift @sums;
227 my ($maxlen) = length $last;
228 my ($max_prefix) = 1;
229 # To allow (more) safely combining multiple sets of test data
230 if (defined $ENV{"STP_MIN_CHECKSUM_PREFIX"}) {
231 $max_prefix = $ENV{"STP_MIN_CHECKSUM_PREFIX"};
232 if ($max_prefix <= 0) {
233 return $maxlen;
234 }
235 }
236 foreach my $sum (@sums) {
237 next if length $sum <= $max_prefix;
238 foreach my $l ($max_prefix..$maxlen) {
239 last if (substr($sum, 0, $l) ne substr($last, 0, $l));
240 $max_prefix++;
241 }
242 $last = $sum;
243 }
244 return $max_prefix;
245 }
246
247 sub min($$) {
248 my ($a, $b) = @_;
249 if ($a < $b) {
250 return $a;
251 } else {
252 return $b;
253 }
254 }
255
256 sub get_printer($) {
257 my ($mode) = @_;
258 $mode =~ s/_PrintingMode.*//;
259 return $mode;
260 }
261
262 sub get_mode($) {
263 my ($mode) = @_;
264 $mode =~ s/.*_PrintingMode/PrintingMode/;
265 return $mode;
266 }
267
268 sub scan_next_block($$$$$$$) {
269 my ($runs, $printers, $start, $blocksize, $ovalue, $rest, $ov1) = @_;
270 my ($limit) = scalar @$runs;
271 # Don't have enough runs left
272 if ($start + $blocksize > $limit) {
273 return "";
274 }
275 my ($run0) = $$runs[$start];
276 $run0 =~ /^([^:]+):(.*)$/o;
277 my ($data) = $1;
278 my ($printer) = $2;
279 # Printer does not match the first printer on the list.
280 if ($printer ne $$printers[0]) {
281 return "";
282 }
283 my (@elts) = split(/_/, $data);
284 my ($v1) = $elts[1];
285 if ($v1 ne $ov1) {
286 return "";
287 }
288 my ($option) = $elts[2];
289 my ($value) = $elts[3];
290 if ($ovalue eq $value) {
291 return "";
292 }
293 my ($nrest) = join("", @elts[4..$#elts]);
294 if ($nrest ne $rest) {
295 return "";
296 }
297 # Different option
298 my $i;
299 for ($i = 1; $i < $blocksize; $i++) {
300 my ($run) = $$runs[$start + $i];
301 $run =~ /^([^:]+):(.*)$/o;
302 $data = $1;
303 $printer = $2;
304 # Different printer that what we expect!
305 if ($printer ne $$printers[$i]) {
306 return "";
307 }
308 @elts = split(/_/, $data);
309 my ($nv1) = $elts[1];
310 my $noption = $elts[2];
311 if ($nv1 ne $v1) {
312 return "";
313 }
314 # Different option than what we expect.
315 if ($noption ne $option) {
316 return "";
317 }
318 my ($nvalue) = $elts[3];
319 # ...or different value from what we expect
320 if ($nvalue ne $value) {
321 return "";
322 }
323 # ...or something else different, perchance?
324 $nrest = join("", @elts[4..$#elts]);
325 if ($nrest ne $rest) {
326 return "";
327 }
328 }
329 return [$option, $value];
330 }
331
332 # If we find a match, return the data: glommed-together option string,
333 # size of the block, and number of values.
334 #
335 # If we don't find a match, return how far we searched without finding
336 # a match, so that the compressor doesn't spend a lot of time searching
337 # for a match where there isn't going to be one.
338 sub scan_for_block($$) {
339 my ($runs, $start) = @_;
340 my ($first_printer);
341 my ($idx) = $start;
342 my ($limit) = scalar @$runs;
343 # Runs are now of the form
344 # PrintingMode_XX_OPT1_YY...:printer
345 # First scan pass: look for the first change of YY while OPT1 remains
346 # the same. The printer should be the same as the first printer we
347 # find.
348 my ($run0) = $$runs[$start];
349 $run0 =~ /^([^:]+):(.*)$/o;
350 my ($data) = $1;
351 my ($printer) = $2;
352 my (@elts) = split(/_/, $data);
353 my ($v1) = $elts[1];
354 my ($option) = $elts[2];
355 my ($value) = $elts[3];
356 my ($rest) = join("", @elts[4..$#elts]);
357 my (@printers) = ($printer);
358 my (@values) = ([$option, $value]);
359 $idx++;
360 while ($idx < $limit) {
361 my ($run) = $$runs[$idx];
362 $run =~ /^([^:]+):(.*)$/o;
363 $data = $1;
364 $printer = $2;
365 @elts = split(/_/, $data);
366 my ($nv1) = $elts[1];
367 my ($noption) = $elts[2];
368 my ($nvalue) = $elts[3];
369 if ($v1 ne $nv1) {
370 return ("", 0, $idx);
371 }
372 # Different option name -- don't have a match.
373 if ($noption ne $option) {
374 return ("", 0, $idx);
375 }
376 my ($nrest) = join("", @elts[4..$#elts]);
377 if ($nrest ne $rest) {
378 return ("", 0, $idx);
379 }
380 if ($printer ne $printers[0]) {
381 if ($nvalue ne $value) {
382 # Different option value, but not the same printer -- don't have
383 # a match.
384 return ("", 0, $idx);
385 } else {
386 # Same option value, new printer
387 push @printers, $printer;
388 $idx++;
389 }
390 } else {
391 if ($nvalue eq $value) {
392 return ("", 0, $idx);
393 }
394 # New option value but same printer as first -- found a block!
395 last;
396 }
397 }
398 my ($blocksize) = $#printers + 1;
399 if ($blocksize < 2) {
400 return ( "", 0, $idx );
401 }
402 my $ovalue = "";
403 while ($idx < $limit) {
404 $value = scan_next_block($runs, \@printers, $idx, $blocksize, $ovalue,
405 $rest, $v1);
406 if (ref $value eq "ARRAY") {
407 push @values, $value;
408 $ovalue = $value->[1];
409 $idx += $blocksize;
410 } else {
411 # End of the line.
412 if ($#values > 0) {
413 last;
414 } else {
415 return ( "", 0, $idx );
416 }
417 }
418 }
419 if ($#values > 0) {
420 my (@optstrs);
421 my (@valstrs);
422 my ($last_option) = "";
423 foreach my $item (@values) {
424 my ($opt) = $item->[0];
425 my ($val) = $item->[1];
426 if ($last_option ne $opt) {
427 if ($last_option ne "") {
428 if ($comp_level >= 5 && defined $sub_table{$last_option}) {
429 $last_option = $sub_table{$last_option};
430 }
431 push @optstrs, "${last_option}_" . join("&", @valstrs);
432 }
433 $last_option = $opt;
434 @valstrs = ();
435 }
436 push @valstrs, $val;
437 }
438 if ($comp_level >= 5 && defined $sub_table{$last_option}) {
439 $last_option = $sub_table{$last_option};
440 }
441 push @optstrs, "${last_option}_" . join("&", @valstrs);;
442 my ($valstr) = join("?", @optstrs);
443 return ( $valstr, $blocksize, $#values + 1 );
444 } else {
445 return ( "", 0, $idx );
446 }
447 }
448
449 GetOptions("l:i" => \$comp_level);
450
451 if ($comp_level <= 0) {
452 print while (<>);
453 exit;
454 }
455
456 while (<>) {
457 chomp;
458 my ($sum, $key) = split;
459 if (! defined $checksums{$sum}) {
460 $checksums{$sum} = [];
461 }
462 push @{$checksums{$sum}}, $key;
463 }
464
465 my $min_prefix;
466
467 if ($comp_level >= 2) {
468 $min_prefix = find_min_nonunique_prefix(keys %checksums);
469 } else { # $comp_level == 1
470 $min_prefix = length((keys %checksums)[0]);
471 }
472
473 my ($last_sum) = "";
474
475 foreach my $sum (sort keys %checksums) {
476 my ($out) = substr($sum, 0, $min_prefix);
477 if ($last_sum ne "" && $comp_level >= 4) {
478 $out = substr($out, find_min_nonunique_prefix($sum, $last_sum) - 1);
479 }
480 $last_sum = $sum;
481 my ($prun) = "";
482 my (@pchunks) = ();
483 my ($plen) = 0;
484 my (@runs) = sort map {
485 s/^\*//;
486 s/\.prn$//;
487 my ($printer) = get_printer($_);
488 my ($mode) = get_mode($_);
489 if ($comp_level >= 6) {
490 $mode = precompress($mode);
491 }
492 "${mode}_:$printer"
493 } @{$checksums{$sum}};
494 my ($lastdup) = 0;
495 my ($valstr) = "";
496 my ($blocksize) = 0;
497 my ($valcount) = 0;
498 my ($stop_at) = -1;
499 my ($idx);
500 my ($found_block_first_time) = 0;
501 my ($next_check) = 0;
502 while ($idx <= $#runs) {
503 my $run = $runs[$idx];
504 my (@chunks) = split(/[_]/, $run);
505 if ($comp_level >= 3 && $stop_at < 0 &&
506 $idx >= $next_check &&
507 $#runs - $idx > 2 &&
508 $pchunks[2] ne $chunks[2]) {
509 ($valstr, $blocksize, $valcount) = scan_for_block(\@runs, $idx);
510 if ($blocksize > 0) {
511 $chunks[2] = "";
512 $chunks[3] = $valstr;
513 $stop_at = $idx + $blocksize;
514 $found_block_first_time = 1;
515 } else {
516 $next_check = $valcount;
517 }
518 }
519 if ($comp_level >= 5) {
520 map {
521 if (defined $sub_table{$chunks[$_]}) {
522 $chunks[$_] = $sub_table{$chunks[$_]};
523 }
524 } (0..$#chunks);
525 }
526 my ($mchunks) = min(scalar @pchunks, scalar @chunks);
527 my ($i) = 0;
528 $out .= " ";
529 if ($stop_at > 0) {
530 if ($found_block_first_time) {
531 map { $pchunks[$_] = "" } (2..$#pchunks);
532 $chunks[2] = "($chunks[2]";
533 $chunks[3] = $valstr;
534 } else {
535 $chunks[2] = "";
536 $chunks[3] = "";
537 }
538 }
539
540 while ($i < $mchunks) {
541 if ($i > 0) {
542 $out .= '_';
543 }
544 if ($chunks[$i] ne $pchunks[$i]) {
545 if ($comp_level >= 7) {
546 my ($plen) = length $pchunks[$i];
547 my ($len) = length $chunks[$i];
548 my ($maxc) = $len;
549 $maxc = $plen if $plen< $len;
550 my ($dup) = 0;
551 my ($chunk) = $chunks[$i];
552 if (substr($pchunks[$i], 0, 1) ne '(') {
553 while ($dup < $maxc) {
554 last if (substr($pchunks[$i], $dup, 1) ne
555 substr($chunks[$i], $dup, 1));
556 $dup++;
557 }
558 if (! $found_block_first_time) {
559 my ($tail) = 0;
560 while ($tail < $maxc - $dup) {
561 last if (substr($pchunks[$i], -1 - $tail, 1) ne
562 substr($chunks[$i], -1 - $tail, 1));
563 $tail++;
564 }
565 if ($tail == 2) {
566 $chunk = substr($chunk, 0, -$tail) . '\\';
567 } elsif ($tail > 2) {
568 $chunk = substr($chunk, 0, -$tail) . "/$tail";
569 }
570 }
571 }
572 if ($dup == 2) {
573 $out .= '*' . substr($chunk, $dup);
574 } elsif ($dup > 2) {
575 if ($dup == $lastdup) {
576 $out .= "+" . substr($chunk, $dup);
577 } else {
578 $out .= "$dup+" . substr($chunk, $dup);
579 }
580 } else {
581 $out .= "$chunk";
582 }
583 $lastdup = $dup;
584 } else {
585 $out .= $chunks[$i];
586 }
587 }
588 $i++;
589 }
590 $found_block_first_time = 0;
591 @pchunks = @chunks;
592 if ($i < scalar @chunks) {
593 if ($i > 0) {
594 $out .= '_';
595 }
596 $out .= join("_", @chunks[$i..$#chunks]);
597 }
598 $idx++;
599 if ($blocksize > 0 && $idx == $stop_at) {
600 $out .= ")";
601 $idx += ($blocksize * ($valcount - 1));
602 $stop_at = -1;
603 }
604 }
605 $out =~ s/\(_/(/g;
606 if ($comp_level >= 7) {
607 $out =~ s/________/=/go;
608 $out =~ s/_______/!/go;
609 $out =~ s/______/"/go;
610 $out =~ s/_____/,/go;
611 $out =~ s/____/'/go;
612 $out =~ s/___/%/go;
613 $out =~ s/__/>/go;
614 $out =~ s/^([0-9a-f]+ )P;_BW_/$1./;
615 $out =~ s/^([0-9a-f]+ )P;_c;_/$1,/;
616 if ($comp_level >= 8) {
617 $out =~ s/(.) /$asc_table{$1}/g;
618 if ($comp_level >= 9) {
619 $out = psub($out);
620 }
621 }
622 }
623 $out =~ s/\|$//;
624 print "$out\n";
625 }