Fix bin/bro-perl
[io-compress-brotli.git] / bin / bro-perl
CommitLineData
2786a68d 1#!perl
2786a68d
QR
2
3use warnings;
4use strict;
5use 5.014;
6
08b471bd 7use bytes ();
64dc5d65 8
2786a68d
QR
9use File::Slurp;
10use Getopt::Long;
64dc5d65 11use Time::HiRes qw/ gettimeofday tv_interval /;
2786a68d
QR
12
13use IO::Compress::Brotli;
14use IO::Uncompress::Brotli;
15
16GetOptions(
2786a68d 17 'd|decompress' => \(my $DECOMPRESS),
64dc5d65 18 'f|force' => \(my $FORCE),
2786a68d
QR
19 'h|help' => \(my $HELP),
20 'i|input=s' => \(my $INPUT),
21 'o|output=s' => \(my $OUTPUT),
22 'q|quality=i' => \(my $QUALITY = 11),
64dc5d65 23 'r|repeat=i' => \(my $REPEAT = 1),
2786a68d
QR
24 's|stream=i' => \(my $STREAM),
25 'v|verbose' => \(my $VERBOSE),
26 'w|window=i' => \(my $WINDOW = 22),
27);
28
29if( $HELP ) {
30 say "Usage: $0 [--force] [--quality n] [--decompress] [--input filename] [--output filename]".
765b9391 31 " [--repeat iters] [--verbose] [--window n] [--stream size]";
2786a68d
QR
32 exit 1;
33}
34
64dc5d65
QR
35if( $REPEAT > 1 && !($INPUT && $OUTPUT) ) {
36 say "You can only run a benchmark on files specifying --input and --output";
37 exit 1;
2786a68d 38}
2786a68d 39
64dc5d65
QR
40my $t0 = [gettimeofday];
41my $total_size = 0;
42my ($encoded, $decoded);
2786a68d 43
64dc5d65
QR
44for ( 1..$REPEAT ) {
45 my $ifh;
46 if( $INPUT ) {
47 open $ifh, "<", $INPUT
48 or die "Cannot open input file $INPUT.\n";
2786a68d 49 }
64dc5d65
QR
50 $ifh //= \*STDIN;
51 binmode $ifh;
52
53 my $ofh;
54 if( $OUTPUT ) {
08b471bd 55 die "Output file exists\n"
64dc5d65
QR
56 if( -e $OUTPUT && $REPEAT == 1 && !$FORCE );
57 open $ofh, ">", $OUTPUT
58 or die "Cannot open output file $OUTPUT.\n";
2786a68d 59 }
64dc5d65
QR
60 $ofh //= \*STDOUT;
61 binmode $ofh;
62
63 if( $DECOMPRESS ) {
765b9391
MG
64 $STREAM //= 4 * 1024 * 1024;
65 my $bro = IO::Uncompress::Brotli->create();
66 while( read $ifh, (my $buf), $STREAM ) {
67 $decoded = $bro->decompress($buf);
64dc5d65 68 $total_size += bytes::length( $decoded );
765b9391 69 print $ofh $decoded;
2786a68d
QR
70 }
71 }
72 else {
64dc5d65
QR
73 if( $STREAM ) {
74 my $bro = IO::Compress::Brotli->create();
75 $bro->quality( $QUALITY );
76 $bro->window( $WINDOW );
77 while( read $ifh, (my $buf), $STREAM ) {
78 $encoded = $bro->compress($buf);
807f50a6 79 $total_size += bytes::length( $buf );
64dc5d65
QR
80 print $ofh $encoded;
81 }
82 $encoded = $bro->finish();
64dc5d65
QR
83 print $ofh $encoded;
84 }
85 else {
86 my $decoded = read_file( $ifh );
87 my $encoded = bro( $decoded, $QUALITY, $WINDOW );
807f50a6 88 $total_size += bytes::length( $decoded );
64dc5d65
QR
89 write_file( $ofh, $encoded );
90 }
2786a68d
QR
91 }
92}
93
64dc5d65
QR
94if( $VERBOSE ) {
95 my $elapsed = tv_interval ( $t0 );
96 say "Ran $REPEAT iterations in a total of $elapsed seconds";
97 say sprintf(
98 "Brotli %s speed: %.6f MB/s",
99 ( $DECOMPRESS ? "decompression" : "compression" ),
100 $total_size / 1024 / 1024 / $elapsed
101 );
102}
This page took 0.017288 seconds and 4 git commands to generate.