| 1 | ################################################## | ||||||||||||||||||||
| 2 | package Log::Log4perl::Util::TimeTracker; | ||||||||||||||||||||
| 3 | ################################################## | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 54 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 54 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 54 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 54 | ▶ | use Log::Log4perl::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 54 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 54 | our $TIME_HIRES_AVAILABLE; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 54 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 14 | # Check if we've got Time::HiRes. If not, don't make a big fuss, | ||||||||||||||||||||
| 15 | # just set a flag so we know later on that we can't have fine-grained | ||||||||||||||||||||
| 16 | # time stamps | ||||||||||||||||||||
| 17 | 54 | $TIME_HIRES_AVAILABLE = 0; | |||||||||||||||||||
| 18 | 54 | ▶ | if(Log::Log4perl::Util::module_available("Time::HiRes")) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 54 | require Time::HiRes; | |||||||||||||||||||
| 20 | 54 | $TIME_HIRES_AVAILABLE = 1; | |||||||||||||||||||
| 21 | } | ||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | ################################################## | ||||||||||||||||||||
| 25 | 168 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 26 | ################################################## | ||||||||||||||||||||
| 27 | 168 | my $class = shift; | |||||||||||||||||||
| 28 | 168 | ▶ | $class = ref ($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 168 | my $self = { | |||||||||||||||||||
| 31 | reset_time => undef, | ||||||||||||||||||||
| 32 | @_, | ||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | $self->{time_function} = \&_gettimeofday unless | ||||||||||||||||||||
| 36 | 168 | ▶ | defined $self->{time_function}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 168 | bless $self, $class; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 168 | $self->reset(); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 168 | return $self; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | ################################################## | ||||||||||||||||||||
| 46 | 4 | ▶ | sub hires_available { | ||||||||||||||||||
|
Subroutine
hires_available: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 47 | ################################################## | ||||||||||||||||||||
| 48 | 4 | return $TIME_HIRES_AVAILABLE; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | ################################################## | ||||||||||||||||||||
| 52 | 271 | ▶ | sub _gettimeofday { | ||||||||||||||||||
|
Subroutine
_gettimeofday: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 53 | ################################################## | ||||||||||||||||||||
| 54 | # Return secs and optionally msecs if we have Time::HiRes | ||||||||||||||||||||
| 55 | 271 | ▶ | if($TIME_HIRES_AVAILABLE) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 271 | return (Time::HiRes::gettimeofday()); | |||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||
| 58 | 0 | return (time(), 0); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | ################################################## | ||||||||||||||||||||
| 63 | 285 | ▶ | sub gettimeofday { | ||||||||||||||||||
|
Subroutine
gettimeofday: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 64 | ################################################## | ||||||||||||||||||||
| 65 | 285 | my($self) = @_; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 285 | my($seconds, $microseconds) = $self->{time_function}->(); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 285 | ▶ | $microseconds = 0 if ! defined $microseconds; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 285 | return($seconds, $microseconds); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | ################################################## | ||||||||||||||||||||
| 74 | 168 | ▶ | sub reset { | ||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 75 | ################################################## | ||||||||||||||||||||
| 76 | 168 | my($self) = @_; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 168 | my $current_time = [$self->gettimeofday()]; | |||||||||||||||||||
| 79 | 168 | $self->{reset_time} = $current_time; | |||||||||||||||||||
| 80 | 168 | $self->{last_call_time} = $current_time; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 168 | return $current_time; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | ################################################## | ||||||||||||||||||||
| 86 | 61 | ▶ | sub time_diff { | ||||||||||||||||||
|
Subroutine
time_diff: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 87 | ################################################## | ||||||||||||||||||||
| 88 | 61 | my($time_from, $time_to) = @_; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 61 | my $seconds = $time_to->[0] - | |||||||||||||||||||
| 91 | $time_from->[0]; | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 61 | my $milliseconds = int(( $time_to->[1] - | |||||||||||||||||||
| 94 | $time_from->[1] ) / 1000); | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 61 | ▶ | if($milliseconds < 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | $milliseconds = 1000 + $milliseconds; | |||||||||||||||||||
| 98 | 0 | $seconds--; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 61 | return($seconds, $milliseconds); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | ################################################## | ||||||||||||||||||||
| 105 | 53 | ▶ | sub milliseconds { | ||||||||||||||||||
|
Subroutine
milliseconds: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 106 | ################################################## | ||||||||||||||||||||
| 107 | 53 | my($self, $current_time) = @_; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 53 | ▶ | $current_time = [ $self->gettimeofday() ] unless | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | defined $current_time; | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | my($seconds, $milliseconds) = time_diff( | ||||||||||||||||||||
| 113 | $self->{reset_time}, | ||||||||||||||||||||
| 114 | 53 | $current_time); | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 53 | return $seconds*1000 + $milliseconds; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | ################################################## | ||||||||||||||||||||
| 120 | 8 | ▶ | sub delta_milliseconds { | ||||||||||||||||||
|
Subroutine
delta_milliseconds: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 121 | ################################################## | ||||||||||||||||||||
| 122 | 8 | my($self, $current_time) = @_; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 8 | ▶ | $current_time = [ $self->gettimeofday() ] unless | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | defined $current_time; | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | my($seconds, $milliseconds) = time_diff( | ||||||||||||||||||||
| 128 | $self->{last_call_time}, | ||||||||||||||||||||
| 129 | 8 | $current_time); | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 8 | $self->{last_call_time} = $current_time; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 8 | return $seconds*1000 + $milliseconds; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 54 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||