| 1 | package Mojo::Util::Benchmark; | ||||||||||||||||||||
| 2 | 13 | ▶ | use Mojo::Base -base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 13 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 13 | ▶ | use Time::HiRes qw(gettimeofday); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 13 | our @EXPORT_OK = qw( | |||||||||||||||||||
| 8 | benchmark | ||||||||||||||||||||
| 9 | ); | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 13 | our $VERSION = '0.0.1'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 13 | has 'digits' => 12; # nanoseconds | |||||||||||||||||||
| 14 | 13 | has 'lastTimer'; | |||||||||||||||||||
| 15 | 13 | has 'output' => 0; | |||||||||||||||||||
| 16 | 12 | ▶ | has 'timers' => sub { {} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head2 average | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Run a callback a number of times and return the average time. | ||||||||||||||||||||
| 21 | If no iterations are given, the default of 10 will be used. | ||||||||||||||||||||
| 22 | If no digits are given, the default of 12 will be used. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | Returns the average time. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 3 | ▶ | sub average { | ||||||||||||||||||
|
Subroutine
average: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 29 | 3 | my ($self, $callback, $iterations, $digits) = @_; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 3 | ▶ | $digits ||= $self->digits; | ||||||||||||||||||
|
Condition33%1/3
$digits ||= $self->digits
MC/DC0%0/2
$digits ||= $self->digits
$digits
$self->digits
Truth table33%1/3
A ||= B
A= $digitsB= $self->digits
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 3 | my @series = $self->series($callback, $iterations, $digits); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 3 | my $sum = 0; | |||||||||||||||||||
| 36 | 3 | $sum += $_ for @series; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 3 | return sprintf("%0.${digits}f", $sum / @series); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head2 benchmark | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | Create a new benchmark object. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 4 | ▶ | sub benchmark { | ||||||||||||||||||
|
Subroutine
benchmark: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 48 | 4 | return Mojo::Util::Benchmark->new(@_); | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head2 microseconds | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | Set the number of digits to 8. | ||||||||||||||||||||
| 54 | Returns the object. | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 2 | ▶ | sub microseconds { | ||||||||||||||||||
|
Subroutine
microseconds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 59 | 2 | my $self = shift; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 2 | $self->digits(8); | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 2 | return $self; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =head2 milliseconds | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Set the number of digits to 4. | ||||||||||||||||||||
| 69 | Returns the object. | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | ▶ | sub milliseconds { | ||||||||||||||||||
|
Subroutine
milliseconds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 74 | 1 | my $self = shift; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | $self->digits(4); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | return $self; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head2 nanoseconds | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | Set the number of digits to 12. | ||||||||||||||||||||
| 84 | Returns the object. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 1 | ▶ | sub nanoseconds { | ||||||||||||||||||
|
Subroutine
nanoseconds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 89 | 1 | my $self = shift; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 1 | $self->digits(12); | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 1 | return $self; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head2 print | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | Print the elapsed time for a timer. | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 2 | ▶ | sub print { | ||||||||||||||||||
|
Subroutine
print: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||
| 103 | 2 | my ($self, $name, $digits) = @_; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 2 | ▶ | $digits ||= $self->timers->{$name}->{digits}; | ||||||||||||||||||
|
Condition33%1/3
$digits ||= $self->timers->{$name}{"digits"}
MC/DC0%0/2
$digits ||= $self->timers->{$name}{"digits"}
$digits
$self->timers->{$name}{"digits"}
Truth table33%1/3
A ||= B
A= $digitsB= $self->timers->{$name}{"digits"}
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 2 | my $duration = $self->timers->{$name}->{stop} - $self->timers->{$name}->{start}; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 2 | printf("%s: %0.${digits}f\n", $name, $duration); | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head2 seconds | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Set the number of digits to 0. | ||||||||||||||||||||
| 115 | Returns the object. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 1 | ▶ | sub seconds { | ||||||||||||||||||
|
Subroutine
seconds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 120 | 1 | my $self = shift; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 1 | $self->digits(0); | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 1 | return $self; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =head2 series | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | Run a callback a number of times and return the elapsed times. | ||||||||||||||||||||
| 130 | If no iterations are given, the default of 10 will be used. | ||||||||||||||||||||
| 131 | If no digits are given, the default of 12 will be used. | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Returns an array ref of elapsed times. | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 5 | ▶ | sub series { | ||||||||||||||||||
|
Subroutine
series: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 138 | 5 | my ($self, $callback, $iterations, $digits) = @_; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 5 | ▶ | $iterations ||= 10; | ||||||||||||||||||
|
Condition50%1/2
$iterations ||= 10
MC/DC0%0/1
$iterations ||= 10
$iterations
Truth table50%1/2
A
A= $iterations
| |||||||||||||||||||||
| 141 | 5 | ▶ | $digits ||= $self->digits; | ||||||||||||||||||
|
Condition66%2/3
$digits ||= $self->digits
MC/DC0%0/2
$digits ||= $self->digits
$digits
$self->digits
Truth table66%2/3
A ||= B
A= $digitsB= $self->digits
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 5 | my @series; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 5 | for (1 .. $iterations) { | |||||||||||||||||||
| 146 | 325 | $self->start("Iteration $_"); | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 325 | $callback->(); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 325 | push @series, sprintf("%0.${digits}f", $self->stop("Iteration $_")); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 5 | ▶ | return wantarray ? @series : \@series; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =head2 start | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | Start a timer. | ||||||||||||||||||||
| 159 | If no name is given, the timer will be named 'elapsed'. | ||||||||||||||||||||
| 160 | If no digits are given, the default of 4 will be used. | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Returns the object. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 333 | ▶ | sub start { | ||||||||||||||||||
|
Subroutine
start: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 167 | 333 | my ($self, $name, $digits) = @_; | |||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 333 | ▶ | $name ||= 'elapsed'; | ||||||||||||||||||
|
Condition50%1/2
$name ||= "elapsed"
MC/DC0%0/1
$name ||= "elapsed"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||
| 170 | 333 | ▶ | $digits ||= $self->digits; | ||||||||||||||||||
|
Condition100%3/3
$digits ||= $self->digits
MC/DC100%2/2
$digits ||= $self->digits
$digits
$self->digits
Truth table100%3/3
A ||= B
A= $digitsB= $self->digits
| |||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 333 | $self->lastTimer($name); | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 333 | my $start = gettimeofday(); | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 333 | $self->timers->{$self->lastTimer} = { | |||||||||||||||||||
| 177 | start => $start, | ||||||||||||||||||||
| 178 | digits => $digits, | ||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 333 | return $self; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =head2 stop | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Stop a timer. | ||||||||||||||||||||
| 187 | If no name is given, the last timer started will be stopped. | ||||||||||||||||||||
| 188 | If no digits are given, the default of 5 will be used. | ||||||||||||||||||||
| 189 | If output is set, the elapsed time will be printed to STDOUT. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | Returns the elapsed time. | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 333 | ▶ | sub stop { | ||||||||||||||||||
|
Subroutine
stop: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||
| 196 | 333 | my ($self, $name, $digits) = @_; | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 333 | ▶ | $name ||= $self->lastTimer; | ||||||||||||||||||
|
Condition33%1/3
$name ||= $self->lastTimer
MC/DC0%0/2
$name ||= $self->lastTimer
$name
$self->lastTimer
Truth table33%1/3
A ||= B
A= $nameB= $self->lastTimer
| |||||||||||||||||||||
| 199 | 333 | ▶ | $digits ||= $self->timers->{$self->lastTimer}->{digits}; | ||||||||||||||||||
|
Condition66%2/3
$digits ||= $self->timers->{$self->lastTimer}{"digits"}
MC/DC50%1/2
$digits ||= $self->timers->{$self->lastTimer}{"digits"}
$digits
$self->timers->{$self->lastTimer}{"digits"}
Truth table66%2/3
A ||= B
A= $digitsB= $self->timers->{$self->lastTimer}{"digits"}
| |||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 333 | my $end = gettimeofday(); | |||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 333 | $self->timers->{$name}->{ stop } = $end; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 333 | my $duration = $self->timers->{$name}->{stop} - $self->timers->{$name}->{start}; | |||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 333 | ▶ | if ($self->output) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 208 | 2 | $self->print($name, $digits); | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 333 | return $duration; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 13 | 1; | |||||||||||||||||||