blib/lib/Algorithm/benchmark.pl untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 150 CC 4 SCAR 20.2
CC
4
Cov
0%
run_child
20.0
BEGIN
2.0
commify
2.0
CRAP 7.5
1 #!/usr/bin/env perl
2 #
3 # Compare the throughput of the Algorithm::EventsPerSecond backends.
4 #
5 # The backend is picked once when the module loads, so each code path
6 # runs in its own child perl: one default (XS if available) and one
7 # with ALGORITHM_EVENTSPERSECOND_PP=1 (pure Perl). Run this after
8 # building for XS numbers:
9 #
10 # perl Makefile.PL && make && ./benchmark.pl
11 # ./benchmark.pl --time 5 --windows 60,3600
12 #
13 # Which SIMD flavor the XS column uses depends on how the .so was
14 # compiled; rebuild with IF_ARCH=native to benchmark AVX2/SSE4.2.
15  
16 use 5.006;
17 use strict;
18 use warnings;
19 use FindBin ();
20 use Getopt::Long qw(GetOptions);
21  
22 my @incs;
23  
24 BEGIN {
25 @incs = grep { -d } ( "$FindBin::Bin/blib/arch", "$FindBin::Bin/blib/lib", "$FindBin::Bin/lib" );
26 unshift @INC, @incs;
27 }
28  
29 my $time = 2;
30 my $windows = '60,600,3600';
31 GetOptions(
32 'time=f' => \$time,
33 'windows=s' => \$windows,
34 'help' => sub { print "usage: $0 [--time seconds] [--windows csv]\n"; exit 0 },
35 ) or die "usage: $0 [--time seconds] [--windows csv]\n";
36  
37 my @windows = grep { /^\d+$/ && $_ > 0 } split /,/, $windows;
38 die "no valid window sizes in --windows\n" unless @windows;
39  
40 #
41 # child mode: benchmark whatever backend loads and emit tab-separated
42 # "label \t ops-per-second" lines for the parent to collate
43 #
44 if ( $ENV{AEPS_BENCH_CHILD} ) {
45 require Algorithm::EventsPerSecond;
46 require Benchmark;
47  
48 my $backend = Algorithm::EventsPerSecond->backend;
49 my $simd = Algorithm::EventsPerSecond->simd;
50 print "BACKEND\t", ( defined $simd ? "$backend ($simd)" : $backend ), "\n";
51  
52 my $bench = sub {
53 my ( $label, $per_iter, $code ) = @_;
54 my $t = Benchmark::countit( $time, $code );
55 my $cpu = $t->cpu_a || 1e-9;
56 printf "%s\t%.1f\n", $label, $t->iters * $per_iter / $cpu;
57 };
58  
59 {
60 my $m = Algorithm::EventsPerSecond->new( window => $windows[0] );
61 $bench->( 'mark, 1 event/call', 1, sub { $m->mark } );
62 $bench->( 'mark, 100 events/call', 100, sub { $m->mark(100) } );
63 }
64  
65 for my $w (@windows) {
66 my $m = Algorithm::EventsPerSecond->new( window => $w );
67 $m->mark;
68 $bench->( "count, window=$w", 1, sub { my $x = $m->count } );
69 $bench->( "rate, window=$w", 1, sub { my $x = $m->rate } );
70  
71 # a realistic consumer: record events, check the rate every 100
72 my $i = 0;
73 $bench->( "mark + rate/100, window=$w", 1, sub { $m->mark; $m->rate if ++$i % 100 == 0; } );
74 } ## end for my $w (@windows)
75  
76 exit 0;
77 } ## end if ( $ENV{AEPS_BENCH_CHILD} )
78  
79 #
80 # parent mode: run one child per backend and print a comparison
81 #
82 sub run_child {
83 my %env = ( @_, AEPS_BENCH_CHILD => 1 );
84 local @ENV{ keys %env } = values %env;
85  
86 my @cmd = ( $^X, ( map { "-I$_" } @incs ), $0, '--time', $time, '--windows', join( ',', @windows ), );
87 open my $fh, '-|', @cmd or die "cannot run child benchmark: $!\n";
88  
89 my ( $backend, %rate, @order );
90 while ( my $line = <$fh> ) {
91 chomp $line;
92 my ( $label, $value ) = split /\t/, $line, 2;
93 next unless defined $value;
94 if ( $label eq 'BACKEND' ) { $backend = $value; next; }
95 $rate{$label} = $value;
96 push @order, $label;
97 }
98 close $fh or die "child benchmark exited non-zero\n";
99 die "child produced no results\n" unless defined $backend;
100  
101 return { backend => $backend, rate => \%rate, order => \@order };
102 } ## end sub run_child
103  
104 sub commify {
105 my $n = sprintf '%.0f', shift;
106 1 while $n =~ s/^(\d+)(\d{3})/$1,$2/;
107 return $n;
108 }
109  
110 print "Algorithm::EventsPerSecond benchmark (perl $], ${time}s per measurement)\n";
111  
112 my $pp = run_child( ALGORITHM_EVENTSPERSECOND_PP => 1 );
113 my $xs = run_child();
114  
115 if ( $xs->{backend} eq $pp->{backend} ) {
116 print "XS backend not available (build it with: perl Makefile.PL && make);\n";
117 print "showing pure Perl only. All figures are calls/second.\n\n";
118 printf " %-32s %15s\n", '', $pp->{backend};
119 printf " %-32s %15s\n", $_, commify( $pp->{rate}{$_} ) for @{ $pp->{order} };
120 exit 0;
121 }
122  
123 print "All figures are events or calls per second, higher is better.\n\n";
124 printf " %-32s %15s %15s %9s\n", '', $pp->{backend}, $xs->{backend}, 'speedup';
125 for my $label ( @{ $pp->{order} } ) {
126 my $p = $pp->{rate}{$label};
127 my $x = $xs->{rate}{$label};
128 next unless defined $x;
129 printf " %-32s %15s %15s %8.1fx\n", $label, commify($p), commify($x), $p ? $x / $p : 0;
130 }