]> iEval git - io-compress-brotli.git/blob - bin/bro-perl
6aac129481f9ac5dcc86ad11caaae5f314777acc
[io-compress-brotli.git] / bin / bro-perl
1 #!perl
2 #
3
4 use warnings;
5 use strict;
6 use 5.014;
7
8 use bytes;
9
10 use File::Slurp;
11 use Getopt::Long;
12 use Time::HiRes qw/ gettimeofday tv_interval /;
13
14 use IO::Compress::Brotli;
15 use IO::Uncompress::Brotli;
16
17 GetOptions(
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),
29 );
30
31 if( $HELP ) {
32 say "Usage: $0 [--force] [--quality n] [--decompress] [--input filename] [--output filename]".
33 " [--repeat iters] [--verbose] [--window n] [--custom-dictionary filename] [--stream size]";
34 exit 1;
35 }
36
37 if( $REPEAT > 1 && !($INPUT && $OUTPUT) ) {
38 say "You can only run a benchmark on files specifying --input and --output";
39 exit 1;
40 }
41
42 my $t0 = [gettimeofday];
43 my $total_size = 0;
44 my ($encoded, $decoded);
45
46 for ( 1..$REPEAT ) {
47 my $ifh;
48 if( $INPUT ) {
49 open $ifh, "<", $INPUT
50 or die "Cannot open input file $INPUT.\n";
51 }
52 $ifh //= \*STDIN;
53 binmode $ifh;
54
55 my $ofh;
56 if( $OUTPUT ) {
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";
61 }
62 $ofh //= \*STDOUT;
63 binmode $ofh;
64
65 if( $DECOMPRESS ) {
66 if( $STREAM ) {
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 );
71 print $ofh $decoded;
72 }
73 }
74 else {
75 $encoded = read_file( $ifh );
76 $decoded = unbro( $encoded );
77 $total_size += bytes::length( $decoded );
78 write_file( $ofh, $decoded );
79 }
80 }
81 else {
82 if( $STREAM ) {
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( $encoded );
89 print $ofh $encoded;
90 }
91 $encoded = $bro->finish();
92 $total_size += bytes::length( $encoded );
93 print $ofh $encoded;
94 }
95 else {
96 my $decoded = read_file( $ifh );
97 my $encoded = bro( $decoded, $QUALITY, $WINDOW );
98 $total_size += bytes::length( $encoded );
99 write_file( $ofh, $encoded );
100 }
101 }
102 }
103
104 if( $VERBOSE ) {
105 my $elapsed = tv_interval ( $t0 );
106 say "Ran $REPEAT iterations in a total of $elapsed seconds";
107 say sprintf(
108 "Brotli %s speed: %.6f MB/s",
109 ( $DECOMPRESS ? "decompression" : "compression" ),
110 $total_size / 1024 / 1024 / $elapsed
111 );
112 }
This page took 0.043772 seconds and 4 git commands to generate.