File Coverage

lib/Weather/GHCN/TimingStats.pm
Criterion Covered Total %
statement 58 58 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 12 12 100.0
pod 6 6 100.0
total 85 85 100.0


line stmt bran cond sub pod time code
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.011
17            
18             =head1 SYNOPSIS
19            
20             use Weather::GHCN::TimingStats qw(:all);
21            
22            
23             =head1 DESCRIPTION
24            
25             The B 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             # these are needed because perlcritic fails to detect that Object::Pad handles these things
34             ## no critic [ValuesAndExpressions::ProhibitVersionStrings]
35            
36 5     5   3479 use v5.18; # minimum for Object::Pad
  5         24  
37 5     5   29 use warnings;
  5         12  
  5         230  
38 5     5   626 use Object::Pad 0.66 qw( :experimental(init_expr) );
  5         12977  
  5         31  
39            
40             package Weather::GHCN::TimingStats;
41             class Weather::GHCN::TimingStats;
42            
43             our $VERSION = 'v0.0.011';
44            
45 5     5   4136 use Carp;
  5         27  
  5         382  
46 5     5   449 use Const::Fast;
  5         2875  
  5         36  
47 5     5   2250 use Time::HiRes;
  5         4657  
  5         29  
48            
49             const my $EMPTY => q(); # empty string
50            
51             field $timer_href { {} };
52            
53             =head1 METHODS
54            
55             =head2 new ()
56            
57             Create a new TimingStats object.
58            
59             =head2 start($timer)
60            
61             Start a timer labelled $timer. Timer labels prefixed with underscore
62             (_) are considered to be internal and not included in the overall
63             duration.
64            
65             =cut
66            
67 216     216 1 3289 method start ($timer) {
  216         445  
  216         568  
  216         445  
68 216         1672 $timer_href->{$timer}->{START} = [Time::HiRes::gettimeofday];
69 216         742 return;
70             }
71            
72             =head2 stop ($timer, $note='')
73            
74             Stop the timer labelled $timer, with an optional note.
75            
76             =cut
77            
78 208     208 1 1422 method stop ($timer, $note=$EMPTY) {
  208         497  
  208         512  
  208         516  
  208         497  
79 208         2548 $timer_href->{$timer}->{DUR} += Time::HiRes::tv_interval($timer_href->{$timer}->{START},[Time::HiRes::gettimeofday]);
80            
81 208 100       4389 $timer_href->{$timer}->{NOTE} = $note
82             if $note;
83            
84 208         629 return;
85             }
86            
87             =head2 get_timers ()
88            
89             Get a sorted list of all the timer labels that have been created so
90             far by invoking the start() method.
91            
92             =cut
93            
94 4     4 1 15 method get_timers () {
  4         8  
  4         8  
95 4         44 return (sort keys $timer_href->%*);
96             }
97            
98             =head2 get_duration($timer)
99            
100             Get the time that has elapsed for the timer labelled $timer.
101            
102             =cut
103            
104 42     42 1 780 method get_duration ($timer) {
  42         59  
  42         62  
  42         56  
105 42         208 return $timer_href->{$timer}->{DUR};
106             }
107            
108             =head2 get_note ($timer)
109            
110             Get the note associated with the timer labelled $timer.
111            
112             =cut
113            
114 39     39 1 72 method get_note ($timer) {
  39         55  
  39         57  
  39         50  
115 39         141 return $timer_href->{$timer}->{NOTE};
116             }
117            
118             =head2 finish ()
119            
120             Finish this set of timers and calculate the overall duration, excluding
121             the duration of any internal timers (those with labels that are prefixed
122             with '_'). The overall duration is associated with label '_Overall'.
123            
124             =cut
125            
126 5     5 1 17 method finish () {
  5         13  
  5         8  
127 5         10 my @warnings;
128            
129 5         27 foreach my $k ( keys $timer_href->%* ) {
130 34 100 100     131 if ( $timer_href->{$k}->{START} and not exists $timer_href->{$k}->{DUR} ) {
131 2         9 push @warnings, '*W* forcing stop of timer ' . $k;
132 2         6 $self->stop($k);
133             }
134             }
135            
136             # calculate the time not captured by other timing categories
137 5         24 $timer_href->{'_Other'}->{DUR} = $timer_href->{'_Overall'}->{DUR};
138            
139 5         18 foreach my $k ( keys $timer_href->%* ) {
140 39 100       122 next if $k =~ m{ \A ( _ | [(]internal[)] ) }xms;
141 29         55 $timer_href->{'_Other'}->{DUR} -= $timer_href->{$k}->{DUR};
142             }
143            
144 5         19 return @warnings;
145             }
146            
147             =head2 DOES
148            
149             Defined by Object::Pad. Included for POD::Coverage.
150            
151             =head2 META
152            
153             Defined by Object::Pad. Included for POD::Coverage.
154            
155             =cut
156            
157             1;