| 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 | use v5.18; # minimum for Object::Pad | ||
| 34 | use Object::Pad 0.66 qw( :experimental(init_expr) ); | ||
| 35 | |||
| 36 | package Weather::GHCN::TimingStats; | ||
| 37 | class Weather::GHCN::TimingStats; | ||
| 38 | |||
| 39 | our $VERSION = 'v0.0.002'; | ||
| 40 | |||
| 41 | use Carp; | ||
| 42 | use Const::Fast; | ||
| 43 | use Time::HiRes; | ||
| 44 | |||
| 45 | const my $EMPTY => q(); # empty string | ||
| 46 | |||
| 47 | 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 | method start ($timer) { | ||
| 64 | $timer_href->{$timer}->{START} = [Time::HiRes::gettimeofday]; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 stop ($timer, $note='') | ||
| 69 | |||
| 70 | Stop the timer labelled $timer, with an optional note. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | method stop ($timer, $note=$EMPTY) { | ||
| 75 | $timer_href->{$timer}->{DUR} += Time::HiRes::tv_interval($timer_href->{$timer}->{START},[Time::HiRes::gettimeofday]); | ||
| 76 | |||
| 77 | $timer_href->{$timer}->{NOTE} = $note | ||
| 78 | if $note; | ||
| 79 | |||
| 80 | return; | ||
| 81 | } | ||
| 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 | method get_timers () { | ||
| 91 | return (sort keys $timer_href->%*); | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 get_duration($timer) | ||
| 95 | |||
| 96 | Get the time that has elapsed for the timer labelled $timer. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | method get_duration ($timer) { | ||
| 101 | return $timer_href->{$timer}->{DUR}; | ||
| 102 | } | ||
| 103 | |||
| 104 | =head2 get_note ($timer) | ||
| 105 | |||
| 106 | Get the note associated with the timer labelled $timer. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | method get_note ($timer) { | ||
| 111 | return $timer_href->{$timer}->{NOTE}; | ||
| 112 | } | ||
| 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 | method finish () { | ||
| 123 | my @warnings; | ||
| 124 | |||
| 125 | foreach my $k ( keys $timer_href->%* ) { | ||
| 126 | if ( $timer_href->{$k}->{START} and not exists $timer_href->{$k}->{DUR} ) { | ||
| 127 | push @warnings, '*W* forcing stop of timer ' . $k; | ||
| 128 | $self->stop($k); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | # calculate the time not captured by other timing categories | ||
| 133 | $timer_href->{'_Other'}->{DUR} = $timer_href->{'_Overall'}->{DUR}; | ||
| 134 | |||
| 135 | foreach my $k ( keys $timer_href->%* ) { | ||
| 136 | next if $k =~ m{ \A ( _ | [(]internal[)] ) }xms; | ||
| 137 | $timer_href->{'_Other'}->{DUR} -= $timer_href->{$k}->{DUR}; | ||
| 138 | } | ||
| 139 | |||
| 140 | return @warnings; | ||
| 141 | } | ||
| 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 | 1; |