Dumbbench/Instance/PerlSub.pm

filter: Statementstmt 100% 52 / 52 Branchbran 100% 8 / 8 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 97.5% 81 / 83 CC 8 SCAR 11.6
CC
8
Cov
98%
_run
7.0
runsub
2.0
__ANON__
1.0
CRAP 3.2
« Dumbbench/Result.pm ↑ Summary Dumbbench/Instance/PerlEval.pm »
1 package # hide from PAUSE
2 Dumbbench::Instance::PerlSub::_Lexical;
3 # clean lexical scope
4 28 ▶sub runsub {
Subroutine runsub: called CC=2 SCAR=6.9
5 28 my $n = shift;
6 28 my $code = shift;
7 28 local $_;
8 28 for (1..$n) {
9 3518 $code->();
10 }
11 }
12  
13 package Dumbbench::Instance::PerlSub;
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use Dumbbench::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use parent 'Dumbbench::Instance';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use Class::XSAccessor {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 getters => [qw(
24 code
25 dry_run_code
26 )],
27 accessors => [qw(
28 _n_loop_timings
29 _n_dry_loop_timings
30 )],
31 3 };
32  
33 3 ▶use constant TOO_SMALL => 1.e-4;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =encoding utf8
36  
37 =head1 NAME
38  
39 Dumbbench::Instance::PerlSub - Benchmarks a Perl code reference
40  
41 =head1 SYNOPSIS
42  
43 use Dumbbench;
44  
45 my $bench = Dumbbench->new(
46 target_rel_precision => 0.005, # seek ~0.5%
47 initial_runs => 20, # the higher the more reliable
48 );
49 $bench->add_instances(
50 Dumbbench::Instance::PerlSub->new(name => 'mauve', code => sub {...}),
51 # ... more things to benchmark ...
52 );
53 $bench->run();
54 # ...
55  
56 =head1 DESCRIPTION
57  
58 This class inherits from L<Dumbbench::Instance> and implements
59 benchmarking of Perl code references.
60  
61 =head1 METHODS
62  
63 =head2 new
64  
65 Constructor that takes named arguments.
66  
67 In addition to the properties of the base class, the
68 C<Dumbbench::Instance::PerlSub> constructor requires a
69 C<code> parameter. The C<code> needs to be subroutine reference.
70  
71 Optionally, you can provide a C<dry_run_code> option.
72 It has the same structure and purpose as the C<code>
73 option, but it is used for the dry-runs. By default, an empty sub
74 is used for this, so it's unlikely you will need the dry-run
75 unless you want to strip out some particular overhead.
76  
77 =head2 code
78  
79 Returns the code string that was set during construction.
80  
81 =head2 dry_run_code
82  
83 Returns the dry-run code string that was set during construction.
84  
85 =cut
86  
87 # Note: We don't need to override clone() since we don't have composite attributes
88 # Note2: Can't (reliably) clone code refs anyway :(
89  
90 2 ▶sub single_run {
Subroutine single_run: called CC=1 SCAR=0.0
Pod documented
91 2 my $self = shift;
92 2 return $self->_run(0);
93 }
94  
95 12 ▶sub single_dry_run {
Subroutine single_dry_run: called CC=1 SCAR=0.0
Pod documented
96 12 my $self = shift;
97 12 return $self->_run(1);
98 }
99  
100 14 ▶sub _run {
Subroutine _run: called CC=7 SCAR=19.5
101 14 my $self = shift;
102 14 my $dry = shift;
103 14 ▶ my $code_acc = $dry ? 'dry_run_code' : 'code';
Branch: 1 branch
BranchTrueFalseTotal
$dry ? : 12 2 14
104 14 ▶ my $n_loop_acc = $dry ? '_n_dry_loop_timings' : '_n_loop_timings';
Branch: 1 branch
BranchTrueFalseTotal
$dry ? : 12 2 14
105  
106 14 my $code = $self->$code_acc;
107 14 ▶ $code = sub {} if not ref($code) or not ref($code) eq 'CODE';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code and ref $code eq "CODE" 12 2 14
Condition66%2/3
ref $code and ref $code eq "CODE"
!ll&&!rl&&r
1202
MC/DC50%1/2
ref $code and ref $code eq "CODE"
ref $code ref $code eq "CODE"
Truth table66%2/3
A and B
A= ref $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
108  
109 14 my $duration;
110 14 ▶ my $n = $self->$n_loop_acc || 1;
Condition100%2/2
$self->$n_loop_acc || 1
l!l
122
MC/DC100%1/1
$self->$n_loop_acc || 1
$self->$n_loop_acc
Truth table100%2/2
A
A= $self->$n_loop_acc
Aresult
11
00
111 14 while (1) {
112 #my $start;
113 #my $tbase = Time::HiRes::time();
114 #while ( ($start = Time::HiRes::time()) <= $tbase+1.e-15 ) {} # wait for clock tick. See discussion in Benchmark.pm comments
115 28 my $start = clock_gettime(CLOCK_MONOTONIC);
116 28 Dumbbench::Instance::PerlSub::_Lexical::runsub($n, $code);
117 28 my $end = clock_gettime(CLOCK_MONOTONIC);
118  
119 28 $duration = $end-$start;
120 28 ▶ if ($duration > TOO_SMALL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration > "0.0001") 14 14 28
121 14 last;
122 }
123 14 $n *= 2;
124 }
125 14 $self->$n_loop_acc($n);
126  
127 14 return $duration / $n;
128 }
129  
130  
131  
132 3 1;
133  
134  
135 __END__
« Dumbbench/Result.pm ↑ Summary Dumbbench/Instance/PerlEval.pm »