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