Add --repeat and --verbose options to bro-perl
authorQuim Rovira <quim@rovira.cat>
Sun, 14 Aug 2016 10:57:31 +0000 (12:57 +0200)
committerQuim Rovira <quim@rovira.cat>
Sun, 14 Aug 2016 10:57:31 +0000 (12:57 +0200)
Makefile.PL
bin/bro-perl

index 79ea79acb60a731e806975d882c07def1d06c747..bfbb161b524714e20a7d003f4b37f3ebf76a2456 100644 (file)
@@ -13,6 +13,7 @@ WriteMakefile(
        PREREQ_PM        => {
                'File::Slurp'  => '9999.19',
                'Getopt::Long' => '2.45',
+               'Time:HiRes'   => '1.9728'
        },
        BUILD_REQUIRES   => {},
        INC              => '-Ibrotli',
index 89a4e0011fe1ab6ab069df611ce9d2c26f4cb816..6aac129481f9ac5dcc86ad11caaae5f314777acc 100755 (executable)
@@ -5,8 +5,11 @@ use warnings;
 use strict;
 use 5.014;
 
+use bytes;
+
 use File::Slurp;
 use Getopt::Long;
+use Time::HiRes qw/ gettimeofday tv_interval /;
 
 use IO::Compress::Brotli;
 use IO::Uncompress::Brotli;
@@ -14,11 +17,12 @@ use IO::Uncompress::Brotli;
 GetOptions(
     'c|custom-dictionary=s' => \(my $DICTIONARY),
     'd|decompress'          => \(my $DECOMPRESS),
+    'f|force'               => \(my $FORCE),
     'h|help'                => \(my $HELP),
     'i|input=s'             => \(my $INPUT),
     'o|output=s'            => \(my $OUTPUT),
     'q|quality=i'           => \(my $QUALITY = 11),
-    'r|repeat=i'            => \(my $REPEAT),
+    'r|repeat=i'            => \(my $REPEAT = 1),
     's|stream=i'            => \(my $STREAM),
     'v|verbose'             => \(my $VERBOSE),
     'w|window=i'            => \(my $WINDOW = 22),
@@ -30,46 +34,79 @@ if( $HELP ) {
     exit 1;
 }
 
-my ($ifh, $ofh) = (\*STDIN, \*STDOUT);
-
-if( $INPUT ) {
-    open $ifh, "<", $INPUT
-        or die "Cannot open input file $INPUT.\n";
+if( $REPEAT > 1 && !($INPUT && $OUTPUT) ) {
+    say "You can only run a benchmark on files specifying --input and --output";
+    exit 1;
 }
-binmode $ifh;
 
-if( $OUTPUT ) {
-    open $ofh, ">", $OUTPUT
-        or die "Cannot open output file $OUTPUT.\n";
-}
-binmode $ofh;
+my $t0 = [gettimeofday];
+my $total_size = 0;
+my ($encoded, $decoded);
 
-if( $DECOMPRESS ) {
-    if( $STREAM ) {
-        my $brotli = IO::Uncompress::Brotli->create();
-        while( read $ifh, (my $buf), $STREAM ) {
-            print $ofh $brotli->decompress($buf);
-        }
+for ( 1..$REPEAT ) {
+    my $ifh;
+    if( $INPUT ) {
+        open $ifh, "<", $INPUT
+            or die "Cannot open input file $INPUT.\n";
     }
-    else {
-        my $encoded = read_file( $ifh );
-        my $decoded = unbro( $encoded );
-        write_file( $ofh, $decoded );
+    $ifh //= \*STDIN;
+    binmode $ifh;
+
+    my $ofh;
+    if( $OUTPUT ) {
+        die "Output file exists"
+            if( -e $OUTPUT && $REPEAT == 1 && !$FORCE );
+        open $ofh, ">", $OUTPUT
+            or die "Cannot open output file $OUTPUT.\n";
     }
-}
-else {
-    if( $STREAM ) {
-        my $brotli = IO::Compress::Brotli->create();
-        $brotli->quality( $QUALITY );
-        $brotli->window( $WINDOW );
-        while( read $ifh, (my $buf), $STREAM ) {
-            print $ofh $brotli->compress($buf);
+    $ofh //= \*STDOUT;
+    binmode $ofh;
+
+    if( $DECOMPRESS ) {
+        if( $STREAM ) {
+            my $bro = IO::Uncompress::Brotli->create();
+            while( read $ifh, (my $buf), $STREAM ) {
+                $decoded = $bro->decompress($buf);
+                $total_size += bytes::length( $decoded );
+                print $ofh $decoded;
+            }
+        }
+        else {
+            $encoded = read_file( $ifh );
+            $decoded = unbro( $encoded );
+            $total_size += bytes::length( $decoded );
+            write_file( $ofh, $decoded );
         }
     }
     else {
-        my $decoded = read_file( $ifh );
-        my $encoded = bro( $decoded, $QUALITY, $WINDOW );
-        write_file( $ofh, $encoded );
+        if( $STREAM ) {
+            my $bro = IO::Compress::Brotli->create();
+            $bro->quality( $QUALITY );
+            $bro->window( $WINDOW );
+            while( read $ifh, (my $buf), $STREAM ) {
+                $encoded = $bro->compress($buf);
+                $total_size += bytes::length( $encoded );
+                print $ofh $encoded;
+            }
+            $encoded = $bro->finish();
+            $total_size += bytes::length( $encoded );
+            print $ofh $encoded;
+        }
+        else {
+            my $decoded = read_file( $ifh );
+            my $encoded = bro( $decoded, $QUALITY, $WINDOW );
+            $total_size += bytes::length( $encoded );
+            write_file( $ofh, $encoded );
+        }
     }
 }
 
+if( $VERBOSE ) {
+    my $elapsed = tv_interval ( $t0 );
+    say "Ran $REPEAT iterations in a total of $elapsed seconds";
+    say sprintf(
+        "Brotli %s speed: %.6f MB/s",
+        ( $DECOMPRESS ? "decompression" : "compression" ),
+        $total_size / 1024 / 1024 / $elapsed
+    );
+}
This page took 0.01274 seconds and 4 git commands to generate.