Dumbbench/Instance.pm

filter: Statementstmt 39.6% 21 / 53 Branchbran 6.2% 1 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 6 / 12 Podpod 100% 5 / 5 totaltotal 35.8% 33 / 92 CC 12 SCAR 30.1
CC
12
Cov
30%
_timings_as_histogram
56.0
clone
12.0
timings_as_histogram
6.0
CRAP 20.2
« Dumbbench.pm ↑ Summary Dumbbench/CPUFrequencyPinner.pm »
1 package Dumbbench::Instance;
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 List::Util qw/min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 require Dumbbench::Instance::Cmd;
8 3 require Dumbbench::Instance::PerlEval;
9 3 require Dumbbench::Instance::PerlSub;
10  
11 3 ▶use Class::XSAccessor {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 constructor => 'new',
13 accessors => [qw(
14 name
15 dry_result
16 result
17 )],
18 getters => [qw(timings dry_timings)],
19 3 };
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 Dumbbench::Instance - A benchmark instance within a Dumbbench
26  
27 =head1 SYNOPSIS
28  
29 use Dumbbench;
30  
31 my $bench = Dumbbench->new(
32 target_rel_precision => 0.005, # seek ~0.5%
33 initial_runs => 20, # the higher the more reliable
34 );
35 $bench->add_instances(
36 Dumbbench::Instance::Cmd->new(name => 'mauve', command => [qw(perl -e 'something')]),
37 # ... more things to benchmark ...
38 );
39 $bench->run();
40 # ...
41  
42 =head1 DESCRIPTION
43  
44 This module is the base class for all benchmark instances. For example,
45 for benchmarking external commands, you should use L<Dumbbench::Instance::Cmd>.
46  
47 The synopsis shows how instances of subclasses of
48 C<Dumbbench::Instance> are added to a benchmark run.
49  
50 =head1 METHODS
51  
52 =head2 new
53  
54 Constructor that takes named arguments. In this base class,
55 the only recognized argument is an instance C<name>.
56  
57 =head2 timings
58  
59 Returns the internal array reference of timings or undef if
60 there aren't any.
61  
62 =head2 dry_timings
63  
64 Same as C<timings> but for dry-run timings.
65  
66 =head2 name
67  
68 Returns the name of the instance.
69  
70 =head2 clone
71  
72 Returns a full (deep) copy of the object. May have to be
73 augmented in subclasses.
74  
75 =cut
76  
77 0 ▶sub clone {
Subroutine clone: not called CC=3 SCAR=24.8
Pod documented
78 0 my $self = shift;
79 0 my $clone = bless({%$self} => ref($self));
80  
81 0 ▶ if (defined $clone->dry_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $clone->dry_result) 0 0 0
82 0 $clone->dry_result($clone->dry_result->new);
83 }
84 0 ▶ if (defined $clone->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $clone->result) 0 0 0
85 0 $clone->result($clone->result->new);
86 }
87 0 return $clone;
88 }
89  
90 =head2 single_run
91  
92 Needs to be implemented in subclasses:
93 A method that performs a single benchmark run and returns the
94 duration of the run in seconds.
95  
96 =cut
97  
98 0 ▶sub single_run {
Subroutine single_run: not called CC=1 SCAR=6.9
Pod documented
99 0 my $self = shift;
100 0 Carp::croak("Can't single_run Dumbbench::Instance: Choose a subclass that implements running.");
101 }
102  
103 =head2 single_dry_run
104  
105 Needs to be implemented in subclasses:
106 A method that performs a single dry-run and returns the
107 duration of the run in seconds.
108  
109 =cut
110  
111 0 ▶sub single_dry_run {
Subroutine single_dry_run: not called CC=1 SCAR=6.9
Pod documented
112 0 my $self = shift;
113 0 Carp::croak("Can't single_dry_run Dumbbench::Instance: Choose a subclass that implements dry-running.");
114 }
115  
116 =head2 timings_as_histogram
117  
118 If the optional L<SOOT> module is installed,
119 C<Dumbbench> can generate histograms of the timing distributions.
120  
121 This method creates such a histogram object (of type C<TH1D>)
122 and returns it. If C<SOOT> is not available, this method
123 returns the empty list.
124  
125 =cut
126  
127 0 ▶sub timings_as_histogram {
Subroutine timings_as_histogram: not called CC=2 SCAR=17.9
Pod documented
128 0 my $self = shift;
129 0 ▶ my $timings = $self->timings||[];
Condition0%0/2
$self->timings || []
l!l
00
MC/DC0%0/1
$self->timings || []
$self->timings
Truth table0%0/2
A
A= $self->timings
Aresult
11
00
130 0 return $self->_timings_as_histogram($timings);
131 }
132  
133 =head2 dry_timings_as_histogram
134  
135 Same as C<timings_as_histogram>, but for the timings
136 from dry-runs.
137  
138 =cut
139  
140 0 ▶sub dry_timings_as_histogram {
Subroutine dry_timings_as_histogram: not called CC=2 SCAR=17.9
Pod documented
141 0 my $self = shift;
142 0 ▶ my $timings = $self->dry_timings||[];
Condition0%0/2
$self->dry_timings || []
l!l
00
MC/DC0%0/1
$self->dry_timings || []
$self->dry_timings
Truth table0%0/2
A
A= $self->dry_timings
Aresult
11
00
143 0 return $self->_timings_as_histogram($timings, 'dry');
144 }
145  
146 0 ▶sub _timings_as_histogram {
Subroutine _timings_as_histogram: not called CC=7 SCAR=40.3
147 0 my $self = shift;
148 0 eval "require SOOT;";
149 0 ▶ return() if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
150  
151 0 my $timings = shift;
152 0 my $is_dry = shift;
153 0 ▶ my $min = (@$timings ? min(@$timings)*0.95 : 0);
Branch: 1 branch
BranchTrueFalseTotal
@$timings ? : 0 0 0
154 0 ▶ my $max = (@$timings ? max(@$timings)*1.05 : 1);
Branch: 1 branch
BranchTrueFalseTotal
@$timings ? : 0 0 0
155 0 my $n = max(@$timings/8, 100);
156 0 ▶ my $prefix = $is_dry ? 'dry_' : '';
Branch: 1 branch
BranchTrueFalseTotal
$is_dry ? : 0 0 0
157 0 ▶ my $name = defined($self->name) ? "${prefix}timings_" . $self->name : "${prefix}timings";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name ? : 0 0 0
158 0 my $hist = TH1D->new($name, "distribution of benchmark ${prefix}timings", int($n), $min, $max);
159 0 $hist->GetXaxis()->SetTitle("run time [s]");
160 0 $hist->GetYaxis()->SetTitle("#");
161 0 $hist->Fill($_) for @$timings;
162 0 return $hist;
163 }
164  
165 8 ▶sub _name_prefix {
Subroutine _name_prefix: called CC=2 SCAR=7.2
166 8 my $name = $_[0]->name;
167 8 ▶ return defined($name) ? "$name: " : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 8 0 8
168 }
169  
170 3 1;
171  
172 __END__
« Dumbbench.pm ↑ Summary Dumbbench/CPUFrequencyPinner.pm »