| 1 | package Class::Easy::Timer; | ||||||||||||
| 2 | # $Id: Timer.pm,v 1.3 2009/07/20 18:00:10 apla Exp $ | ||||||||||||
| 3 | |||||||||||||
| 4 | 5 | ▶ | use Class::Easy::Import; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 5 | ▶ | use Class::Easy::Log (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 5 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 4 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 10 | 4 | my $class = shift; | |||||||||||
| 11 | |||||||||||||
| 12 | 4 | my $logger = Class::Easy::Log::logger ('default'); | |||||||||||
| 13 | |||||||||||||
| 14 | 4 | ▶ | if (ref $_[-1] eq 'Class::Easy::Log') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 1 | $logger = pop @_; | |||||||||||
| 16 | } | ||||||||||||
| 17 | |||||||||||||
| 18 | 4 | ▶ | my $msg = join (' ', @_) || ''; | ||||||||||
|
Condition50%1/2
join(' ', @_) || ''
MC/DC0%0/1
join(' ', @_) || ''
join(' ', @_)
Truth table50%1/2
A
A= join(' ', @_)
| |||||||||||||
| 19 | |||||||||||||
| 20 | return bless [], $class | ||||||||||||
| 21 | 4 | ▶ | unless $logger->{tied}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | |||||||||||||
| 23 | 2 | my $t = [gettimeofday]; | |||||||||||
| 24 | |||||||||||||
| 25 | 2 | bless [$msg, $t, $t, undef, $logger], $class; | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 2 | ▶ | sub lap { | ||||||||||
|
Subroutine
lap: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 29 | 2 | my $self = shift; | |||||||||||
| 30 | 2 | ▶ | my $msg = shift || ''; | ||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 31 | |||||||||||||
| 32 | return 0 | ||||||||||||
| 33 | 2 | ▶ | unless $self->[4]->{tied}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | |||||||||||||
| 35 | 1 | my $interval = tv_interval ($self->[1]); | |||||||||||
| 36 | |||||||||||||
| 37 | 1 | my $caller1 = [caller (1)]; | |||||||||||
| 38 | 1 | my $caller0 = [caller]; | |||||||||||
| 39 | |||||||||||||
| 40 | Class::Easy::Log::_wrapper ( | ||||||||||||
| 41 | 1 | $self->[4]->{category}, $self->[4], $caller1, $caller0, | |||||||||||
| 42 | "$self->[0]: " . $interval*1000 . 'ms' | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | 1 | $self->[0] = $msg; | |||||||||||
| 46 | |||||||||||||
| 47 | 1 | $self->[1] = [gettimeofday]; | |||||||||||
| 48 | |||||||||||||
| 49 | 1 | return $interval; | |||||||||||
| 50 | |||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 4 | ▶ | sub end { | ||||||||||
|
Subroutine
end: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 54 | 4 | my $self = shift; | |||||||||||
| 55 | |||||||||||||
| 56 | return 0 | ||||||||||||
| 57 | 4 | ▶ | unless $self->[4]->{tied}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | |||||||||||||
| 59 | 2 | my $interval = tv_interval ($self->[1]); | |||||||||||
| 60 | |||||||||||||
| 61 | 2 | $self->[3] = $interval; | |||||||||||
| 62 | |||||||||||||
| 63 | 2 | my $caller1 = [caller (1)]; | |||||||||||
| 64 | 2 | my $caller0 = [caller]; | |||||||||||
| 65 | |||||||||||||
| 66 | Class::Easy::Log::_wrapper ( | ||||||||||||
| 67 | 2 | $self->[4]->{category}, $self->[4], $caller1, $caller0, | |||||||||||
| 68 | "$self->[0]: " . $interval*1000 . 'ms' | ||||||||||||
| 69 | ); | ||||||||||||
| 70 | |||||||||||||
| 71 | 2 | return $interval; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 2 | ▶ | sub total { | ||||||||||
|
Subroutine
total: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 75 | 2 | my $self = shift; | |||||||||||
| 76 | |||||||||||||
| 77 | return 0 | ||||||||||||
| 78 | 2 | ▶ | unless $self->[4]->{tied}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | |||||||||||||
| 80 | 1 | ▶ | return $self->[3] | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | unless $self->[2]; | ||||||||||||
| 82 | |||||||||||||
| 83 | 1 | my $interval = tv_interval ($self->[2], $self->[1]) + $self->[3]; | |||||||||||
| 84 | |||||||||||||
| 85 | 1 | my $caller1 = [caller (1)]; | |||||||||||
| 86 | 1 | my $caller0 = [caller]; | |||||||||||
| 87 | |||||||||||||
| 88 | Class::Easy::Log::_wrapper ( | ||||||||||||
| 89 | 1 | $self->[4]->{category}, $self->[4], $caller1, $caller0, | |||||||||||
| 90 | "total time: " . $interval*1000 . 'ms' | ||||||||||||
| 91 | ); | ||||||||||||
| 92 | |||||||||||||
| 93 | 1 | return $interval; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | |||||||||||||
| 97 | 5 | 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<< <apla at the-singlers.us> >> | ||||||||||||
| 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<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Easy>. | ||||||||||||
| 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 | ||||||||||||