I made this module for my use,
It gives me a very simple interface to set timers, multiples... by name. and i can get a "report" that tells me an average of each (if named the same).
I use it like this:
use lib '/where/you/have/Timers.pm';
use Timers.pm
Tstart('ok');
for (@these){
Tstart('one_this');
do { this($_);};
Tstop('one_this');
}
for (@these2){
Tstart('one_this2');
do { this($_);};
Tstop('one_this2');
}
Tstop('ok);
Treport();
so.. it would output how many runs there were of "one_this" and how long they took on average, etc.
It prints to STDERR, so it's useful for webstuffs, you just read /var/log/apache2/error-log or what have you, in that case
Requires Time::HiRes
package Timers;
#BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/srv/www/htdocs/timer
+s-log") or die("Unable to open error-log: $!\n"); carpout(LOG);} # op
+tional other log
use vars qw{$VERSION @ISA @EXPORT @EXPORT_OK};
require Exporter; @ISA = qw(Exporter);
@EXPORT = qw(&Tstop &Tstart &Treport); # just to calling package
$VERSION = '0.01';
use strict;
use Time::HiRes qw(gettimeofday);
my %CONF =(
human_legible=>1, # if on, uses limit for shown second vals
decimals=>3 # default decimal places for human legible vals
);
# running timers
our %TIMERS=();
# stored results, arrays labeled after timer labels
our %HISTORY=();
sub Tstart {
my $label=$_[0];
$TIMERS{$label}=gettimeofday;
}
sub Tstop {
no warnings;
my $label=$_[0];
my $elapsed = (gettimeofday-$TIMERS{$label});
delete $TIMERS{$label};
push @{$HISTORY{$label}}, $elapsed;
if ($CONF{human_legible}){
$elapsed = sprintf("%.$CONF{decimals}f",$elapsed);
}
return $elapsed;#someone may want to use that right away.
}
sub Treport {
print STDERR "--\nTimers.pm Report ..\n".`date`."\n";
for ( sort { $HISTORY{$a} cmp $HISTORY{$b} } keys %HISTORY ){
my $timer=$_;
my $total=scalar(@{$HISTORY{$timer}});
my $timetotal=0;
for (@{$HISTORY{$timer}}){ $timetotal = ( $timetotal + $_ ); }
my $average = ( $timetotal / $total );
if ($CONF{human_legible}){
$timetotal = sprintf("%.$CONF{decimals}f",$timetotal);
$total = sprintf("%.$CONF{decimals}f",$total);
$average = sprintf("%.$CONF{decimals}f",$average);
}
print STDERR "$timer\n [$total] avg: $average\n sum:$timeto
+tal\n\n";
}
#clear history
%HISTORY=();
}
1;
It's not a full blown module! It's just a script that solved my needs. Maybe it helps your needs.
|