Timer.pm

filter: Statementstmt 81.1% 168 / 207 Branchbran 55.8% 48 / 86 Conditioncond 35.5% 27 / 76 MC/DCmcdc 8.3% 3 / 36 Subroutinesub 92.5% 25 / 27 Podpod 100% 10 / 10 totaltotal 63.5% 281 / 442 CC 76 SCAR 35.7
CC
76
Cov
66%
reset
110.6
need_more_samples
42.0
_commify
30.0
CRAP 35.4
↑ Summary delta.pl »
1 package Benchmark::Timer;
2 6 require 5.005;
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 $VERSION = sprintf "%d.%02d%02d", q/0.71.11/ =~ /(\d+)/g;
10  
11 6 ▶use constant BEFORE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use constant ELAPSED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use constant LASTTAG => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use constant TAGS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use constant SKIP => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use constant MINIMUM => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use constant SKIPCOUNT => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use constant CONFIDENCE => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use constant ERROR => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use constant STAT => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # ------------------------------------------------------------------------
23 # Constructor
24  
25 sub new {
26 14 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
27 14 my $self = [];
28 14 bless $self, $class;
29 14 return $self->reset(@_);
30 }
31  
32  
33 # ------------------------------------------------------------------------
34 # Public methods
35  
36 sub reset {
37 17 ▶ my $self = shift;
Subroutine reset: called CC=25 SCAR=47.1
Pod documented
38 17 my %args = @_;
39  
40 17 $self->[BEFORE] = {}; # [ gettimeofday ] storage
41 17 $self->[ELAPSED] = {}; # elapsed fractional seconds
42 17 $self->[LASTTAG] = undef; # what the last tag was
43 17 $self->[TAGS] = []; # keep list of tags in order seen
44 17 $self->[SKIP] = 0; # how many samples to skip
45 17 $self->[MINIMUM] = 1; # the minimum number of trails to run
46 17 $self->[SKIPCOUNT] = {}; # trial skip storage
47 17 delete $self->[CONFIDENCE]; # confidence factor
48 17 delete $self->[ERROR]; # allowable error
49 17 delete $self->[STAT]; # stat objects for each tag
50  
51 17 ▶ if(exists $args{skip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"skip"}) 8 9 17
52 croak 'argument skip must be a non-negative integer'
53 unless defined $args{skip}
54 and $args{skip} !~ /\D/
55 8 ▶ and int $args{skip} == $args{skip};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"skip"} and not $args{"skip"} =~ /\D/ and int $args{"skip"} == $args{"skip"} 2 5 7
Condition83%5/6
defined $args{"skip"} and not $args{"skip"} =~ /\D/ and int $args{"skip"} == $args{"skip"}
!ll&&!rl&&r
305
defined $args{"skip"} and not $args{"skip"} =~ /\D/
!ll&&!rl&&r
125
MC/DC66%2/3
defined $args{"skip"} and not $args{"skip"} =~ /\D/ and int $args{"skip"} == $args{"skip"}
defined $args{"skip"} not $args{"skip"} =~ /\D/ int $args{"skip"} == $args{"skip"}
Truth table75%3/4
A and not(B) and C
A= defined $args{"skip"}B= not $args{"skip"} =~ /\D/C= int $args{"skip"} == $args{"skip"}
ABCresult
0XX0
10X0
1100
1111
56 5 $self->[SKIP] = $args{skip};
57 5 delete $args{skip};
58 }
59  
60 14 ▶ if(exists $args{minimum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"minimum"}) 0 14 14
61 croak 'argument minimum must be a non-negative integer'
62 unless defined $args{minimum}
63 and $args{minimum} !~ /\D/
64 0 ▶ and int $args{minimum} == $args{minimum};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"minimum"} and not $args{"minimum"} =~ /\D/ and int $args{"minimum"} == $args{"minimum"} 0 0 0
Condition0%0/6
defined $args{"minimum"} and not $args{"minimum"} =~ /\D/ and int $args{"minimum"} == $args{"minimum"}
!ll&&!rl&&r
000
defined $args{"minimum"} and not $args{"minimum"} =~ /\D/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args{"minimum"} and not $args{"minimum"} =~ /\D/ and int $args{"minimum"} == $args{"minimum"}
defined $args{"minimum"} not $args{"minimum"} =~ /\D/ int $args{"minimum"} == $args{"minimum"}
Truth table0%0/4
A and not(B) and C
A= defined $args{"minimum"}B= not $args{"minimum"} =~ /\D/C= int $args{"minimum"} == $args{"minimum"}
ABCresult
0XX0
10X0
1100
1111
65 croak 'argument minimum must greater than or equal to skip'
66 unless defined $args{minimum}
67 0 ▶ and $args{minimum} >= $self->[SKIP];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"minimum"} and $args{"minimum"} >= $self->[4] 0 0 0
Condition0%0/3
defined $args{"minimum"} and $args{"minimum"} >= $self->[4]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"minimum"} and $args{"minimum"} >= $self->[4]
defined $args{"minimum"} $args{"minimum"} >= $self->[4]
Truth table0%0/3
A and B
A= defined $args{"minimum"}B= $args{"minimum"} >= $self->[4]
ABresult
0X0
100
111
68 0 $self->[MINIMUM] = $args{minimum};
69 0 delete $args{minimum};
70 }
71  
72 my $confidence_is_valid =
73 (defined $args{confidence}
74 and $args{confidence} =~ /^\d*\.?\d*$/
75 and $args{confidence} > 0
76 14 ▶ and $args{confidence} < 100);
Condition33%3/9
defined $args{'confidence'} && $args{'confidence'} =~ /^\d*\.?\d*$/ && $args{'confidence'} > 0 && $args{'confidence'} < 100
!ll&&!rl&&r
1400
defined $args{"confidence"} and $args{"confidence"} =~ /^\d*\.?\d*$/ and $args{"confidence"} > 0
!ll&&!rl&&r
1400
defined $args{"confidence"} and $args{"confidence"} =~ /^\d*\.?\d*$/
!ll&&!rl&&r
1400
MC/DC0%0/4
defined $args{'confidence'} && $args{'confidence'} =~ /^\d*\.?\d*$/ && $args{'confidence'} > 0 && $args{'confidence'} < 100
defined $args{"confidence"} $args{"confidence"} =~ /^\d*\.?\d*$/ $args{"confidence"} > 0 $args{'confidence'} < 100
Truth table20%1/5
A and B and C && D
A= defined $args{"confidence"}B= $args{"confidence"} =~ /^\d*\.?\d*$/C= $args{"confidence"} > 0D= $args{'confidence'} < 100
ABCDresult
0XXX0
10XX0
110X0
11100
11111
77  
78 my $error_is_valid =
79 (defined $args{error}
80 and $args{error} =~ /^\d*\.?\d*$/
81 and $args{error} > 0
82 14 ▶ and $args{error} < 100);
Condition33%3/9
defined $args{'error'} && $args{'error'} =~ /^\d*\.?\d*$/ && $args{'error'} > 0 && $args{'error'} < 100
!ll&&!rl&&r
1400
defined $args{"error"} and $args{"error"} =~ /^\d*\.?\d*$/ and $args{"error"} > 0
!ll&&!rl&&r
1400
defined $args{"error"} and $args{"error"} =~ /^\d*\.?\d*$/
!ll&&!rl&&r
1400
MC/DC0%0/4
defined $args{'error'} && $args{'error'} =~ /^\d*\.?\d*$/ && $args{'error'} > 0 && $args{'error'} < 100
defined $args{"error"} $args{"error"} =~ /^\d*\.?\d*$/ $args{"error"} > 0 $args{'error'} < 100
Truth table20%1/5
A and B and C && D
A= defined $args{"error"}B= $args{"error"} =~ /^\d*\.?\d*$/C= $args{"error"} > 0D= $args{'error'} < 100
ABCDresult
0XXX0
10XX0
110X0
11100
11111
83  
84 14 ▶ if ($confidence_is_valid && !$error_is_valid ||
Branch: 2 branches
BranchTrueFalseTotal
if ($confidence_is_valid and not $error_is_valid or not $confidence_is_valid and $error_is_valid) { } 0 14 14
elsif ($confidence_is_valid and $error_is_valid) { } 0 28 28
Condition33%3/9
$confidence_is_valid and not $error_is_valid or not $confidence_is_valid and $error_is_valid
l!l&&r!l&&!r
0014
$confidence_is_valid and not $error_is_valid
!ll&&!rl&&r
1400
not $confidence_is_valid and $error_is_valid
!ll&&!rl&&r
0140
MC/DC0%0/4
$confidence_is_valid and not $error_is_valid or not $confidence_is_valid and $error_is_valid
$confidence_is_valid not $error_is_valid not $confidence_is_valid $error_is_valid
Truth table14%1/7
A and not(B) or not(C) and D
A= $confidence_is_validB= not $error_is_validC= not $confidence_is_validD= $error_is_valid
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
85 !$confidence_is_valid && $error_is_valid)
86 {
87 0 carp 'you must specify both confidence and error'
88 }
89 ▶ elsif ($confidence_is_valid && $error_is_valid)
Condition33%1/3
$confidence_is_valid and $error_is_valid
!ll&&!rl&&r
1400
MC/DC0%0/2
$confidence_is_valid and $error_is_valid
$confidence_is_valid $error_is_valid
Truth table33%1/3
A and B
A= $confidence_is_validB= $error_is_valid
ABresult
0X0
100
111
90 {
91 0 $self->[CONFIDENCE] = $args{confidence};
92 0 delete $args{confidence};
93  
94 0 $self->[ERROR] = $args{error};
95 0 delete $args{error};
96  
97 # Demand load the module we need. We could just
98 # require people to install it...
99 croak 'Could not load the Statistics::PointEstimation module'
100 0 ▶ unless eval {require Statistics::PointEstimation};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Statistics::PointEstimation } } 0 0 0
101 }
102  
103 14 ▶ if(%args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) 1 13 14
104 1 carp 'skipping unknown arguments';
105 }
106  
107 14 return $self;
108 }
109  
110  
111 # In this routine we try hard to make the [ gettimeofday ] take place
112 # as late as possible to minimize Heisenberg problems. :)
113  
114 sub start {
115 65 ▶ my $self = shift;
Subroutine start: called CC=6 SCAR=18.0
Pod documented
116 65 ▶ my $tag = shift || $self->[LASTTAG] || '_default';
Condition40%2/5
shift() || $self->[2] || '_default'
l!l
650
shift() or $self->[2]
l!l&&r!l&&!r
6500
MC/DC0%0/2
shift() || $self->[2] || '_default'
shift() $self->[2]
Truth table33%1/3
A or B
A= shift()B= $self->[2]
ABresult
1X1
011
000
117 65 $self->[LASTTAG] = $tag;
118 65 ▶ if(exists $self->[SKIPCOUNT]->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[6]{$tag}) { } 55 10 65
119 55 ▶ if($self->[SKIPCOUNT]->{$tag} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[6]{$tag} > 1) { } 26 29 55
120 26 $self->[SKIPCOUNT]->{$tag}--;
121 } else {
122 29 $self->[SKIPCOUNT]->{$tag} = 0;
123 29 push @{$self->[BEFORE]->{$tag}}, [ gettimeofday ];
124 }
125 } else {
126 10 push @{$self->[TAGS]}, $tag;
127 10 $self->[SKIPCOUNT]->{$tag} = $self->[SKIP] + 1;
128 10 ▶ if($self->[SKIPCOUNT]->{$tag} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[6]{$tag} > 1) { } 4 6 10
129 4 $self->[SKIPCOUNT]->{$tag}--;
130 } else {
131 6 $self->[SKIPCOUNT]->{$tag} = 0;
132 6 $self->[BEFORE]->{$tag} = [ [ gettimeofday ] ]
133 }
134 }
135 }
136  
137  
138 sub stop {
139 67 ▶ my $after = [ gettimeofday ]; # minimize overhead
Subroutine stop: called CC=11 SCAR=24.5
Pod documented
140 67 my $self = shift;
141 67 ▶ my $tag = shift || $self->[LASTTAG] || '_default';
Condition80%4/5
shift() || $self->[2] || '_default'
l!l
661
shift() or $self->[2]
l!l&&r!l&&!r
0661
MC/DC50%1/2
shift() || $self->[2] || '_default'
shift() $self->[2]
Truth table66%2/3
A or B
A= shift()B= $self->[2]
ABresult
1X1
011
000
142  
143 croak 'must call $t->start($tag) before $t->stop($tag)'
144 67 ▶ unless exists $self->[SKIPCOUNT]->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[6]{$tag} 1 66 67
145  
146 66 ▶ return if $self->[SKIPCOUNT]->{$tag} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[6]{$tag} > 0 30 36 66
147  
148 my $i = exists $self->[ELAPSED]->{$tag} ?
149 36 ▶ scalar @{$self->[ELAPSED]->{$tag}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[1]{$tag} ? : 28 8 36
150 36 my $before = $self->[BEFORE]->{$tag}->[$i];
151 36 ▶ croak 'timer out of sync' unless defined $before;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $before 1 35 36
152  
153 # Create a stats object if we need to
154 35 ▶ if (defined $self->[CONFIDENCE] && !defined $self->[STAT]->{$tag})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[7] and not defined $self->[9]{$tag}) 0 35 35
Condition33%1/3
defined $self->[7] and not defined $self->[9]{$tag}
!ll&&!rl&&r
3500
MC/DC0%0/2
defined $self->[7] and not defined $self->[9]{$tag}
defined $self->[7] not defined $self->[9]{$tag}
Truth table33%1/3
A and not(B)
A= defined $self->[7]B= not defined $self->[9]{$tag}
ABresult
0X0
100
111
155 {
156 0 $self->[STAT]->{$tag} = Statistics::PointEstimation->new;
157 0 $self->[STAT]->{$tag}->set_significance($self->[CONFIDENCE]);
158 }
159  
160 35 my $elapsed = tv_interval($before, $after);
161  
162 35 ▶ if($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) { } 27 8 35
163 27 push @{$self->[ELAPSED]->{$tag}}, $elapsed;
164 } else {
165 8 $self->[ELAPSED]->{$tag} = [ $elapsed ];
166 }
167  
168 $self->[STAT]->{$tag}->add_data($elapsed)
169 35 ▶ if defined $self->[STAT]->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[9]{$tag} 0 35 35
170  
171 35 return $elapsed;
172 }
173  
174  
175 sub need_more_samples {
176 0 ▶ my $self = shift;
Subroutine need_more_samples: not called CC=6 SCAR=37.4
Pod documented
177 0 ▶ my $tag = shift || $self->[LASTTAG] || '_default';
Condition0%0/5
shift() || $self->[2] || '_default'
l!l
00
shift() or $self->[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->[2] || '_default'
shift() $self->[2]
Truth table0%0/3
A or B
A= shift()B= $self->[2]
ABresult
1X1
011
000
178  
179 0 ▶ carp 'You must set the confidence and error in order to use need_more_samples'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[7] 0 0 0
180 unless defined $self->[CONFIDENCE];
181  
182 # In case this function is called before any trials are run
183 return 1
184 if !defined $self->[STAT]->{$tag} ||
185 0 ▶ $self->[STAT]->{$tag}->count < $self->[MINIMUM];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->[9]{$tag} or $self->[9]{$tag}->count < $self->[5] 0 0 0
Condition0%0/3
not defined $self->[9]{$tag} or $self->[9]{$tag}->count < $self->[5]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->[9]{$tag} or $self->[9]{$tag}->count < $self->[5]
not defined $self->[9]{$tag} $self->[9]{$tag}->count < $self->[5]
Truth table0%0/3
not(A) or B
A= not defined $self->[9]{$tag}B= $self->[9]{$tag}->count < $self->[5]
ABresult
1X1
011
000
186  
187 # For debugging
188 # printf STDERR "Average: %.5f +/- %.5f, Samples: %d\n",
189 # $self->[STAT]->{$tag}->mean(), $self->[STAT]->{$tag}->delta(),
190 # $self->[STAT]->{$tag}->count;
191 # printf STDERR "Percent Error: %.5f > %.5f\n",
192 # $self->[STAT]->{$tag}->delta() / $self->[STAT]->{$tag}->mean() * 100,
193 # $self->[ERROR];
194  
195 0 return (($self->[STAT]->{$tag}->delta() / $self->[STAT]->{$tag}->mean() * 100) >
196 $self->[ERROR]);
197 }
198  
199  
200 sub report {
201 9 ▶ my $self = shift;
Subroutine report: called CC=4 SCAR=15.9
Pod documented
202 9 ▶ my $tag = shift || $self->[LASTTAG] || '_default';
Condition60%3/5
shift() || $self->[2] || '_default'
l!l
90
shift() or $self->[2]
l!l&&r!l&&!r
630
MC/DC0%0/2
shift() || $self->[2] || '_default'
shift() $self->[2]
Truth table66%2/3
A or B
A= shift()B= $self->[2]
ABresult
1X1
011
000
203  
204 9 ▶ unless(exists $self->[ELAPSED]->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[1]{$tag}) 0 9 9
205 0 carp join ' ', 'tag', $tag, 'still running';
206 0 return;
207 }
208  
209 9 return $self->_report($tag);
210 }
211  
212  
213  
214 sub reports {
215 4 ▶ my $self = shift;
Subroutine reports: called CC=4 SCAR=13.9
Pod documented
216  
217 4 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 2 4
218 {
219 2 my @reports;
220  
221 2 foreach my $tag (@{$self->[TAGS]}) {
222 3 push @reports, $tag;
223 3 push @reports, $self->report($tag);
224 }
225  
226 2 return @reports;
227 }
228 else
229 {
230 2 my $report = '';
231  
232 2 foreach my $tag (@{$self->[TAGS]}) {
233 3 $report .= $self->report($tag);
234 }
235  
236 2 return $report;
237 }
238 }
239  
240  
241 sub _report {
242 9 ▶ my $self = shift;
Subroutine _report: called CC=6 SCAR=19.5
243 9 my $tag = shift;
244  
245 9 ▶ unless(exists $self->[ELAPSED]->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[1]{$tag}) 0 9 9
246 0 return "Tag $tag is still running or has not completed its skipped runs, skipping\n";
247 }
248  
249 9 my $report = '';
250  
251 9 my @times = @{$self->[ELAPSED]->{$tag}};
252 9 my $n = scalar @times;
253 9 my $total = 0; $total += $_ foreach @times;
254  
255 9 ▶ if ($n == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) { } 4 5 9
256 {
257 4 $report .= sprintf "\%d trial of \%s (\%s total)\n",
258 $n, $tag, _timestr($total);
259 }
260 else
261 {
262 5 $report .= sprintf "\%d trials of \%s (\%s total), \%s/trial\n",
263 $n, $tag, _timestr($total), _timestr($total / $n);
264 }
265  
266 9 ▶ if (defined $self->[STAT]->{$tag})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[9]{$tag}) 0 9 9
267 {
268 0 my $delta = 0;
269 $delta = $self->[STAT]->{$tag}->delta()
270 0 ▶ if defined $self->[STAT]->{$tag}->delta();
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[9]{$tag}->delta 0 0 0
271
272 0 $report .= sprintf "Error: +/- \%.5f with \%s confidence\n",
273 $delta, $self->[CONFIDENCE];
274 }
275  
276 9 return $report;
277 }
278  
279  
280  
281 sub result {
282 3 ▶ my $self = shift;
Subroutine result: called CC=5 SCAR=18.1
Pod documented
283 3 ▶ my $tag = shift || $self->[LASTTAG] || '_default';
Condition40%2/5
shift() || $self->[2] || '_default'
l!l
30
shift() or $self->[2]
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || $self->[2] || '_default'
shift() $self->[2]
Truth table33%1/3
A or B
A= shift()B= $self->[2]
ABresult
1X1
011
000
284 3 ▶ unless(exists $self->[ELAPSED]->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[1]{$tag}) 0 3 3
285 0 carp join ' ', 'tag', $tag, 'still running';
286 0 return;
287 }
288 3 my @times = @{$self->[ELAPSED]->{$tag}};
289 3 my $total = 0; $total += $_ foreach @times;
290 3 return $total / @times;
291 }
292  
293  
294 sub results {
295 2 ▶ my $self = shift;
Subroutine results: called CC=3 SCAR=11.0
Pod documented
296 2 my @results;
297 2 foreach my $tag (@{$self->[TAGS]}) {
298 2 push @results, $tag;
299 2 push @results, $self->result($tag);
300 }
301 2 ▶ return wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
302 }
303  
304  
305  
306 sub data {
307 10 ▶ my $self = shift;
Subroutine data: called CC=5 SCAR=16.1
Pod documented
308 10 my $tag = shift;
309 10 my @results;
310 10 ▶ if($tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag) { } 8 2 10
311 8 ▶ if(exists $self->[ELAPSED]->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[1]{$tag}) { } 6 2 8
312 6 @results = @{$self->[ELAPSED]->{$tag}};
313 } else {
314 2 @results = ();
315 }
316 } else {
317 2 ▶ @results = map { ( $_ => $self->[ELAPSED]->{$_} || [] ) }
Branch: 1 branch
BranchTrueFalseTotal
$self->[1]{$_} or [] 0 2 2
318 2 @{$self->[TAGS]};
319 }
320 10 ▶ return wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 2 10
321 }
322  
323  
324 # ------------------------------------------------------------------------
325 # Internal utility subroutines
326  
327 # _timestr($sec) takes a floating-point number of seconds and formats
328 # it in a sensible way, commifying large numbers of seconds, and
329 # converting to milliseconds if it makes sense. Since Time::HiRes has
330 # at most microsecond resolution, no attempt is made to convert into
331 # anything below that. A unit string is appended to the number.
332  
333 sub _timestr {
334 14 ▶ my $sec = shift;
Subroutine _timestr: called CC=7 SCAR=28.4
335 14 my $retstr;
336 14 ▶ if($sec >= 1_000) {
Branch: 4 branches
BranchTrueFalseTotal
if ($sec >= 1000) { } 0 14 14
elsif ($sec >= 1) { } 0 14 14
elsif ($sec >= 0.001) { } 0 14 14
elsif ($sec >= 1e-06) { } 14 0 14
337 0 $retstr = _commify(int $sec) . 's';
338 } elsif($sec >= 1) {
339 0 ▶ $retstr = sprintf $sec == int $sec ? '%ds' : '%0.3fs', $sec;
Branch: 1 branch
BranchTrueFalseTotal
$sec == int $sec ? : 0 0 0
340 } elsif($sec >= 0.001) {
341 0 my $ms = $sec * 1_000;
342 0 ▶ $retstr = sprintf $ms == int $ms ? '%dms' : '%0.3fms', $ms;
Branch: 1 branch
BranchTrueFalseTotal
$ms == int $ms ? : 0 0 0
343 } elsif($sec >= 0.000001) {
344 14 $retstr = sprintf '%dus', $sec * 1_000_000;
345 } else {
346 # I'll have whatever real-time OS she's having
347 0 $retstr = $sec . 's';
348 }
349 14 $retstr;
350 }
351  
352  
353 # _commify($num) inserts a grouping comma according to en-US standards
354 # for numbers larger than 1000. For example, the integer 123456 would
355 # be written 123,456. Any fractional part is left untouched.
356  
357 sub _commify {
358 0 ▶ my $num = shift;
Subroutine _commify: not called CC=5 SCAR=34.0
359 0 ▶ return unless $num =~ /\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num =~ /\d/ 0 0 0
360 0 ▶ return $num if $num < 1_000;
Branch: 1 branch
BranchTrueFalseTotal
if $num < 1000 0 0 0
361  
362 0 my $ip = int $num;
363 0 my($fp) = ($num =~ /\.(\d+)/);
364  
365 0 $ip =~ s/(\d\d\d)$/,$1/;
366 0 ▶ 1 while $ip =~ s/(\d)(\d\d\d),/$1,$2,/;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ s/(\d)(\d\d\d),/$1,$2,/ and do { '???' } 0 0 0
367  
368 0 ▶ return $fp ? join '.', $ip, $fp : $ip;
Branch: 1 branch
BranchTrueFalseTotal
$fp ? : 0 0 0
369 }
370  
371 # ------------------------------------------------------------------------
372 # Return true for a valid Perl include
373  
374 6 1;
375  
376 # ---------------------------------------------------------------------------
377  
378 =head1 NAME
379  
380 Benchmark::Timer - Benchmarking with statistical confidence
381  
382  
383 =head1 SYNOPSIS
384  
385 # Non-statistical usage
386 use Benchmark::Timer;
387 $t = Benchmark::Timer->new(skip => 1);
388  
389 for(1 .. 1000) {
390 $t->start('tag');
391 &long_running_operation();
392 $t->stop('tag');
393 }
394 print $t->report;
395  
396 # --------------------------------------------------------------------
397  
398 # Statistical usage
399 use Benchmark::Timer;
400 $t = Benchmark::Timer->new(skip => 1, confidence => 97.5, error => 2);
401  
402 while($t->need_more_samples('tag')) {
403 $t->start('tag');
404 &long_running_operation();
405 $t->stop('tag');
406 }
407 print $t->report;
408  
409 =head1 DESCRIPTION
410  
411 The Benchmark::Timer class allows you to time portions of code
412 conveniently, as well as benchmark code by allowing timings of repeated
413 trials. It is perfect for when you need more precise information about the
414 running time of portions of your code than the Benchmark module will give
415 you, but don't want to go all out and profile your code.
416  
417 The methodology is simple; create a Benchmark::Timer object, and wrap portions
418 of code that you want to benchmark with C<start()> and C<stop()> method calls.
419 You can supply a tag to those methods if you plan to time multiple portions of
420 code. If you provide error and confidence values, you can also use
421 C<need_more_samples()> to determine, statistically, whether you need to
422 collect more data.
423  
424 After you have run your code, you can obtain information about the running
425 time by calling the C<results()> method, or get a descriptive benchmark report
426 by calling C<report()>. If you run your code over multiple trials, the
427 average time is reported. This is wonderful for benchmarking time-critical
428 portions of code in a rigorous way. You can also optionally choose to skip any
429 number of initial trials to cut down on initial case irregularities.
430  
431 =head1 METHODS
432  
433 In all of the following methods, C<$tag> refers to the user-supplied name of
434 the code being timed. Unless otherwise specified, $tag defaults to the tag of
435 the last call to C<start()>, or "_default" if C<start()> was not previously
436 called with a tag.
437  
438 =over 4
439  
440 =item $t = Benchmark::Timer->new( [options] );
441  
442 Constructor for the Benchmark::Timer object; returns a reference to a
443 timer object. Takes the following named arguments:
444  
445 =over 4
446  
447 =item skip
448  
449 The number of trials (if any) to skip before recording timing information.
450  
451 =item minimum
452  
453 The minimum number of trials to run.
454  
455 =item error
456  
457 A percentage between 0 and 100 which indicates how much error you are willing
458 to tolerate in the average time measured by the benchmark. For example, a
459 value of 1 means that you want the reported average time to be within 1% of
460 the real average time. C<need_more_samples()> will use this value to determine
461 when it is okay to stop collecting data.
462  
463 If you specify an error you must also specify a confidence.
464  
465 =item confidence
466  
467 A percentage between 0 and 100 which indicates how confident you want to be in
468 the error measured by the benchmark. For example, a value of 97.5 means that
469 you want to be 97.5% confident that the real average time is within the error
470 margin you have specified. C<need_more_samples()> will use this value to
471 compute the estimated error for the collected data, so that it can determine
472 when it is okay to stop.
473  
474 If you specify a confidence you must also specify an error.
475  
476 =back
477  
478 =item $t->reset;
479  
480 Reset the timer object to the pristine state it started in.
481 Erase all memory of tags and any previously accumulated timings.
482 Returns a reference to the timer object. It takes the same arguments
483 the constructor takes.
484  
485 =item $t->start($tag);
486  
487 Record the current time so that when C<stop()> is called, we can calculate an
488 elapsed time.
489  
490 =item $t->stop($tag);
491  
492 Record timing information. If $tag is supplied, it must correspond to one
493 given to a previously called C<start()> call. It returns the elapsed time in
494 milliseconds. C<stop()> croaks if the timer gets out of sync (e.g. the number
495 of C<start()>s does not match the number of C<stop()>s.)
496  
497 =item $t->need_more_samples($tag);
498  
499 Compute the estimated error in the average of the data collected thus far, and
500 return true if that error exceeds the user-specified error. If a $tag is
501 supplied, it must correspond to one given to a previously called C<start()>
502 call.
503  
504 This routine assumes that the data are normally distributed.
505  
506 =item $t->report($tag);
507  
508 Returns a string containing a simple report on the collected timings for $tag.
509 This report contains the number of trials run, the total time taken, and, if
510 more than one trial was run, the average time needed to run one trial and
511 error information. C<report()> will complain (via a warning) if a tag is
512 still active.
513  
514 =item $t->reports;
515  
516 In a scalar context, returns a string containing a simple report on the
517 collected timings for all tags. The report is a concatenation of the
518 individual tag reports, in the original tag order. In an list context, returns
519 a hash keyed by tag and containing reports for each tag. The return value is
520 actually an array, so that the original tag order is preserved if you assign
521 to an array instead of a hash. C<reports()> will complain (via a warning) if a
522 tag is still active.
523  
524 =item $t->result($tag);
525  
526 Return the time it took for $tag to elapse, or the mean time it took for $tag
527 to elapse once, if $tag was used to time code more than once. C<result()> will
528 complain (via a warning) if a tag is still active.
529  
530 =item $t->results;
531  
532 Returns the timing data as a hash keyed on tags where each value is
533 the time it took to run that code, or the average time it took,
534 if that code ran more than once. In scalar context it returns a reference
535 to that hash. The return value is actually an array, so that the original
536 tag order is preserved if you assign to an array instead of a hash.
537  
538 =item $t->data($tag), $t->data;
539  
540 These methods are useful if you want to recover the full internal timing
541 data to roll your own reports.
542  
543 If called with a $tag, returns the raw timing data for that $tag as
544 an array (or a reference to an array if called in scalar context). This is
545 useful for feeding to something like the Statistics::Descriptive package.
546  
547 If called with no arguments, returns the raw timing data as a hash keyed
548 on tags, where the values of the hash are lists of timings for that
549 code. In scalar context, it returns a reference to that hash. As with
550 C<results()>, the data is internally represented as an array so you can
551 recover the original tag order by assigning to an array instead of a hash.
552  
553 =back
554  
555 =head1 BUGS
556  
557 Benchmarking is an inherently futile activity, fraught with uncertainty
558 not dissimilar to that experienced in quantum mechanics. But things are a
559 little better if you apply statistics.
560  
561 =head1 LICENSE
562  
563 This code is distributed under the GNU General Public License (GPL) Version 2.
564 See the file LICENSE in the distribution for details.
565  
566 =head1 AUTHOR
567  
568 The original code (written before April 20, 2001) was written by Andrew Ho
569 E<lt>andrew@zeuscat.comE<gt>, and is copyright (c) 2000-2001 Andrew Ho.
570 Versions up to 0.5 are distributed under the same terms as Perl.
571  
572 Maintenance of this module is now being done by David Coppit
573 E<lt>david@coppit.orgE<gt>.
574  
575 =head1 SEE ALSO
576  
577 L<Benchmark>, L<Time::HiRes>, L<Time::Stopwatch>, L<Statistics::Descriptive>
578  
579 =cut
↑ Summary delta.pl »