line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Easy::Timer; |
2
|
|
|
|
|
|
|
# $Id: Timer.pm,v 1.3 2009/07/20 18:00:10 apla Exp $ |
3
|
|
|
|
|
|
|
|
4
|
6
|
|
|
6
|
|
32
|
use Class::Easy::Import; |
|
6
|
|
|
|
|
21
|
|
|
6
|
|
|
|
|
40
|
|
5
|
6
|
|
|
6
|
|
3488
|
use Class::Easy::Log (); |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
182
|
|
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
7346
|
use Time::HiRes qw(gettimeofday tv_interval); |
|
6
|
|
|
|
|
17388
|
|
|
6
|
|
|
|
|
28
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
4
|
|
|
4
|
1
|
10
|
my $class = shift; |
11
|
|
|
|
|
|
|
|
12
|
4
|
|
|
|
|
18
|
my $logger = Class::Easy::Log::logger ('default'); |
13
|
|
|
|
|
|
|
|
14
|
4
|
100
|
|
|
|
16
|
if (ref $_[-1] eq 'Class::Easy::Log') { |
15
|
1
|
|
|
|
|
2
|
$logger = pop @_; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
4
|
|
50
|
|
|
20
|
my $msg = join (' ', @_) || ''; |
19
|
|
|
|
|
|
|
|
20
|
4
|
100
|
|
|
|
23
|
return bless [], $class |
21
|
|
|
|
|
|
|
unless $logger->{tied}; |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
|
|
26
|
my $t = [gettimeofday]; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
11
|
bless [$msg, $t, $t, undef, $logger], $class; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub lap { |
29
|
2
|
|
|
2
|
1
|
1005138
|
my $self = shift; |
30
|
2
|
|
50
|
|
|
13
|
my $msg = shift || ''; |
31
|
|
|
|
|
|
|
|
32
|
2
|
100
|
|
|
|
34
|
return 0 |
33
|
|
|
|
|
|
|
unless $self->[4]->{tied}; |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
12
|
my $interval = tv_interval ($self->[1]); |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
34
|
my $caller1 = [caller (1)]; |
38
|
1
|
|
|
|
|
9
|
my $caller0 = [caller]; |
39
|
|
|
|
|
|
|
|
40
|
1
|
|
|
|
|
36
|
Class::Easy::Log::_wrapper ( |
41
|
|
|
|
|
|
|
$self->[4]->{category}, $self->[4], $caller1, $caller0, |
42
|
|
|
|
|
|
|
"$self->[0]: " . $interval*1000 . 'ms' |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
|
|
3
|
$self->[0] = $msg; |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
5
|
$self->[1] = [gettimeofday]; |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
7
|
return $interval; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub end { |
54
|
4
|
|
|
4
|
1
|
2001285
|
my $self = shift; |
55
|
|
|
|
|
|
|
|
56
|
4
|
100
|
|
|
|
57
|
return 0 |
57
|
|
|
|
|
|
|
unless $self->[4]->{tied}; |
58
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
31
|
my $interval = tv_interval ($self->[1]); |
60
|
|
|
|
|
|
|
|
61
|
2
|
|
|
|
|
46
|
$self->[3] = $interval; |
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
9
|
my $caller1 = [caller (1)]; |
64
|
2
|
|
|
|
|
22
|
my $caller0 = [caller]; |
65
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
52
|
Class::Easy::Log::_wrapper ( |
67
|
|
|
|
|
|
|
$self->[4]->{category}, $self->[4], $caller1, $caller0, |
68
|
|
|
|
|
|
|
"$self->[0]: " . $interval*1000 . 'ms' |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
9
|
return $interval; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub total { |
75
|
2
|
|
|
2
|
1
|
641
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
2
|
100
|
|
|
|
12
|
return 0 |
78
|
|
|
|
|
|
|
unless $self->[4]->{tied}; |
79
|
|
|
|
|
|
|
|
80
|
1
|
50
|
|
|
|
5
|
return $self->[3] |
81
|
|
|
|
|
|
|
unless $self->[2]; |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
5
|
my $interval = tv_interval ($self->[2], $self->[1]) + $self->[3]; |
84
|
|
|
|
|
|
|
|
85
|
1
|
|
|
|
|
10
|
my $caller1 = [caller (1)]; |
86
|
1
|
|
|
|
|
3
|
my $caller0 = [caller]; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
12
|
Class::Easy::Log::_wrapper ( |
89
|
|
|
|
|
|
|
$self->[4]->{category}, $self->[4], $caller1, $caller0, |
90
|
|
|
|
|
|
|
"total time: " . $interval*1000 . 'ms' |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
3
|
return $interval; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 NAME |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Class::Easy::Timer - really easy timer |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 ABSTRACT |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
SYNOPSIS |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
use Class::Easy; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# timer doesn't run without properly configured logger |
112
|
|
|
|
|
|
|
logger ('default')->appender (*STDERR); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$t = timer ('sleep one second'); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sleep (1); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $interval = $t->lap ('one more second'); # $interval == 1 |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
warn "your system have bad timer: 1s = ${interval}s" |
121
|
|
|
|
|
|
|
if $interval < 1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sleep (1); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$interval = $t->end; # $interval == 1 |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
warn "your system have bad timer: 1s = ${interval}s" |
128
|
|
|
|
|
|
|
if $interval < 1; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
$interval = $t->total; # $interval == 2 |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 METHODS |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 new |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
create timer, start new lap and return timer object |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 lap |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
get lap duration and start a new lap |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 end |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
get duration for last lap |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 total |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
get duration between timer creation and end call |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 AUTHOR |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Ivan Baktsheev, C<< >> |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 BUGS |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Please report any bugs or feature requests to my email address, |
166
|
|
|
|
|
|
|
or through the web interface at L. |
167
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified |
168
|
|
|
|
|
|
|
of progress on your bug as I make changes. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 SUPPORT |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Copyright 2008-2009 Ivan Baktsheev |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
183
|
|
|
|
|
|
|
under the same terms as Perl itself. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |