Dumbbench.pm

filter: Statementstmt 75.8% 113 / 149 Branchbran 51.3% 37 / 72 Conditioncond 36.3% 8 / 22 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 81.2% 13 / 16 Podpod 66.6% 6 / 9 totaltotal 63.1% 178 / 282 CC 53 SCAR 34.4
CC
53
Cov
65%
report
56.0
_run
49.6
new
13.1
CRAP 31.3
« Benchmark/Dumb.pm ↑ Summary Dumbbench/Instance.pm »
1 package Dumbbench;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.505';
8  
9 3 require Dumbbench::Result;
10 3 require Dumbbench::Stats;
11 3 require Dumbbench::Instance;
12  
13 3 ▶use Params::Util '_INSTANCE';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Class::XSAccessor {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 getters => [qw(
17 target_rel_precision
18 target_abs_precision
19 initial_runs
20 max_iterations
21 variability_measure
22 started
23 outlier_rejection
24 subtract_dry_run
25 )],
26 accessors => [qw(verbosity)],
27 3 };
28  
29  
30 3 ▶sub new {
Subroutine new: called CC=7 SCAR=25.7
Pod documented
31 3 my $proto = shift;
32 3 ▶ my $class = ref($proto)||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
33 3 my $self;
34 3 ▶ if (not ref($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $proto) { } 3 0 3
35 3 $self = bless {
36 verbosity => 0,
37 target_rel_precision => 0.05,
38 target_abs_precision => 0,
39 initial_runs => 20,
40 max_iterations => 10000,
41 variability_measure => 'mad',
42 instances => [],
43 started => 0,
44 outlier_rejection => 3,
45 subtract_dry_run => 1,
46 @_,
47 } => $class;
48 }
49 else {
50 0 $self = bless {%$proto, @_} => $class;
51 0 my @inst = $self->instances;
52 0 $self->{instances} = [];
53 0 foreach my $instance (@inst) {
54 0 push @{$self->{instances}}, $instance->new;
55 }
56 }
57  
58 3 ▶ if ($self->target_abs_precision <= 0 and $self->target_rel_precision <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->target_abs_precision <= 0 and $self->target_rel_precision <= 0) 0 3 3
Condition33%1/3
$self->target_abs_precision <= 0 and $self->target_rel_precision <= 0
!ll&&!rl&&r
030
MC/DC0%0/2
$self->target_abs_precision <= 0 and $self->target_rel_precision <= 0
$self->target_abs_precision <= 0 $self->target_rel_precision <= 0
Truth table33%1/3
A and B
A= $self->target_abs_precision <= 0B= $self->target_rel_precision <= 0
ABresult
0X0
100
111
59 0 Carp::croak("Need either target_rel_precision or target_abs_precision > 0");
60 }
61 3 ▶ if ($self->initial_runs < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->initial_runs < 6) 3 0 3
62 3 Carp::carp("Number of initial runs is very small (<6). Precision will be off.");
63 }
64  
65 3 return $self;
66 }
67  
68 4 ▶sub add_instances {
Subroutine add_instances: called CC=4 SCAR=15.2
Pod documented
69 4 my $self = shift;
70  
71 4 ▶ if ($self->started) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->started) 0 4 4
72 0 Carp::croak("Can't add instances after the benchmark has been started");
73 }
74 4 foreach my $instance (@_) {
75 4 ▶ if (not _INSTANCE($instance, 'Dumbbench::Instance')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_INSTANCE($instance, "Dumbbench::Instance")) 0 4 4
76 0 Carp::croak("Argument to add_instances is not a Dumbbench::Instance");
77 }
78 }
79 4 push @{$self->{instances}}, @_;
80 }
81  
82 10 ▶sub instances {
Subroutine instances: called CC=1 SCAR=0.0
Pod documented
83 10 my $self = shift;
84 10 return @{$self->{instances}};
85 }
86  
87 3 ▶sub run {
Subroutine run: called CC=3 SCAR=11.4
Pod documented
88 3 my $self = shift;
89 3 ▶ Carp::croak("Can't re-run same benchmark instance") if $self->started;
Branch: 1 branch
BranchTrueFalseTotal
if $self->started 0 3 3
90 3 ▶ $self->dry_run_timings if $self->subtract_dry_run;
Branch: 1 branch
BranchTrueFalseTotal
if $self->subtract_dry_run 3 0 3
91 3 $self->run_timings;
92 }
93  
94 3 ▶sub run_timings {
Subroutine run_timings: called CC=3 SCAR=11.1
Pod undocumented
95 3 my $self = shift;
96 3 $self->{started} = 1;
97 3 foreach my $instance ($self->instances) {
98 4 ▶ next if $instance->result;
Branch: 1 branch
BranchTrueFalseTotal
if $instance->result 0 4 4
99 4 $self->_run($instance);
100 }
101 }
102  
103 3 ▶sub dry_run_timings {
Subroutine dry_run_timings: called CC=3 SCAR=11.1
Pod undocumented
104 3 my $self = shift;
105 3 $self->{started} = 1;
106  
107 3 foreach my $instance ($self->instances) {
108 4 ▶ next if $instance->dry_result;
Branch: 1 branch
BranchTrueFalseTotal
if $instance->dry_result 0 4 4
109 4 $self->_run($instance, 'dry');
110 }
111 }
112  
113 8 ▶sub _run {
Subroutine _run: called CC=32 SCAR=39.0
114 8 my $self = shift;
115 8 my $instance = shift;
116 8 my $dry = shift;
117  
118 8 my $name = $instance->_name_prefix;
119  
120 # for overriding in case of dry-run mode
121 8 ▶ my $V = $self->verbosity || 0;
Condition50%1/2
$self->verbosity || 0
l!l
08
MC/DC0%0/1
$self->verbosity || 0
$self->verbosity
Truth table50%1/2
A
A= $self->verbosity
Aresult
11
00
122 8 my $initial_timings = $self->initial_runs;
123 8 my $abs_precision = $self->target_abs_precision;
124 8 my $rel_precision = $self->target_rel_precision;
125 8 my $max_iterations = $self->max_iterations;
126  
127 8 ▶ if ($dry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry) 4 4 8
128 4 ▶ $V--; $V = 0 if $V < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $V < 0 4 0 4
129 4 $initial_timings *= 5;
130 4 $abs_precision = 0;
131 4 $rel_precision /= 2;
132 4 $max_iterations *= 10;
133 }
134  
135 8 ▶ print "${name}Running initial timing for warming up the cache...\n" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $V 0 8 8
136 8 ▶ if ($dry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry) { } 4 4 8
137 # be generous, this is fast
138 4 $instance->single_dry_run() for 1..3;
139 }
140 else {
141 4 $instance->single_run();
142 }
143  
144 8 my @timings;
145 8 ▶ print "${name}Running $initial_timings initial timings...\n" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $V 0 8 8
146 8 foreach (1..$initial_timings) {
147 24 ▶ print "${name}Running timing $_...\n" if $V > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $V > 1 0 24 24
148 24 ▶ push @timings, ($dry ? $instance->single_dry_run() : $instance->single_run());
Branch: 1 branch
BranchTrueFalseTotal
$dry ? : 20 4 24
149 }
150  
151 8 ▶ print "${name}Iterating until target precision reached...\n" if $V;
Branch: 1 branch
BranchTrueFalseTotal
if $V 0 8 8
152  
153 8 my $stats = Dumbbench::Stats->new(data => \@timings);
154 8 my $sigma;
155 my $mean;
156  
157 #My mental model for the distribution was Gauss+outliers.
158 #If my expectation is correct, the following algorithm should produce a reasonable EV +/- uncertainty:
159 #1) Calc. median of the whole distribution.
160 #2) Calculate the median-absolute deviation from the whole distribution (MAD, see wikipedia). It needs rescaling to become a measure of variability that is robust against outliers.
161 #(The MAD will be our initial guess for a "sigma")
162 #3) Reject the samples that are outside $median +/- $n*$MAD.
163 #I was expecting several high outliers but few lows. An ordinary truncated mean or the like would be unsuitable for removing the outliers in such a case since you'd get a significant upward bias of your EV.
164 #By using the median as the initial guess, we keep the initial bias to a minimum. The MAD will be similarly unaffected by outliers AND the asymmetry.
165 #Thus cutting the tails won't blow up the bias too strongly (hopefully).
166 #4) Calculate mean & MAD/sqrt($n) of the remaining distribution. These are our EV and uncertainty on the mean.
167  
168 8 my $n_good = 0;
169 8 my $variability_measure = $self->variability_measure;
170 8 while (1) {
171 20 my ($good, $outliers) = $stats->filter_outliers(
172 variability_measure => $variability_measure,
173 nsigma_outliers => $self->outlier_rejection,
174 );
175  
176 20 $n_good = @$good;
177  
178 20 ▶ if (not $n_good and @timings >= $max_iterations) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $n_good and @timings >= $max_iterations) 0 20 20
Condition33%1/3
not $n_good and @timings >= $max_iterations
!ll&&!rl&&r
2000
MC/DC0%0/2
not $n_good and @timings >= $max_iterations
not $n_good @timings >= $max_iterations
Truth table33%1/3
not(A) and B
A= not $n_goodB= @timings >= $max_iterations
ABresult
0X0
100
111
179 0 $mean = 0; $sigma = 0;
180 0 last;
181 }
182  
183 20 ▶ if ($n_good) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_good) 20 0 20
184 20 my $new_stats = Dumbbench::Stats->new(data => $good);
185 20 $sigma = $new_stats->$variability_measure() / sqrt($n_good);
186 20 $mean = $new_stats->mean();
187  
188 # stop condition
189 20 my $need_iter = 0;
190 20 ▶ if ($rel_precision > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_precision > 0) 20 0 20
191 20 my $rel = $sigma/$mean;
192 20 ▶ print "${name}Reached relative precision $rel (neeed $rel_precision).\n" if $V > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $V > 1 0 20 20
193 20 ▶ $need_iter++ if $rel > $rel_precision;
Branch: 1 branch
BranchTrueFalseTotal
if $rel > $rel_precision 4 16 20
194 }
195 20 ▶ if ($abs_precision > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abs_precision > 0) 0 20 20
196 0 ▶ print "${name}Reached absolute precision $sigma (neeed $abs_precision).\n" if $V > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $V > 1 0 0 0
197 0 ▶ $need_iter++ if $sigma > $abs_precision;
Branch: 1 branch
BranchTrueFalseTotal
if $sigma > $abs_precision 0 0 0
198 }
199 20 ▶ if ($n_good < $initial_timings) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_good < $initial_timings) 8 12 20
200 8 $need_iter++;
201 }
202 20 ▶ last if not $need_iter or @timings >= $max_iterations;
Branch: 1 branch
BranchTrueFalseTotal
if not $need_iter or @timings >= $max_iterations 8 12 20
Condition66%2/3
not $need_iter or @timings >= $max_iterations
l!l&&r!l&&!r
8012
MC/DC50%1/2
not $need_iter or @timings >= $max_iterations
not $need_iter @timings >= $max_iterations
Truth table66%2/3
not(A) or B
A= not $need_iterB= @timings >= $max_iterations
ABresult
1X1
011
000
203 }
204  
205 # progressively run more new timings in one go. Otherwise,
206 # we start to stall on the O(n*log(n)) complexity of the median.
207 12 my $n = List::Util::min( $max_iterations - @timings, List::Util::max(1, @timings*0.05) );
208 12 ▶ push @timings, ($dry ? $instance->single_dry_run() : $instance->single_run()) for 1..$n;
Branch: 1 branch
BranchTrueFalseTotal
$dry ? : 12 0 12
209 } # end while more data required
210  
211 8 ▶ if (@timings >= $max_iterations and not $dry) {
Branch: 1 branch
BranchTrueFalseTotal
if (@timings >= $max_iterations and not $dry) 0 8 8
Condition33%1/3
@timings >= $max_iterations and not $dry
!ll&&!rl&&r
800
MC/DC0%0/2
@timings >= $max_iterations and not $dry
@timings >= $max_iterations not $dry
Truth table33%1/3
A and not(B)
A= @timings >= $max_iterationsB= not $dry
ABresult
0X0
100
111
212 0 print "${name}Reached maximum number of iterations. Stopping. Precision not reached.\n";
213 }
214  
215 # rescale sigma
216 # This is necessary since by cutting everything outside of n-sigma,
217 # we artificially reduce the variability of the main distribution.
218 8 ▶ if ($self->outlier_rejection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->outlier_rejection) 8 0 8
219 # TODO implement
220 }
221  
222 8 my $result = Dumbbench::Result->new(
223 timing => $mean,
224 uncertainty => $sigma,
225 nsamples => $n_good,
226 );
227  
228 8 ▶ if ($dry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry) { } 4 4 8
229 4 $instance->{dry_timings} = \@timings;
230 4 $instance->dry_result($result);
231 }
232 else {
233 4 $instance->{timings} = \@timings;
234 4 ▶ $result -= $instance->dry_result
Branch: 1 branch
BranchTrueFalseTotal
if defined $instance->dry_result and $self->subtract_dry_run 4 0 4
Condition33%1/3
defined $instance->dry_result and $self->subtract_dry_run
!ll&&!rl&&r
004
MC/DC0%0/2
defined $instance->dry_result and $self->subtract_dry_run
defined $instance->dry_result $self->subtract_dry_run
Truth table33%1/3
A and B
A= defined $instance->dry_resultB= $self->subtract_dry_run
ABresult
0X0
100
111
235 if defined $instance->dry_result and $self->subtract_dry_run;
236 4 $instance->result($result);
237 }
238 }
239  
240 0 ▶sub report {
Subroutine report: not called CC=7 SCAR=40.3
Pod documented
241 0 my ( $self, $raw, $options ) = @_;
242 0 ▶ $options ||= {};
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
243 0 ▶ Carp::carp( "The second option to report was not a hash ref" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options eq ref {} 0 0 0
244 unless ref $options eq ref {};
245  
246 0 foreach my $instance ($self->instances) {
247 0 my $result = $instance->result;
248 0 ▶ my $result_str = ($options->{float}) ? unscientific_notation($result) : "$result";
Branch: 1 branch
BranchTrueFalseTotal
$options->{'float'} ? : 0 0 0
249  
250 0 ▶ if (not $raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $raw) { } 0 0 0
251 0 my $mean = $result->raw_number;
252 0 my $sigma = $result->raw_error->[0];
253 0 my $name = $instance->_name_prefix;
254 printf(
255 "%sRan %u iterations (%u outliers).\n",
256 $name,
257 0 scalar(@{$instance->timings}),
258 0 scalar(@{$instance->timings})-$result->nsamples
259 );
260 0 printf(
261 "%sRounded run time per iteration (seconds): %s (%.1f%%)\n",
262 $name,
263 $result_str,
264 $sigma/$mean*100
265 );
266 0 ▶ if ($self->verbosity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbosity) 0 0 0
267 0 printf("%sRaw: $mean +/- $sigma\n", $name);
268 }
269 }
270 else {
271 0 print $result_str, "\n";
272 }
273 }
274 }
275  
276 0 ▶sub box_plot {
Subroutine box_plot: not called CC=1 SCAR=6.9
Pod documented
277 0 my $self = shift;
278 0 require Dumbbench::BoxPlot;
279  
280 0 return Dumbbench::BoxPlot->new($self);
281 }
282  
283 0 ▶sub unscientific_notation {
Subroutine unscientific_notation: not called CC=1 SCAR=6.9
Pod undocumented
284 0 sprintf( "%f %s %f", split( / /, $_[0] ) );
285 }
286  
287 3 1;
288  
289 __END__
« Benchmark/Dumb.pm ↑ Summary Dumbbench/Instance.pm »