| 1 | # Weather::GHCN::TimingStats.pm - class for capturing performance timing statistics | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # To Do: | ||||||||||||||||||||||||||||
| 4 | # - prevent stop without matching start | ||||||||||||||||||||||||||||
| 5 | # - prevent start after start | ||||||||||||||||||||||||||||
| 6 | # - add reset($timer) method | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | ## no critic (Documentation::RequirePodAtEnd) | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Weather::GHCN::TimingStats - collect timing statistics for GHCN modules and scripts | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | version v0.0.002 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Weather::GHCN::TimingStats qw(:all); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | The B<Weather::GHCN::TimingStats> module provides a class and methods that are | ||||||||||||||||||||||||||||
| 26 | used to collect timing statistics from within GHCN modules or from | ||||||||||||||||||||||||||||
| 27 | application scripts that use GHCN modules. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | The module is primarily for use by module Weather::GHCN::StationTable. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | use v5.18; # minimum for Object::Pad | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | use Object::Pad 0.66 qw( :experimental(init_expr) ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | package Weather::GHCN::TimingStats; | ||||||||||||||||||||||||||||
| 37 | class Weather::GHCN::TimingStats; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | our $VERSION = 'v0.0.002'; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use Const::Fast; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | const my $EMPTY => q(); # empty string | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | field $timer_href { {} }; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 new () | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Create a new TimingStats object. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head2 start($timer) | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Start a timer labelled $timer. Timer labels prefixed with underscore | ||||||||||||||||||||||||||||
| 58 | (_) are considered to be internal and not included in the overall | ||||||||||||||||||||||||||||
| 59 | duration. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 7 | ▶ | method start ($timer) { | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 7 | $timer_href->{$timer}->{START} = [Time::HiRes::gettimeofday]; | |||||||||||||||||||||||||||
| 65 | 7 | return; | |||||||||||||||||||||||||||
| 66 | 4 | } | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 stop ($timer, $note='') | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Stop the timer labelled $timer, with an optional note. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | ▶ | method stop ($timer, $note=$EMPTY) { | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 6 | $timer_href->{$timer}->{DUR} += Time::HiRes::tv_interval($timer_href->{$timer}->{START},[Time::HiRes::gettimeofday]); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | $timer_href->{$timer}->{NOTE} = $note | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | if $note; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 6 | return; | |||||||||||||||||||||||||||
| 81 | 4 | } | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 get_timers () | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Get a sorted list of all the timer labels that have been created so | ||||||||||||||||||||||||||||
| 86 | far by invoking the start() method. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | method get_timers () { | ||||||||||||||||||||||||||
|
Subroutine
get_timers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 1 | return (sort keys $timer_href->%*); | |||||||||||||||||||||||||||
| 92 | 4 | } | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 get_duration($timer) | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Get the time that has elapsed for the timer labelled $timer. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 4 | ▶ | method get_duration ($timer) { | ||||||||||||||||||||||||||
|
Subroutine
get_duration: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 4 | return $timer_href->{$timer}->{DUR}; | |||||||||||||||||||||||||||
| 102 | 4 | } | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 get_note ($timer) | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Get the note associated with the timer labelled $timer. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | ▶ | method get_note ($timer) { | ||||||||||||||||||||||||||
|
Subroutine
get_note: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 1 | return $timer_href->{$timer}->{NOTE}; | |||||||||||||||||||||||||||
| 112 | 4 | } | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 finish () | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Finish this set of timers and calculate the overall duration, excluding | ||||||||||||||||||||||||||||
| 117 | the duration of any internal timers (those with labels that are prefixed | ||||||||||||||||||||||||||||
| 118 | with '_'). The overall duration is associated with label '_Overall'. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 2 | ▶ | method finish () { | ||||||||||||||||||||||||||
|
Subroutine
finish: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 2 | my @warnings; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | foreach my $k ( keys $timer_href->%* ) { | |||||||||||||||||||||||||||
| 126 | 9 | ▶ | if ( $timer_href->{$k}->{START} and not exists $timer_href->{$k}->{DUR} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$timer_href->{$k}{"START"} and not exists $timer_href->{$k}{"DUR"}
MC/DC100%2/2
$timer_href->{$k}{"START"} and not exists $timer_href->{$k}{"DUR"}
$timer_href->{$k}{"START"}
not exists $timer_href->{$k}{"DUR"}
Truth table100%3/3
A and not(B)
A= $timer_href->{$k}{"START"}B= not exists $timer_href->{$k}{"DUR"}
| |||||||||||||||||||||||||||||
| 127 | 2 | push @warnings, '*W* forcing stop of timer ' . $k; | |||||||||||||||||||||||||||
| 128 | 2 | $self->stop($k); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # calculate the time not captured by other timing categories | ||||||||||||||||||||||||||||
| 133 | 2 | $timer_href->{'_Other'}->{DUR} = $timer_href->{'_Overall'}->{DUR}; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | foreach my $k ( keys $timer_href->%* ) { | |||||||||||||||||||||||||||
| 136 | 11 | ▶ | next if $k =~ m{ \A ( _ | [(]internal[)] ) }xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 7 | $timer_href->{'_Other'}->{DUR} -= $timer_href->{$k}->{DUR}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | return @warnings; | |||||||||||||||||||||||||||
| 141 | 4 | } | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 DOES | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Defined by Object::Pad. Included for POD::Coverage. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 META | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Defined by Object::Pad. Included for POD::Coverage. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 4 | 1; | |||||||||||||||||||||||||||