Benchmark/Dumb.pm

filter: Statementstmt 71.6% 124 / 173 Branchbran 50.0% 21 / 42 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 90.0% 18 / 20 Podpod 100% 8 / 8 totaltotal 64.4% 178 / 276 CC 37 SCAR 38.1
CC
37
Cov
64%
cmpthese
182.0
_timethese_guts
7.1
timestr
6.3
CRAP 45.3
« Dumbbench/Instance/Cmd.pm ↑ Summary Dumbbench.pm »
1 package Benchmark::Dumb;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Dumbbench;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our @CARP_NOT = qw(
8 Dumbbench
9 Dumbbench::Instance
10 Dumbbench::Instance::Cmd
11 Dumbbench::Instance::PerlEval
12 Dumbbench::Instance::PerlSub
13 Dumbbench::Result
14 );
15  
16 2 our $VERSION = '0.505';
17  
18 2 require Exporter;
19  
20 2 our @ISA = qw(Exporter);
21 2 our @EXPORT = ();
22 2 our @EXPORT_OK = qw(
23 timeit timethis timethese cmpthese
24 timediff timestr timesum
25 );
26 2 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
27  
28 # strip out :hireswallclock
29 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
30 2 my $class = shift;
31 2 my @args = grep $_ ne ':hireswallclock', @_;
32 2 $class->export_to_level(1, $class, @args);
33 }
34  
35 3 ▶sub _dumbbench_from_count {
Subroutine _dumbbench_from_count: called CC=4 SCAR=15.2
36 3 my $count = shift;
37 3 my %opt = @_;
38 3 ▶ if ($count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 0) 0 3 3
39 0 Carp::croak("The negative-value variant of COUNT in benchmarks is not supported by Benchmark::Dumb");
40 }
41 3 ▶ if ($count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= 1) 3 0 3
42 3 $opt{initial_runs} = int($count);
43 }
44 3 ▶ if (int($count) != $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $count != $count) 0 3 3
45 0 $opt{target_rel_precision} = $count - int($count);
46 }
47  
48 3 return Dumbbench->new(
49 # TODO configurable default settings?
50 %opt,
51 );
52 }
53  
54 4 ▶sub _prepare {
Subroutine _prepare: called CC=4 SCAR=13.9
55 4 my $count = shift;
56 4 my $code = shift;
57 4 my $name = shift;
58 4 ▶ my $bench = shift || _dumbbench_from_count($count); # FIXME %opt?
Condition66%2/3
shift() || _dumbbench_from_count($count)
l!l&&r!l&&!r
220
MC/DC0%0/2
shift() || _dumbbench_from_count($count)
shift() _dumbbench_from_count($count)
Truth table66%2/3
A || B
A= shift()B= _dumbbench_from_count($count)
ABresult
1X1
011
000
59 4 ▶ $name = 'anon' if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 2 4
60 4 ▶ my $class = ref($code) ? "Dumbbench::Instance::PerlSub" : "Dumbbench::Instance::PerlEval";
Branch: 1 branch
BranchTrueFalseTotal
ref $code ? : 1 3 4
61 4 $bench->add_instances(
62 $class->new(
63 name => $name, code => $code,
64 )
65 );
66 4 return $bench;
67 }
68  
69 2 ▶sub timeit {
Subroutine timeit: called CC=1 SCAR=0.0
Pod documented
70 2 my $count = shift;
71 2 my $code = shift;
72 2 my $bench = _prepare($count, $code);
73 2 $bench->run;
74  
75 2 return __PACKAGE__->_new(
76 instance => ($bench->instances)[0],
77 );
78 }
79  
80 1 ▶sub timethis {
Subroutine timethis: called CC=2 SCAR=6.9
Pod documented
81 1 my $count = shift;
82 1 my $code = shift;
83 1 my $title = shift;
84 1 ▶ $title = 'timethis ' . $count if not defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 1 0 1
85 1 my $style = shift;
86 1 my $res = timeit($count, $code);
87 1 $res->{name} = $title;
88 1 print "$title: ", $res->timestr($style), "\n";
89 1 return $res;
90 }
91  
92 1 ▶sub _timethese_guts {
Subroutine _timethese_guts: called CC=7 SCAR=19.6
93 1 my $count = shift;
94 1 my $instances = shift;
95 1 my $silent = shift;
96  
97 1 my $max_name_len = 1;
98 1 my $bench = _dumbbench_from_count($count); # FIXME %opt?
99 1 foreach my $name (sort keys %$instances) {
100 2 _prepare($count, $instances->{$name}, $name, $bench);
101 2 ▶ $max_name_len = length($name) if length($name) > $max_name_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $name > $max_name_len 1 1 2
102 }
103  
104 1 $bench->run;
105 1 ▶ $bench->verbosity(0) if $silent;
Branch: 1 branch
BranchTrueFalseTotal
if $silent 0 1 1
106  
107 1 ▶ if (not $silent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($silent) 1 0 1
108 1 print "Benchmark: ran ",
109 join(', ', map $_->name, $bench->instances),
110 ".\n";
111 }
112  
113 1 my $result = {};
114 1 foreach my $inst ($bench->instances) {
115 2 my $r = $result->{$inst->name} = __PACKAGE__->_new(
116 instance => $inst,
117 );
118 2 ▶ if (not $silent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($silent) 2 0 2
119 2 printf("%${max_name_len}s: ", $r->name);
120 2 print $r->timestr(), "\n";
121 }
122 }
123 1 return $result;
124 }
125  
126 1 ▶sub timethese {
Subroutine timethese: called CC=4 SCAR=16.4
Pod documented
127 1 my $count = shift;
128 1 my $instances = shift;
129 1 ▶ Carp::croak("Need count and code-hashref as arguments")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and ref $instances and ref $instances eq "HASH" 0 1 1
Condition33%2/6
defined $count and ref $instances and ref $instances eq "HASH"
!ll&&!rl&&r
001
defined $count and ref $instances
!ll&&!rl&&r
001
MC/DC0%0/3
defined $count and ref $instances and ref $instances eq "HASH"
defined $count ref $instances ref $instances eq "HASH"
Truth table25%1/4
A and B and C
A= defined $countB= ref $instancesC= ref $instances eq "HASH"
ABCresult
0XX0
10X0
1100
1111
130 if not defined $count or not ref($instances) or not ref($instances) eq 'HASH';
131  
132 1 return _timethese_guts($count, $instances, 0);
133 }
134  
135  
136 0 ▶sub cmpthese {
Subroutine cmpthese: not called CC=13 SCAR=52.0
Pod documented
137 0 my $count = shift;
138 0 my $codehashref = shift;
139 0 ▶ my $style = shift || ''; # ignored unless 'none'
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
140  
141 0 my $results;
142 0 ▶ if (ref($count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $count) { } 0 0 0
143 0 $results = $count;
144 }
145 else {
146 0 $results = _timethese_guts($count, $codehashref, 'silent');
147 }
148  
149 0 my @sort_res = map [$_, $results->{$_}, $results->{$_}->_rate], keys %$results;
150 0 @sort_res = sort { $a->[2] <=> $b->[2] } @sort_res;
151  
152 0 my @cols = map $_->[0], @sort_res;
153 0 my @rows = (
154 ['', 'Rate/s', 'Precision/s', @cols]
155 );
156  
157 0 foreach my $record (@sort_res) {
158 0 my ($name, $bench, $rate) = @$record;
159 0 my $rstr = $bench->_rate_str($rate);
160  
161 0 $rstr =~ s/\s+//g;
162  
163 0 my @rstr = split /\+-/, $rstr;
164  
165 0 my @row;
166 0 push @row, $name, @rstr;
167  
168 0 foreach my $cmp_record (@sort_res) {
169 0 my ($cmp_name, $cmp_bench, $cmp_rate) = @$cmp_record;
170 0 ▶ if ($cmp_name eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp_name eq $name) 0 0 0
171 0 push @row, '--';
172 0 next;
173 }
174  
175 0 my $cmp = 100*$rate/$cmp_rate - 100;
176 # skip the uncertainty if it's less than one permille
177 # absolute or relative
178 0 ▶ if ($cmp->raw_error->[0] < 1.e-1
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp->raw_error->[0] < 0.1 or ($cmp->raw_error->[0] + 1e-15) / $cmp->raw_number < 0.001) { } 0 0 0
Condition0%0/3
$cmp->raw_error->[0] < "0.1" or ($cmp->raw_error->[0] + "1e-15") / $cmp->raw_number < "0.001"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmp->raw_error->[0] < "0.1" or ($cmp->raw_error->[0] + "1e-15") / $cmp->raw_number < "0.001"
$cmp->raw_error->[0] < "0.1" ($cmp->raw_error->[0] + "1e-15") / $cmp->raw_number < "0.001"
Truth table0%0/3
A or B
A= $cmp->raw_error->[0] < "0.1"B= ($cmp->raw_error->[0] + "1e-15") / $cmp->raw_number < "0.001"
ABresult
1X1
011
000
179 or ($cmp->raw_error->[0]+1.e-15)/$cmp->raw_number < 1.e-3)
180 {
181 0 my $rounded = Number::WithError::round_a_number($cmp->raw_number, -1);
182 0 push @row, sprintf('%.1f', $rounded) . '%';
183 }
184 else {
185 0 my $cmp_str = $bench->_rate_str($cmp).'%'; # abuse
186 0 $cmp_str =~ s/\s+//g;
187 0 push @row, $cmp_str;
188 }
189 }
190  
191 0 push @rows, \@row;
192 }
193  
194 0 ▶ if (lc($style) ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $style ne "none") 0 0 0
195 # find the max column lengths
196 # could be done in the above iteration, too
197 0 my $ncols = @{$rows[0]};
198 0 my @col_len = ((0) x $ncols);
199 0 foreach my $row (@rows) {
200 0 foreach my $colno (0..$ncols-1) {
201 0 ▶ $col_len[$colno] = length($row->[$colno])
Branch: 1 branch
BranchTrueFalseTotal
if length $row->[$colno] > $col_len[$colno] 0 0 0
202 if length($row->[$colno]) > $col_len[$colno];
203 }
204 }
205  
206 0 my $format = join( ' ', map { "%${_}s" } @col_len) . "\n";
207 0 substr( $format, 1, 0 ) = '-'; # right-align name
208  
209 0 foreach my $row (@rows) {
210 0 printf($format, @$row);
211 }
212 }
213  
214 0 return \@rows;
215 }
216  
217  
218 #####################################
219 # the fake-OO stuff
220 2 ▶use Class::XSAccessor {
Subroutine BEGIN: called CC=1 SCAR=0.0
221 2 getters => {
222 _result => 'result',
223 name => 'name',
224 },
225 2 };
226 # No. Users aren't meant to create new objects at this point.
227 8 ▶sub _new {
Subroutine _new: called CC=3 SCAR=11.0
228 8 my $class = shift;
229 8 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 4 4 8
230 8 my %args = @_;
231 8 my $self = bless {} => $class;
232 8 ▶ if (defined $args{instance}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'instance'}) { } 4 4 8
233 4 my $inst = $args{instance};
234 4 $self->{name} = $inst->name;
235 4 $self->{result} = $inst->result->new;
236 }
237 else {
238 4 %$self = %args;
239 }
240 8 return $self;
241 }
242  
243 0 ▶sub iters {
Subroutine iters: not called CC=1 SCAR=6.9
Pod documented
244 0 my $self = shift;
245 0 return $self->_result->nsamples;
246 }
247  
248 1 ▶sub timesum {
Subroutine timesum: called CC=1 SCAR=0.0
Pod documented
249 1 my $self = shift;
250 1 my $other = shift;
251 1 my $result = $self->_result + $other->_result;
252 1 return $self->_new(result => $result, name => '');
253 }
254  
255  
256 1 ▶sub timediff {
Subroutine timediff: called CC=1 SCAR=0.0
Pod documented
257 1 my $self = shift;
258 1 my $other = shift;
259 1 my $result = $self->_result - $other->_result;
260 1 return $self->_new(result => $result, name => '');
261 }
262  
263 4 ▶sub timestr {
Subroutine timestr: called CC=6 SCAR=18.3
Pod documented
264 4 my $self = shift;
265 4 ▶ my $style = shift || '';
Condition50%1/2
shift() || ''
l!l
04
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
266 4 ▶ my $format = shift || '5.2f';
Condition50%1/2
shift() || '5.2f'
l!l
04
MC/DC0%0/1
shift() || '5.2f'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
267  
268 4 $style = lc($style);
269 4 ▶ return("") if $style eq 'none'; # what's the point?
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "none" 0 4 4
270  
271 4 my $res = $self->_result;
272 4 my $time = $res->number;
273 4 my $err = $res->error->[0];
274 4 ▶ my $rel = ($time > 0 ? $err/$time : 1) * 100;
Branch: 1 branch
BranchTrueFalseTotal
$time > 0 ? : 4 0 4
275 4 my $digits;
276 4 ▶ if ($rel =~ /^([0\.]*)/) { # quick'n'dirty significant digits
Branch: 1 branch
BranchTrueFalseTotal
if ($rel =~ /^([0\.]*)/) 4 0 4
277 4 $digits = length($1) + 1;
278 }
279 4 $rel = sprintf("\%.${digits}f", $rel);
280  
281 4 my $rate = $self->_rate_str;
282 4 my $str = "$time +- $err wallclock secs ($rel%) @ ($rate)/s (n=" . $res->nsamples . ")";
283  
284 4 return $str;
285 }
286  
287 4 ▶sub _rate_str {
Subroutine _rate_str: called CC=2 SCAR=7.1
288 4 my $self = shift;
289 4 ▶ my $per_sec = shift || $self->_rate;
Condition33%1/3
shift() || $self->_rate
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || $self->_rate
shift() $self->_rate
Truth table33%1/3
A || B
A= shift()B= $self->_rate
ABresult
1X1
011
000
290  
291 # The joys of people-not-enjoying-scientific-notation
292 4 my $digit = $per_sec->significant_digit;
293 4 my $before_radix = length(int($per_sec->raw_number));
294 # FIXME: not clear if this makes sense. Need to revisit later in a day.
295 #$before_radix = 0 if int($per_sec->raw_number) == 0;
296 4 $digit = $before_radix - $digit;
297 4 my $ps_format = "%${digit}g";
298 4 my $ps_string = sprintf("$ps_format +- $ps_format", $per_sec->number*1., $per_sec->error->[0]);
299 4 return $ps_string;
300 }
301  
302 4 ▶sub _rate {
Subroutine _rate: called CC=1 SCAR=0.0
303 4 my $self = shift;
304 4 my $res = $self->_result;
305 4 my $per_sec = 1./($res+1.e-20); # the joys of overloading. See Number::WithError.
306 4 return $per_sec;
307 }
308  
309  
310  
311 2 1;
312  
313 __END__
« Dumbbench/Instance/Cmd.pm ↑ Summary Dumbbench.pm »