line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Time::Checkpoint; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
3375
|
use 5.12.0; |
|
4
|
|
|
|
|
15
|
|
|
4
|
|
|
|
|
190
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
22
|
use feature qw{ state }; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
486
|
|
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
10365
|
use Time::HiRes qw{ time }; |
|
4
|
|
|
|
|
8216
|
|
|
4
|
|
|
|
|
20
|
|
8
|
4
|
|
|
4
|
|
4633
|
use Params::Validate qw{ :all }; |
|
4
|
|
|
|
|
62821
|
|
|
4
|
|
|
|
|
1054
|
|
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
40
|
use constant IDX_CALLBACK => 0; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
257
|
|
11
|
4
|
|
|
4
|
|
22
|
use constant IDX_CKPOINTS => 1; |
|
4
|
|
|
|
|
35
|
|
|
4
|
|
|
|
|
4756
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
3
|
|
|
3
|
1
|
2678
|
my $package = shift; |
15
|
|
|
|
|
|
|
|
16
|
3
|
|
|
|
|
16
|
state $val = { |
17
|
|
|
|
|
|
|
callback => { |
18
|
|
|
|
|
|
|
type => CODEREF, |
19
|
|
|
|
|
|
|
optional => 1, |
20
|
|
|
|
|
|
|
}, |
21
|
|
|
|
|
|
|
}; |
22
|
3
|
|
|
|
|
99
|
validate( @_, $val ); |
23
|
|
|
|
|
|
|
|
24
|
3
|
|
|
|
|
13
|
my $self = [ ]; |
25
|
|
|
|
|
|
|
|
26
|
3
|
100
|
|
|
|
20
|
if (defined { @_ }->{callback}) { |
27
|
1
|
|
|
|
|
3
|
$self->[IDX_CALLBACK] = { @_ }->{callback}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
3
|
|
|
|
|
10
|
$self->[IDX_CKPOINTS] = { }; |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
16
|
return bless $self, $package; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub checkpoint { |
36
|
13
|
|
|
13
|
1
|
4095
|
state $val = [ |
37
|
|
|
|
|
|
|
{ isa => 'Time::Checkpoint', }, |
38
|
|
|
|
|
|
|
{ type => SCALAR, }, |
39
|
|
|
|
|
|
|
]; |
40
|
13
|
|
|
|
|
313
|
validate_pos( @_, @$val ); |
41
|
|
|
|
|
|
|
|
42
|
13
|
|
|
|
|
43
|
my ($self, $cp) = (@_); |
43
|
|
|
|
|
|
|
|
44
|
13
|
100
|
|
|
|
84
|
if (not defined $self->[IDX_CKPOINTS]->{$cp}) { |
|
|
50
|
|
|
|
|
|
45
|
6
|
|
|
|
|
8
|
my $ot = undef; |
46
|
6
|
|
|
|
|
21
|
my $nt = time; |
47
|
6
|
100
|
|
|
|
21
|
if ($self->[IDX_CALLBACK]) { |
48
|
1
|
|
|
|
|
6
|
$self->[IDX_CALLBACK]->( $cp, $ot, $nt ); |
49
|
|
|
|
|
|
|
} |
50
|
6
|
|
|
|
|
1408
|
$self->[IDX_CKPOINTS]->{$cp} = $nt; |
51
|
6
|
|
|
|
|
24
|
return 0; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif (defined $self->[IDX_CKPOINTS]->{$cp}) { |
54
|
7
|
|
|
|
|
24
|
my $ot = $self->[IDX_CKPOINTS]->{$cp}; |
55
|
7
|
|
|
|
|
29
|
my $nt = time; |
56
|
7
|
100
|
|
|
|
24
|
if ($self->[IDX_CALLBACK]) { |
57
|
2
|
|
|
|
|
11
|
$self->[IDX_CALLBACK]->( $cp, $self->[IDX_CKPOINTS]->{$cp}, $nt ); |
58
|
|
|
|
|
|
|
} |
59
|
7
|
|
|
|
|
5122
|
$self->[IDX_CKPOINTS]->{$cp} = $nt; |
60
|
7
|
|
|
|
|
48
|
return $nt - $ot; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
0
|
1
|
|
sub cp { checkpoint( @_ ) } |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub list_checkpoints { |
67
|
0
|
|
|
0
|
1
|
|
state $val = [ |
68
|
|
|
|
|
|
|
{ isa => 'Time::Checkpoint' }, |
69
|
|
|
|
|
|
|
]; |
70
|
0
|
|
|
|
|
|
validate_pos( @_, @$val ); |
71
|
0
|
|
|
|
|
|
my ($self) = (@_); |
72
|
0
|
|
|
|
|
|
my $points = $self->[IDX_CKPOINTS]; |
73
|
0
|
|
|
|
|
|
return $points; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
0
|
1
|
|
sub lscp { list_checkpoints( @_ ) } |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub checkpoint_status { |
79
|
0
|
|
|
0
|
1
|
|
state $val = [ |
80
|
|
|
|
|
|
|
{ isa => 'Time::Checkpoint', }, |
81
|
|
|
|
|
|
|
{ type => SCALAR, }, |
82
|
|
|
|
|
|
|
]; |
83
|
0
|
|
|
|
|
|
validate_pos( @_, @$val ); |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my ($self, $cp) = (@_); |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return $self->[IDX_CKPOINTS]->{$cp}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
0
|
1
|
|
sub cpstat { checkpoint_status( @_ ) } |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub checkpoint_remove { |
93
|
0
|
|
|
0
|
1
|
|
state $val = [ |
94
|
|
|
|
|
|
|
{ isa => 'Time::Checkpoint', }, |
95
|
|
|
|
|
|
|
{ type => SCALAR, }, |
96
|
|
|
|
|
|
|
]; |
97
|
0
|
|
|
|
|
|
validate_pos( @_, @$val ); |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my ($self, $cp) = (@_); |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
if (defined $self->[IDX_CKPOINTS]->{$cp}) { |
102
|
0
|
|
|
|
|
|
return delete $self->[IDX_CKPOINTS]->{$cp}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
else { |
105
|
0
|
|
|
|
|
|
return undef; |
106
|
|
|
|
|
|
|
} |
107
|
0
|
|
|
|
|
|
return undef; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
0
|
1
|
|
sub cprm { checkpoint_remove( @_ ) } |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub flush { |
113
|
0
|
|
|
0
|
1
|
|
state $val = [ |
114
|
|
|
|
|
|
|
{ isa => 'Time::Checkpoint', }, |
115
|
|
|
|
|
|
|
{ type => SCALAR, }, |
116
|
|
|
|
|
|
|
]; |
117
|
0
|
|
|
|
|
|
validate_pos( @_, @$val ); |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
my ($self)= (@_); |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
$self->[IDX_CKPOINTS] = { }; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=pod |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 NAME |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Time::Checkpoint |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 ABSTRACT |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Simple module to report deltas between waypoints in code, with extensible |
135
|
|
|
|
|
|
|
reactions. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SYNOPSIS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
my $t = Time::Checkpoint->new( ); |
140
|
|
|
|
|
|
|
$t->checkpoint( 'Start' ); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Code elapses ... |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
my $delta = $t->checkpoint( 'Start' ); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# With callback |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $t = Time::Checkpoint->new( |
149
|
|
|
|
|
|
|
callback => \&print_delta |
150
|
|
|
|
|
|
|
); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$t->checkpoint( 'foo' ); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub print_delta { |
155
|
|
|
|
|
|
|
my ($checkpoint, $old_time, $new_time) = (@_); |
156
|
|
|
|
|
|
|
my $delta = $new_time - $old_time; |
157
|
|
|
|
|
|
|
$LOG->debug( "$checkpoint: delta $delta seconds" ); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# More code elapses... |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$t->checkpoint( 'foo' ); # print_delta is called |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 METHODS |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=over 2 |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 Constructor |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item B |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=over 2 |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The constructor takes either no arguments or a hash with one key: I. |
175
|
|
|
|
|
|
|
If a callback is passed (a code ref), that code will be called with the arguments |
176
|
|
|
|
|
|
|
$name_of_checkpoint, $old_timestamp, $new_timestamp. Returns a Time::Checkpoint object. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=back |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item B |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item B |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=over 2 |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Takes one argument, the name of the checkpoint reached. When called, it will perform |
187
|
|
|
|
|
|
|
a hash lookup to determine when it was called last. It will return the delta between |
188
|
|
|
|
|
|
|
the two. It will also call the 'callback' code, as mentioned above, provided it exists. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=back |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item B |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item B |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=over 2 |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Takes no arguments. Returns a hash of checkpoints and their timestamps. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=back |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item B |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=item B |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=over 2 |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Returns the timestamp for a given checkpoint, or undef. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=back |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item B |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item B |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=over 2 |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Removes the specified checkpoint, returning its value if it had one. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=back |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item B |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=over 2 |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Removes all checkpoints. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=back |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 AUTHOR |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Jane A. Avriette |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 BUGS |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Yep. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# jaa // vim:tw=80:ts=2:noet |