/usr/lib/perl5/Xacobeo/Timer.pm is in xacobeo 0.13-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | package Xacobeo::Timer;
=encoding utf8
=head1 NAME
Xacobeo::Timer - A custom made timer.
=head1 SYNOPSIS
use Xacobeo::Timer;
# As a one time use
my $timer = Xacobeo::Timer->start("Long operation");
do_long_operation();
$timer->elapsed(); # Displays the time elapsed
# A simple stop watch (the destructor displays the time elapsed)
my $TIMER = Xacobeo::Timer->new("Method calls");
sub hotspot {
$TIMER->start();
# Very slow stuff here
$TIMER->stop();
}
=head1 DESCRIPTION
This package provides a very simple timer. This timer is used for finding hot
spots in the application.
The timer is quite simple it provides the method L</start> that starts the timer
and the method L</stop> that stops the timer and accumulates the elapsed time.
The method L</show> can be used to print the time elapsed so far while the
method L</elapsed> returns the time elapsed so far.
When an instance of this class dies (because it was undefed or collected by the
garbage collector) the builtin Perl desctrutor will automatically call the
method L</show>. But if the method I<show> or I<elapsed> was called during the
lifetime of the instance then the destructor will not invoke the method I<show>.
=head1 METHODS
The package defines the following methods:
=cut
use 5.006;
use strict;
use warnings;
use Time::HiRes qw(time);
use Xacobeo::I18n qw(__);
=head2 new
Creates a new Timer.
Parameters:
=over
=item * $name (Optional)
The name of the timer.
=back
=cut
sub new {
my ($class, $name) = @_;
my $self = {
elapsed => 0,
name => $name,
};
bless $self, ref($class) || $class;
return $self;
}
=head2 start
Starts the timer. If this sub is called without a blessed instance then a new
Timer will be created.
Parameters:
=over
=item * $name (optional)
The name is used only when called without a blessed instance.
=back
=cut
sub start {
my ($self, @params) = @_;
if (!ref $self) {
$self = $self->new(@params);
}
$self->{start} = time;
return $self;
}
=head2 stop
Stops the timer and adds accumulates the elapsed time. If the timer wasn't
started previously this results in a no-op.
=cut
sub stop {
my $self = shift;
my $start = delete $self->{start};
if (defined $start) {
$self->{elapsed} += time - $start;
}
return $self;
}
=head2 show
Prints the elapsed time. This method stops the timer if it was started
previously and wasn't stopped.
=cut
sub show {
my $self = shift;
if ($self->{start}) {
$self->stop();
}
my $name = $self->{name};
printf __("Time: %-20s %.4fs\n"),
(defined $name ? $name : __('Unnamed')),
$self->elapsed
;
return $self;
}
=head2 elapsed
Returns the total time elapsed so far. If the timer was already started the
pending time will not be taking into account.
=cut
sub elapsed {
my $self = shift;
$self->{displayed} = 1;
return $self->{elapsed};
}
sub DESTROY {
my $self = shift;
$self->show() unless $self->{displayed};
return;
}
# A true value
1;
=head1 AUTHORS
Emmanuel Rodriguez E<lt>potyl@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2008,2009 by Emmanuel Rodriguez.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.
=cut
|