File Coverage

blib/lib/MooseX/Role/Timer.pm
Criterion Covered Total %
statement 30 36 83.3
branch 6 8 75.0
condition 3 8 37.5
subroutine 7 9 77.7
pod 5 5 100.0
total 51 66 77.2


line stmt bran cond sub pod time code
1             package MooseX::Role::Timer;
2             $MooseX::Role::Timer::VERSION = '0.05';
3             #use Any::Moose '::Role';
4 2     2   3783752 use Moo::Role;
  2         5  
  2         12  
5 2     2   1391 use Time::HiRes;
  2         1853  
  2         12  
6 2     2   253 use strict;
  2         3  
  2         59  
7 2     2   9 use warnings;
  2         2  
  2         955  
8              
9             =head1 NAME
10              
11             MooseX::Role::Timer - Measure times with your object.
12              
13             =head1 SYNOPSIS
14              
15             package Demo;
16             use Moose; # or Any::Moose
17             with 'MooseX::Role::Timer';
18              
19             sub BUILD {
20             shift->start_timer("build");
21             }
22              
23             sub do_something {
24             my $self = shift;
25             $self->start_timer("something");
26             # do something...
27             $self->stop_timer("something");
28             }
29              
30             package main;
31             my $demo = Demo->new;
32             $demo->do_something;
33             $demo->do_something;
34             printf "%3.6fs\n", $demo->elapsed_timer("build"); # time spent since BUILD
35             printf "%3.6fs\n", $demo->elapsed_timer("something"); # time spent in sub do_something
36              
37             This Role provides your object with timers, making it easier to keep track of how long
38             whatever actions take.
39              
40             =cut
41              
42             has '_timers' => ( is=>'rw', default=>sub{{}} );
43              
44             =over 4
45              
46             =item start_timer($name)
47              
48             Start timer $name.
49              
50             =cut
51              
52             sub start_timer {
53 11     11 1 78 my $self = shift;
54 11   50     32 my $name = shift || die "usage: start_timer('name')";
55 11 100       37 if ( ! exists $self->_timers->{$name} ) {
56 3         7 $self->_timers->{$name} = [0];
57             }
58 11         61 $self->_timers->{$name}->[1] = [ Time::HiRes::gettimeofday ];
59             }
60              
61             =item stop_timer($name)
62              
63             Stop timer $name. Could be started again to cumulatively measure time.
64              
65             =cut
66              
67             sub stop_timer {
68 10     10 1 91713 my $self = shift;
69 10   50     54 my $name = shift || die "usage: stop_timer('name')";
70 10         70 my $timer = $self->_timers->{$name};
71 10 50       34 if ( $timer->[1] ) {
72 10         52 $timer->[0] += Time::HiRes::tv_interval( $timer->[1] );
73 10         247 $timer->[1] = undef;
74             } else {
75 0         0 warn "timer '$name' is not running";
76             }
77             }
78              
79             =item reset_timer($name)
80              
81             Stops timer $name and clears cumulated times for $name.
82              
83             =cut
84              
85             sub reset_timer {
86 0     0 1 0 my $self = shift;
87 0   0     0 my $name = shift || die "usage: reset_timer('name')";
88 0         0 $self->_timers->{$name} = [0];
89             }
90              
91             =item elapsed_timer('name')
92              
93             Return the elapsed time in seconds (cumulated) for timer $name.
94              
95             =cut
96              
97             sub elapsed_timer {
98 3     3 1 13 my $self = shift;
99 3   50     7 my $name = shift || die "usage: elapsed_timer('name')";
100 3 50       9 die "timer '$name' was never started" if ! exists $self->_timers->{$name};
101 3         7 my $elapsed = $self->_timers->{$name}->[0];
102 3 100       7 if ( $self->_timers->{$name}->[1] ) {
103 1         7 $elapsed += Time::HiRes::tv_interval( $self->_timers->{$name}->[1] );
104             }
105 3         12 return $elapsed;
106             }
107              
108             =item timer_names
109              
110             Return all timer names.
111              
112             =cut
113              
114             sub timer_names {
115 0     0 1   return keys %{ shift->_timers };
  0            
116             }
117              
118             =back
119              
120             =head1 AUTHOR
121              
122             Michael Langner, C<< >>
123              
124             =head1 BUGS
125              
126             Please report any bugs or feature requests to
127             C, or through the web interface at
128             L.
129             I will be notified, and then you'll automatically be notified of progress on
130             your bug as I make changes.
131              
132             =head1 COPYRIGHT & LICENSE
133              
134             Copyright 2015 Michael Langner, all rights reserved.
135              
136             This program is free software; you can redistribute it and/or modify it under the
137             same terms as Perl itself.
138              
139             =cut
140              
141             1; # track-id: 3a59124cfcc7ce26274174c962094a20