File Coverage

blib/lib/DiaColloDB/Timer.pm
Criterion Covered Total %
statement 9 28 32.1
branch 0 6 0.0
condition 0 3 0.0
subroutine 3 10 30.0
pod 7 7 100.0
total 19 54 35.1


line stmt bran cond sub pod time code
1             ## -*- Mode: CPerl -*-
2             ## File: DiaColloDB::Client.pm
3             ## Author: Bryan Jurish <moocow@cpan.org>
4             ## Description: collocation db, timer
5              
6             package DiaColloDB::Timer;
7 1     1   6 use Time::HiRes qw(gettimeofday tv_interval);
  1         2  
  1         9  
8 1     1   222 use DiaColloDB::Utils qw(:time);
  1         2  
  1         26  
9 1     1   171 use strict;
  1         1  
  1         324  
10              
11              
12             ##==============================================================================
13             ## Globals & Constants
14              
15             our @ISA = qw();
16              
17             ##==============================================================================
18             ## Constructors etc.
19              
20             ## $timer = CLASS_OR_OBJECT->new(%args)
21             ## + %args, object structure:
22             ## (
23             ## started => $t0, ##-- time last operation started
24             ## elapsed => $elapsed, ##-- elapsed time (after stop())
25             ## )
26             sub new {
27 0     0 1   my $that = shift;
28 0   0       return bless({
29             started=>undef,
30             elapsed=>0,
31             @_
32             }, ref($that)||$that);
33             }
34              
35             ##==============================================================================
36             ## Timing
37              
38             ## $timer = CLASS_OR_OBJECT->start()
39             ## + (re-)starts timer
40             sub start {
41 0     0 1   my $timer = shift;
42 0 0         $timer = $timer->new() if (!ref($timer));
43 0           $timer->{started} = [gettimeofday];
44 0           return $timer;
45             }
46              
47             ## $timer = $timer->stop()
48             ## + stops timer and adds current interval to {elapsed}
49             sub stop {
50 0     0 1   my $timer = shift;
51 0 0         return $timer if (!defined($timer->{started}));
52 0           $timer->{elapsed} = $timer->elapsed();
53 0           $timer->{started} = undef;
54 0           return $timer;
55             }
56              
57             ## $timer = $timer->reset()
58             ## + stops and re-sets timer
59             sub reset {
60 0     0 1   my $timer = shift;
61 0           $timer->{started} = undef;
62 0           $timer->{elapsed} = 0;
63 0           return $timer;
64             }
65              
66             ## $elapsed = $timer->elapsed()
67             ## + get total elapsed time for this timer
68             sub elapsed {
69 0     0 1   my $timer = shift;
70 0 0         return $timer->{elapsed} + (defined($timer->{started}) ? tv_interval($timer->{started},[gettimeofday]) : 0);
71             }
72              
73              
74             ## $hms = $timer->hms($sfmt?)
75             ## ($h,$m,$s) = $timer->hms($sfmt?)
76             ## + parses and optionally formats elapsed time as HH:MM:SS.SSS
77             sub hms {
78 0     0 1   return DiaColloDB::Utils::s2hms($_[0]->elapsed,@_[1..$#_]);
79             }
80              
81             ## $timestr = $timer->timestr($sfmt?)
82             ## + parses and formats elapsed time as Hh?Mm?Ss
83             sub timestr {
84 0     0 1   return DiaColloDB::Utils::s2timestr($_[0]->elapsed,@_[1..$#_]);
85             }
86              
87             ##==============================================================================
88             ## Footer
89             1;
90              
91             __END__
92              
93              
94              
95