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