Remove the generated README.md, which was accidentally added automatically by my...
[io-compress-brotli.git] / bin / bro-perl
CommitLineData
2786a68d
QR
1#!perl
2#
3
4use warnings;
5use strict;
6use 5.014;
7
64dc5d65
QR
8use bytes;
9
2786a68d
QR
10use File::Slurp;
11use Getopt::Long;
64dc5d65 12use Time::HiRes qw/ gettimeofday tv_interval /;
2786a68d
QR
13
14use IO::Compress::Brotli;
15use IO::Uncompress::Brotli;
16
17GetOptions(
18 'c|custom-dictionary=s' => \(my $DICTIONARY),
19 'd|decompress' => \(my $DECOMPRESS),
64dc5d65 20 'f|force' => \(my $FORCE),
2786a68d
QR
21 'h|help' => \(my $HELP),
22 'i|input=s' => \(my $INPUT),
23 'o|output=s' => \(my $OUTPUT),
24 'q|quality=i' => \(my $QUALITY = 11),
64dc5d65 25 'r|repeat=i' => \(my $REPEAT = 1),
2786a68d
QR
26 's|stream=i' => \(my $STREAM),
27 'v|verbose' => \(my $VERBOSE),
28 'w|window=i' => \(my $WINDOW = 22),
29);
30
31if( $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
64dc5d65
QR
37if( $REPEAT > 1 && !($INPUT && $OUTPUT) ) {
38 say "You can only run a benchmark on files specifying --input and --output";
39 exit 1;
2786a68d 40}
2786a68d 41
64dc5d65
QR
42my $t0 = [gettimeofday];
43my $total_size = 0;
44my ($encoded, $decoded);
2786a68d 45
64dc5d65
QR
46for ( 1..$REPEAT ) {
47 my $ifh;
48 if( $INPUT ) {
49 open $ifh, "<", $INPUT
50 or die "Cannot open input file $INPUT.\n";
2786a68d 51 }
64dc5d65
QR
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";
2786a68d 61 }
64dc5d65
QR
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 );
2786a68d
QR
79 }
80 }
81 else {
64dc5d65
QR
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);
807f50a6 88 $total_size += bytes::length( $buf );
64dc5d65
QR
89 print $ofh $encoded;
90 }
91 $encoded = $bro->finish();
64dc5d65
QR
92 print $ofh $encoded;
93 }
94 else {
95 my $decoded = read_file( $ifh );
96 my $encoded = bro( $decoded, $QUALITY, $WINDOW );
807f50a6 97 $total_size += bytes::length( $decoded );
64dc5d65
QR
98 write_file( $ofh, $encoded );
99 }
2786a68d
QR
100 }
101}
102
64dc5d65
QR
103if( $VERBOSE ) {
104 my $elapsed = tv_interval ( $t0 );
105 say "Ran $REPEAT iterations in a total of $elapsed seconds";
106 say sprintf(
107 "Brotli %s speed: %.6f MB/s",
108 ( $DECOMPRESS ? "decompression" : "compression" ),
109 $total_size / 1024 / 1024 / $elapsed
110 );
111}
This page took 0.015596 seconds and 4 git commands to generate.