| 1 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | package Devel::Timer; | ||||||||||||
| 3 | 3 | $Devel::Timer::VERSION = '0.14'; | |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 3 | ▶ | use Time::HiRes(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | ## no critic (ProhibitAccessOfPrivateData ) | ||||||||||||
| 10 | |||||||||||||
| 11 | ## | ||||||||||||
| 12 | ## instantiate (and initialize) timer object | ||||||||||||
| 13 | ## | ||||||||||||
| 14 | |||||||||||||
| 15 | 6 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 16 | 6 | my ($class) = @_; | |||||||||||
| 17 | 6 | my $self = {}; | |||||||||||
| 18 | |||||||||||||
| 19 | 6 | bless $self, $class; | |||||||||||
| 20 | |||||||||||||
| 21 | 6 | $self->_self(); | |||||||||||
| 22 | |||||||||||||
| 23 | 6 | $self->initialize(); | |||||||||||
| 24 | |||||||||||||
| 25 | 6 | $self->mark('INIT'); | |||||||||||
| 26 | |||||||||||||
| 27 | 6 | return $self; | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | 7 | ▶ | sub _self { | ||||||||||
|
Subroutine
_self: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 7 | my ($self) = @_; | |||||||||||
| 32 | |||||||||||||
| 33 | 7 | %{$self} = ( | |||||||||||
| 34 | times => [], | ||||||||||||
| 35 | count => 0, | ||||||||||||
| 36 | label => {}, ## index:label | ||||||||||||
| 37 | ); | ||||||||||||
| 38 | |||||||||||||
| 39 | 7 | return $self; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | ## | ||||||||||||
| 43 | ## mark time (w/ optional label) | ||||||||||||
| 44 | ## | ||||||||||||
| 45 | |||||||||||||
| 46 | 51 | ▶ | sub mark { | ||||||||||
|
Subroutine
mark: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 47 | 51 | my($self, $label) = @_; | |||||||||||
| 48 | |||||||||||||
| 49 | 51 | ▶ | $label = '' if (!defined($label)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | |||||||||||||
| 51 | 51 | my $t = [ Time::HiRes::gettimeofday() ]; | |||||||||||
| 52 | |||||||||||||
| 53 | 51 | my $last_time; | |||||||||||
| 54 | 51 | ▶ | if ($self->{count} == 0) { ## first time has no last time | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 7 | $last_time = $t; | |||||||||||
| 56 | } | ||||||||||||
| 57 | else { | ||||||||||||
| 58 | 44 | $last_time = $self->{times}->[($self->{count}-1)]; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | ## save time for final report | ||||||||||||
| 62 | |||||||||||||
| 63 | 51 | push(@{$self->{times}}, $t); | |||||||||||
| 64 | |||||||||||||
| 65 | ## save time interval | ||||||||||||
| 66 | |||||||||||||
| 67 | my $interval = { | ||||||||||||
| 68 | value => Time::HiRes::tv_interval($last_time, $t), | ||||||||||||
| 69 | index => $self->{count}, | ||||||||||||
| 70 | 51 | }; | |||||||||||
| 71 | 51 | push(@{$self->{intervals}}, $interval); | |||||||||||
| 72 | |||||||||||||
| 73 | ## save label in separate hash for fast lookup | ||||||||||||
| 74 | |||||||||||||
| 75 | 51 | $self->{label}->{$self->{count}} = $label; | |||||||||||
| 76 | |||||||||||||
| 77 | 51 | $self->{count}++; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | ## | ||||||||||||
| 81 | ## reset all accumulated data | ||||||||||||
| 82 | ## | ||||||||||||
| 83 | |||||||||||||
| 84 | 1 | ▶ | sub reset { | ||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 85 | 1 | my ($self) = @_; | |||||||||||
| 86 | |||||||||||||
| 87 | 1 | return $self->_self; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | |||||||||||||
| 91 | ## | ||||||||||||
| 92 | ## output report to error log | ||||||||||||
| 93 | ## | ||||||||||||
| 94 | |||||||||||||
| 95 | 5 | ▶ | sub report { | ||||||||||
|
Subroutine
report: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 96 | 5 | my ($self, %args) = @_; | |||||||||||
| 97 | |||||||||||||
| 98 | ## calculate total time (start time vs last time) | ||||||||||||
| 99 | |||||||||||||
| 100 | 5 | my $total_time = Time::HiRes::tv_interval($self->{times}->[0], $self->{times}->[$self->{count}-1]); | |||||||||||
| 101 | |||||||||||||
| 102 | 5 | $self->print("\n"); | |||||||||||
| 103 | 5 | $self->print(ref($self) . ' Report -- Total time: ' . sprintf('%.4f', $total_time) . ' secs'); | |||||||||||
| 104 | 5 | ▶ | if ($args{collapse}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | 2 | $self->_calculate_collapsed; | |||||||||||
| 106 | |||||||||||||
| 107 | 2 | $self->print('Count Time Percent'); | |||||||||||
| 108 | 2 | $self->print('----------------------------------------------'); | |||||||||||
| 109 | |||||||||||||
| 110 | 2 | my $c = $self->{collapsed}; | |||||||||||
| 111 | 2 | ▶ | my $sort_by = $args{sort_by} || 'time'; | ||||||||||
|
Condition100%2/2
$args{'sort_by'} || 'time'
MC/DC100%1/1
$args{'sort_by'} || 'time'
$args{'sort_by'}
Truth table100%2/2
A
A= $args{'sort_by'}
| |||||||||||||
| 112 | 2 | my @labels = sort { $c->{$b}->{$sort_by} <=> $c->{$a}->{$sort_by} } keys %$c; | |||||||||||
| 113 | 2 | foreach my $label (@labels) { | |||||||||||
| 114 | 8 | my $count = $c->{$label}->{count}; | |||||||||||
| 115 | 8 | my $time = $c->{$label}->{time}; | |||||||||||
| 116 | 8 | my $msg = sprintf('%8s %.4f %5.2f%% %s', | |||||||||||
| 117 | ($count, $time, (($time/$total_time)*100), $label)); | ||||||||||||
| 118 | 8 | $self->print($msg); | |||||||||||
| 119 | } | ||||||||||||
| 120 | 2 | return 1; | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | |||||||||||||
| 124 | 3 | $self->print('Interval Time Percent'); | |||||||||||
| 125 | 3 | $self->print('----------------------------------------------'); | |||||||||||
| 126 | |||||||||||||
| 127 | ## sort interval structure based on value | ||||||||||||
| 128 | |||||||||||||
| 129 | 3 | @{$self->{intervals}} = sort { $b->{value} <=> $a->{value} } @{$self->{intervals}}; | |||||||||||
| 130 | |||||||||||||
| 131 | ## | ||||||||||||
| 132 | ## report of each time space between marks | ||||||||||||
| 133 | ## | ||||||||||||
| 134 | |||||||||||||
| 135 | 3 | for my $i (@{$self->{intervals}}) | |||||||||||
| 136 | { | ||||||||||||
| 137 | ## skip first time (to make an interval, | ||||||||||||
| 138 | ## compare the current time with the previous one) | ||||||||||||
| 139 | |||||||||||||
| 140 | 19 | ▶ | next if ($i->{index} == 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | |||||||||||||
| 142 | my $msg = sprintf('%02d -> %02d %.4f %5.2f%% %s -> %s', | ||||||||||||
| 143 | ($i->{index}-1), $i->{index}, $i->{value}, (($i->{value}/$total_time)*100), | ||||||||||||
| 144 | 16 | $self->{label}->{($i->{index}-1)}, $self->{label}->{$i->{index}}); | |||||||||||
| 145 | |||||||||||||
| 146 | 16 | $self->print($msg); | |||||||||||
| 147 | } | ||||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | 6 | ▶ | sub _calculate_collapsed { | ||||||||||
|
Subroutine
_calculate_collapsed: called CC=2 SCAR=6.9
| |||||||||||||
| 151 | 6 | my ($self) = @_; | |||||||||||
| 152 | |||||||||||||
| 153 | 6 | my %collapsed; | |||||||||||
| 154 | 6 | foreach my $i (0 .. $self->{count} - 2) { | |||||||||||
| 155 | 42 | my $label = $self->{label}->{$i} . ' -> ' . $self->{label}->{$i + 1}; | |||||||||||
| 156 | 42 | my $time = Time::HiRes::tv_interval($self->{times}->[$i], $self->{times}->[$i + 1]); | |||||||||||
| 157 | 42 | $collapsed{$label}{time} += $time; | |||||||||||
| 158 | 42 | $collapsed{$label}{count}++; | |||||||||||
| 159 | } | ||||||||||||
| 160 | 6 | $self->{collapsed} = \%collapsed; | |||||||||||
| 161 | } | ||||||||||||
| 162 | |||||||||||||
| 163 | 4 | ▶ | sub get_stats { | ||||||||||
|
Subroutine
get_stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 164 | 4 | my ($self, $a, $b) = @_; | |||||||||||
| 165 | 4 | $self->_calculate_collapsed; | |||||||||||
| 166 | 4 | my $collapsed = $self->{collapsed}; | |||||||||||
| 167 | 4 | my $total_time = Time::HiRes::tv_interval($self->{times}->[0], $self->{times}->[$self->{count}-1]); | |||||||||||
| 168 | 4 | my $label = "$a -> $b"; | |||||||||||
| 169 | 4 | my $time = $collapsed->{$label}->{time}; | |||||||||||
| 170 | 4 | my $count = $collapsed->{$label}->{count}; | |||||||||||
| 171 | 4 | return ($time, $time / $total_time * 100, $count); | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | |||||||||||||
| 175 | |||||||||||||
| 176 | ## output methods | ||||||||||||
| 177 | ## note: if you want to send output to somewhere other than stderr, | ||||||||||||
| 178 | ## you can override the print() method below. The initialize() | ||||||||||||
| 179 | ## and shutdown() methods are provided in case you need to open a file | ||||||||||||
| 180 | ## or connect to a database before printing the report. | ||||||||||||
| 181 | ## See pod for an example. | ||||||||||||
| 182 | |||||||||||||
| 183 | 5 | ▶ | sub initialize { | ||||||||||
|
Subroutine
initialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | 37 | ▶ | sub print { | ||||||||||
|
Subroutine
print: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 187 | 37 | my($self, $msg) = @_; | |||||||||||
| 188 | 37 | print STDERR $msg . "\n"; | |||||||||||
| 189 | } | ||||||||||||
| 190 | |||||||||||||
| 191 | 5 | ▶ | sub shutdown { | ||||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | 6 | ▶ | sub DESTROY { | ||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||
| 195 | 6 | my $self = shift; | |||||||||||
| 196 | 6 | $self->shutdown(); | |||||||||||
| 197 | } | ||||||||||||
| 198 | |||||||||||||
| 199 | 3 | 1; | |||||||||||
| 200 | |||||||||||||
| 201 | __END__ | ||||||||||||