| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use v5.16; | ||
| 6 | use Time::HiRes qw(time); | ||
| 7 | use FindBin qw($RealBin); | ||
| 8 | use lib "$RealBin/lib"; | ||
| 9 | use Perl::Minify qw(minify); | ||
| 10 | |||
| 11 | # Benchmark suite for Perl::Minify | ||
| 12 | |||
| 13 | my @test_files = ( | ||
| 14 | { name => 'small', file => 'color.pl', iterations => 100 }, | ||
| 15 | { name => 'medium', file => 'lib/Perl/Minify.pm', iterations => 50 }, | ||
| 16 | ); | ||
| 17 | |||
| 18 | print "=" x 70, "\n"; | ||
| 19 | print "Perl::Minify Benchmark Suite\n"; | ||
| 20 | print "=" x 70, "\n\n"; | ||
| 21 | |||
| 22 | for my $test (@test_files) { | ||
| 23 | my $file = "$RealBin/$test->{file}"; | ||
| 24 | next unless -f $file; | ||
| 25 | |||
| 26 | open my $fh, '<', $file or die "Cannot open $file: $!"; | ||
| 27 | local $/; | ||
| 28 | my $code = <$fh>; | ||
| 29 | close $fh; | ||
| 30 | |||
| 31 | my $size = length($code); | ||
| 32 | my $lines = scalar(split /\n/, $code); | ||
| 33 | |||
| 34 | print "Test: $test->{name} ($test->{file})\n"; | ||
| 35 | print " Size: $size bytes, $lines lines\n"; | ||
| 36 | |||
| 37 | # Benchmark default minification | ||
| 38 | { | ||
| 39 | my $start = time; | ||
| 40 | for (1..$test->{iterations}) { | ||
| 41 | minify($code); | ||
| 42 | } | ||
| 43 | my $elapsed = time - $start; | ||
| 44 | my $per_iter = ($elapsed / $test->{iterations}) * 1000; | ||
| 45 | my $throughput = ($size * $test->{iterations}) / $elapsed / 1024; | ||
| 46 | |||
| 47 | printf " Default minify: %.2f ms/iter, %.1f KB/s\n", $per_iter, $throughput; | ||
| 48 | } | ||
| 49 | |||
| 50 | # Benchmark with shorten_vars | ||
| 51 | { | ||
| 52 | my $start = time; | ||
| 53 | for (1..$test->{iterations}) { | ||
| 54 | minify($code, { shorten_vars => 1 }); | ||
| 55 | } | ||
| 56 | my $elapsed = time - $start; | ||
| 57 | my $per_iter = ($elapsed / $test->{iterations}) * 1000; | ||
| 58 | my $throughput = ($size * $test->{iterations}) / $elapsed / 1024; | ||
| 59 | |||
| 60 | printf " With shorten_vars: %.2f ms/iter, %.1f KB/s\n", $per_iter, $throughput; | ||
| 61 | } | ||
| 62 | |||
| 63 | # Show compression ratio | ||
| 64 | { | ||
| 65 | my $minified = minify($code); | ||
| 66 | my $ratio = 100 * (1 - length($minified) / $size); | ||
| 67 | printf " Compression: %.1f%% (%.0f -> %.0f bytes)\n", | ||
| 68 | $ratio, $size, length($minified); | ||
| 69 | } | ||
| 70 | |||
| 71 | print "\n"; | ||
| 72 | } | ||
| 73 | |||
| 74 | print "=" x 70, "\n"; | ||
| 75 | print "Benchmark complete\n"; |