]> iEval git - io-compress-brotli.git/blame - bin/bro-perl
Remove Getopt::Long from README, it's been along since perl 5
[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(
17 'c|custom-dictionary=s' => \(my $DICTIONARY),
18 'd|decompress' => \(my $DECOMPRESS),
64dc5d65 19 'f|force' => \(my $FORCE),
2786a68d
QR
20 'h|help' => \(my $HELP),
21 'i|input=s' => \(my $INPUT),
22 'o|output=s' => \(my $OUTPUT),
23 'q|quality=i' => \(my $QUALITY = 11),
64dc5d65 24 'r|repeat=i' => \(my $REPEAT = 1),
2786a68d
QR
25 's|stream=i' => \(my $STREAM),
26 'v|verbose' => \(my $VERBOSE),
27 'w|window=i' => \(my $WINDOW = 22),
28);
29
30if( $HELP ) {
31 say "Usage: $0 [--force] [--quality n] [--decompress] [--input filename] [--output filename]".
32 " [--repeat iters] [--verbose] [--window n] [--custom-dictionary filename] [--stream size]";
33 exit 1;
34}
35
64dc5d65
QR
36if( $REPEAT > 1 && !($INPUT && $OUTPUT) ) {
37 say "You can only run a benchmark on files specifying --input and --output";
38 exit 1;
2786a68d 39}
2786a68d 40
64dc5d65
QR
41my $t0 = [gettimeofday];
42my $total_size = 0;
43my ($encoded, $decoded);
2786a68d 44
64dc5d65
QR
45for ( 1..$REPEAT ) {
46 my $ifh;
47 if( $INPUT ) {
48 open $ifh, "<", $INPUT
49 or die "Cannot open input file $INPUT.\n";
2786a68d 50 }
64dc5d65
QR
51 $ifh //= \*STDIN;
52 binmode $ifh;
53
54 my $ofh;
55 if( $OUTPUT ) {
08b471bd 56 die "Output file exists\n"
64dc5d65
QR
57 if( -e $OUTPUT && $REPEAT == 1 && !$FORCE );
58 open $ofh, ">", $OUTPUT
59 or die "Cannot open output file $OUTPUT.\n";
2786a68d 60 }
64dc5d65
QR
61 $ofh //= \*STDOUT;
62 binmode $ofh;
63
64 if( $DECOMPRESS ) {
65 if( $STREAM ) {
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 );
70 print $ofh $decoded;
71 }
72 }
73 else {
74 $encoded = read_file( $ifh );
75 $decoded = unbro( $encoded );
76 $total_size += bytes::length( $decoded );
77 write_file( $ofh, $decoded );
2786a68d
QR
78 }
79 }
80 else {
64dc5d65
QR
81 if( $STREAM ) {
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);
807f50a6 87 $total_size += bytes::length( $buf );
64dc5d65
QR
88 print $ofh $encoded;
89 }
90 $encoded = $bro->finish();
64dc5d65
QR
91 print $ofh $encoded;
92 }
93 else {
94 my $decoded = read_file( $ifh );
95 my $encoded = bro( $decoded, $QUALITY, $WINDOW );
807f50a6 96 $total_size += bytes::length( $decoded );
64dc5d65
QR
97 write_file( $ofh, $encoded );
98 }
2786a68d
QR
99 }
100}
101
64dc5d65
QR
102if( $VERBOSE ) {
103 my $elapsed = tv_interval ( $t0 );
104 say "Ran $REPEAT iterations in a total of $elapsed seconds";
105 say sprintf(
106 "Brotli %s speed: %.6f MB/s",
107 ( $DECOMPRESS ? "decompression" : "compression" ),
108 $total_size / 1024 / 1024 / $elapsed
109 );
110}
This page took 0.037809 seconds and 4 git commands to generate.