++ed by:
NEYASOV MELO MISHIN BESSARABV

4 PAUSE users
3 non-PAUSE users.

גאבור סבו - Gábor Szabó
and 1 contributors

NAME

Devel::Timer - Track and report execution time for parts of code

SYNOPSIS

  use Devel::Timer;

  my $t = Devel::Timer->new();

  $t->mark("first db query");

  ## do some work

  $t->mark("second db query");

  ## do some more work

  $t->mark("end of second db query");

  $t->report();

DESCRIPTION

Devel::Timer allows developers to accurately time how long a specific piece of code takes to execute. This can be helpful in locating the slowest parts of an existing application.

First, the Devel::Timer module is used and instantiated.

  use Devel::Timer;

  my $t = Devel::Timer->new();

Second, markers are placed before and after pieces of code that need to be timed. For this example, we are profiling the methods get_user_score() and get_average_user_score().

  $t->mark("first db query");
  &get_user_score($user);

  $t->mark("second db query");
  &get_average_user_score();

Finally, at the end of the code that you want to profile, and end marker is place, and a report is generated on stderr.

  $t->mark("END");
  $t->report();

Sample report:

  Devel::Timer Report -- Total time: 0.3464 secs
  Interval  Time    Percent
  ----------------------------------------------
  02 -> 03  0.3001  86.63%  second db query -> END
  01 -> 02  0.0461  13.30%  first db query -> second db query
  00 -> 01  0.0002   0.07%  INIT -> first db query

The report is output using the method Devel::Timer::print() which currently just prints to stderr. If you want to send the output to a custom location you can override the print() method. The initialize() and shutdown() methods can also overridden if you want to open and close log files or database connections.

Methods

new

Create a new instance. No parameters are processed.

initialize

Empty method. Can be implemented in the subclass.

mark(NAME)

Set a timestamp with a NAME.

print

Prints to the standar error. Can be overridden in the subclass.

report

Generates the report. Prints using the print method.

shutdown

Empty method. Can be implemented in subclass.

Subclassing

e.g.

package MyTimer;

use strict; use Devel::Timer; use vars qw(@ISA);

@ISA = ("Devel::Timer");

sub initialize { my $log = "/tmp/timer.log"; open(LOG, ">>$log") or die("Unable to open [$log] for writing."); }

sub print { my($self, $msg) = @_; print LOG $msg . "\n"; }

sub shutdown { close LOG; }

You would then use the new module MyTimer exactly as you would use Devel::Timer.

  use MyTimer;
  my $t = MyTimer->new();
  $t->mark("about to do x");
  $t->mark("about to do y");
  $t->mark("done y");
  $t->report();

SEE ALSO

Time::HiRes

Copyright

Jason Moore

This is free software. It is licensed under the same terms as Perl itself.

AUTHOR

  Jason Moore - jmoore@sober.com

  Maintainer: Gabor Szabo - gabor@pti.co.il