Dumbbench/Instance/PerlEval.pm

filter: Statementstmt 100% 52 / 52 Branchbran 100% 8 / 8 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 100% 77 / 77 CC 7 SCAR 10.2
CC
7
Cov
100%
_run
6.0
doeval
2.0
BEGIN
1.0
CRAP 2.8
« Dumbbench/Instance/PerlSub.pm ↑ Summary Dumbbench/Stats.pm »
1 package # hide from PAUSE
2 Dumbbench::Instance::PerlEval::_Lexical;
3 # clean lexical scope
4 50 ▶sub doeval {
Subroutine doeval: called CC=2 SCAR=6.9
5 50 local $_ = shift;
6 50 for (1..$_) {
7 563 local $_;
8 563 eval ${shift()};
9 }
10 }
11  
12 package Dumbbench::Instance::PerlEval;
13 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Dumbbench::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use parent 'Dumbbench::Instance';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use Class::XSAccessor {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 getters => [qw(
23 code
24 dry_run_code
25 )],
26 accessors => [qw(
27 _n_loop_timings
28 _n_dry_loop_timings
29 )],
30 3 };
31  
32 3 ▶use constant TOO_SMALL => 1.e-4;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =encoding utf8
35  
36 =head1 NAME
37  
38 Dumbbench::Instance::PerlEval - Benchmarks a string of Perl code
39  
40 =head1 SYNOPSIS
41  
42 use Dumbbench;
43  
44 my $bench = Dumbbench->new(
45 target_rel_precision => 0.005, # seek ~0.5%
46 initial_runs => 20, # the higher the more reliable
47 );
48 $bench->add_instances(
49 Dumbbench::Instance::PerlEval->new(name => 'mauve', code => 'for(1..1e9){$i++}'),
50 # ... more things to benchmark ...
51 );
52 $bench->run();
53 # ...
54  
55 =head1 DESCRIPTION
56  
57 This class inherits from L<Dumbbench::Instance> and implements
58 benchmarking of strings of Perl code using C<eval "">.
59  
60 =head1 METHODS
61  
62 =head2 new
63  
64 Constructor that takes named arguments.
65  
66 In addition to the properties of the base class, the
67 C<Dumbbench::Instance::PerlEval> constructor requires a
68 C<code> parameter. The C<code> needs to be a string that
69 is suitable for passing repeatedly to string-C<eval>.
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, a simple
74 C<eval> is used for this, so it's unlikely you will need the dry-run
75 unless you want to strip out the compile-time overhead of your code.
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  
89 6 ▶sub single_run {
Subroutine single_run: called CC=1 SCAR=0.0
Pod documented
90 6 my $self = shift;
91 6 return $self->_run(0);
92 }
93  
94 32 ▶sub single_dry_run {
Subroutine single_dry_run: called CC=1 SCAR=0.0
Pod documented
95 32 my $self = shift;
96 32 return $self->_run(1);
97 }
98  
99 38 ▶sub _run {
Subroutine _run: called CC=6 SCAR=17.9
100 38 my $self = shift;
101 38 my $dry = shift;
102 38 ▶ my $code_acc = $dry ? 'dry_run_code' : 'code';
Branch: 1 branch
BranchTrueFalseTotal
$dry ? : 32 6 38
103 38 ▶ my $n_loop_acc = $dry ? '_n_dry_loop_timings' : '_n_loop_timings';
Branch: 1 branch
BranchTrueFalseTotal
$dry ? : 32 6 38
104  
105 38 my $code = $self->$code_acc;
106 38 ▶ $code = '' if not defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 32 6 38
107  
108 38 my $duration;
109 38 ▶ my $n = $self->$n_loop_acc || 1;
Condition100%2/2
$self->$n_loop_acc || 1
l!l
326
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
110 38 while (1) {
111 #my $start;
112 #my $tbase = Time::HiRes::time();
113 #while ( ($start = Time::HiRes::time()) <= $tbase+1.e-15 ) {} # wait for clock tick. See discussion in Benchmark.pm comments
114 50 my $start = clock_gettime(CLOCK_MONOTONIC);
115 50 Dumbbench::Instance::PerlEval::_Lexical::doeval($n, \$code);
116 50 my $end = clock_gettime(CLOCK_MONOTONIC);
117  
118 50 $duration = $end-$start;
119 50 ▶ if ($duration > TOO_SMALL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration > "0.0001") 38 12 50
120 38 last;
121 }
122 12 $n *= 2;
123 }
124 38 $self->$n_loop_acc($n);
125  
126 38 return $duration / $n;
127 }
128  
129  
130  
131 3 1;
132  
133  
134 __END__
« Dumbbench/Instance/PerlSub.pm ↑ Summary Dumbbench/Stats.pm »