12 use Time::HiRes qw/ gettimeofday tv_interval /;
14 use IO::Compress::Brotli;
15 use IO::Uncompress::Brotli;
18 'c|custom-dictionary=s' => \(my $DICTIONARY),
19 'd|decompress' => \(my $DECOMPRESS),
20 'f|force' => \(my $FORCE),
21 'h|help' => \(my $HELP),
22 'i|input=s' => \(my $INPUT),
23 'o|output=s' => \(my $OUTPUT),
24 'q|quality=i' => \(my $QUALITY = 11),
25 'r|repeat=i' => \(my $REPEAT = 1),
26 's|stream=i' => \(my $STREAM),
27 'v|verbose' => \(my $VERBOSE),
28 'w|window=i' => \(my $WINDOW = 22),
32 say "Usage: $0 [--force] [--quality n] [--decompress] [--input filename] [--output filename]".
33 " [--repeat iters] [--verbose] [--window n] [--custom-dictionary filename] [--stream size]";
37 if( $REPEAT > 1 && !($INPUT && $OUTPUT) ) {
38 say "You can only run a benchmark on files specifying --input and --output";
42 my $t0 = [gettimeofday];
44 my ($encoded, $decoded);
49 open $ifh, "<", $INPUT
50 or die "Cannot open input file $INPUT.\n";
57 die "Output file exists"
58 if( -e $OUTPUT && $REPEAT == 1 && !$FORCE );
59 open $ofh, ">", $OUTPUT
60 or die "Cannot open output file $OUTPUT.\n";
67 my $bro = IO::Uncompress::Brotli->create();
68 while( read $ifh, (my $buf), $STREAM ) {
69 $decoded = $bro->decompress($buf);
70 $total_size += bytes::length( $decoded );
75 $encoded = read_file( $ifh );
76 $decoded = unbro( $encoded );
77 $total_size += bytes::length( $decoded );
78 write_file( $ofh, $decoded );
83 my $bro = IO::Compress::Brotli->create();
84 $bro->quality( $QUALITY );
85 $bro->window( $WINDOW );
86 while( read $ifh, (my $buf), $STREAM ) {
87 $encoded = $bro->compress($buf);
88 $total_size += bytes::length( $buf );
91 $encoded = $bro->finish();
95 my $decoded = read_file( $ifh );
96 my $encoded = bro( $decoded, $QUALITY, $WINDOW );
97 $total_size += bytes::length( $decoded );
98 write_file( $ofh, $encoded );
104 my $elapsed = tv_interval ( $t0 );
105 say "Ran $REPEAT iterations in a total of $elapsed seconds";
107 "Brotli %s speed: %.6f MB/s",
108 ( $DECOMPRESS ? "decompression" : "compression" ),
109 $total_size / 1024 / 1024 / $elapsed