NAME

Benchmark::Stopwatch::Pause - simple timing of stages of your code with a pause option.

VERSION

Version 0.02

SYNOPSIS

#!/usr/bin/perl
use strict;
use Benchmark::Stopwatch::Pause;

# a stopwatch to track the whole run of this script
my $stopwatch_whole = Benchmark::Stopwatch::Pause->new()->start;

# a stopwatch to track just select sections of the code
my $stopwatch_pause = Benchmark::Stopwatch::Pause->new()->start->pause;

sleep(5);

foreach (1..3) {
   $stopwatch_whole->lap('new loop ' . $_);
   $stopwatch_pause->unpause('time code ' . $_);
   sleep(2);
   $stopwatch_pause->pause;
   sleep(1);
}

print $stopwatch_whole->stop->summary;
print "---------\n";
print $stopwatch_pause->stop->summary;

# NAME                        TIME        CUMULATIVE      PERCENTAGE
#  start                       5.005       5.005           35.677%
#  new loop 1                  3.008       8.013           21.440%
#  new loop 2                  3.008       11.021          21.440%
#  new loop 3                  3.009       14.030          21.443%
# ---------
# NAME                        TIME        CUMULATIVE      PERCENTAGE
#  start                       0.000       0.000           0.000%
#  time code 1                 2.004       2.004           33.332%
#  time code 2                 2.004       4.008           33.332%
#  time code 3                 2.004       6.012           33.336%

DESCRIPTION

This is an extention of the handy Benchmark::Stopwatch module. This is an
attempt to allow very granular timeing of very specific sections of code. The 
Stopwatch concept is carried thru in this module, while adding the ability to
pause your stopwatch as needed.

CHANGES

Things that differ from Benchmark::Stopwatch

  • Laps are now look ahead

    The concept of a lap is diffrent from Benchmark::Stopwatch, they are now look ahead.

    In Benchmark::Stopwatch :

    # ... code that is tracked by lap 'one'
    $stopwatch->lap('one');

    In Benchmark::Stopwatch::Pause :

    $stopwatch->lap('one');
    # ... code that is tracked by lap 'one'

    This allows the time from unpause till pause to be tied to your unpause.

  • _start_ is displayed in the summary

    Due to the change in the logic of what a lap is _start_ will be displayed.

  • _stop_ is not displayed in summary

    Due to the change in the logic of what a lap is _stop_ will always be a null event.

METHODS

new

my $stopwatch = Benchmark::Stopwatch::Pause->new;

Creates a new stopwatch.

start

$stopwatch = $stopwatch->start;

Starts the stopwatch. Returns a reference to the stopwatch so that you can chain.

lap

$stopwatch = $stopwatch->lap( 'name of event' );

Notes down the time at which an event occurs. This event will later appear in the summary.

pause

$stopwatch = $stopwatch->pause;

Notes the time at which you paused the clock, this has the effect of pausing the clock. This allows you to track a small portion of repeated code without worrying about any other code.

unpause

$stopwatch = $stopwatch->unpause( 'name of event' );

unpauses your stopwatch to allow for tracking again.

stop

$stopwatch = $stopwatch->stop;

Stops the stopwatch. Returns a reference to the stopwatch so you can chain.

total_time

my $time_in_seconds = $stopwatch->total_time;

Returns the time that the stopwatch ran for in fractional seconds. If the stopwatch has not been stopped yet then it returns time it has been running for.

summary

my $summary_text = $stopwatch->summary;
-- or --
print $stopwatch->summary;

Returns text summarizing the events that occured.

as_data

my $data_structure_hashref = $stopwatch->as_data;

Returns a data structure that contains all the information that was logged. This is so that you can use this module to gather the data but then use your own code to manipulate it.

print Dumper($stopwatch_pause->stop->as_data);

would look like:

{
  'total_elapsed_time' => '14.0544471740723',
  'laps' => [
              {
                'pause_time' => 0,
                'pause' => undef,
                'apparent_elapse_time' => '1.59740447998047e-05',
                'time' => '1179438668.5038',
                'name' => '_start_',
                'elapsed_time' => '1.59740447998047e-05'
              },
              {
                'pause_time' => '5.00632405281067',
                'pause' => 1,
                'apparent_elapse_time' => '1.59740447998047e-05',
                'time' => '1179438668.50382',
                'name' => 'pause',
                'elapsed_time' => '5.00632405281067'
              },
              {
                'pause_time' => 0,
                'pause' => 0,
                'apparent_elapse_time' => '2.00797200202942',
                'time' => '1179438673.51014',
                'name' => 'time code 1',
                'elapsed_time' => '2.00795602798462'
              },
              {
                'pause_time' => '1.0080509185791',
                'pause' => 1,
                'apparent_elapse_time' => '2.00797200202942',
                'time' => '1179438675.5181',
                'name' => 'pause',
                'elapsed_time' => '1.0080509185791'
              },
              {
                'pause_time' => 0,
                'pause' => 0,
                'apparent_elapse_time' => '4.01593208312988',
                'time' => '1179438676.52615',
                'name' => 'time code 2',
                'elapsed_time' => '2.00796008110046'
              },
              {
                'pause_time' => '1.0080668926239',
                'pause' => 1,
                'apparent_elapse_time' => '4.01593208312988',
                'time' => '1179438678.53411',
                'name' => 'pause',
                'elapsed_time' => '1.0080668926239'
              },
              {
                'pause_time' => 0,
                'pause' => 0,
                'apparent_elapse_time' => '6.02388095855713',
                'time' => '1179438679.54218',
                'name' => 'time code 3',
                'elapsed_time' => '2.00794887542725'
              },
              {
                'pause_time' => '1.00811719894409',
                'pause' => 1,
                'apparent_elapse_time' => '6.02388095855713',
                'time' => '1179438681.55012',
                'name' => 'pause',
                'elapsed_time' => '1.00811719894409'
              }
            ],
  'stop_time' => '1179438682.55824',
  'start_time' => '1179438668.50379',
  'total_effective_time' => '6.0238881111145'
};

AUTHOR

Ben Hengst, <notbenh at cpan.org>

BUGS

Please report any bugs or feature requests to bug-benchmark-stopwatch-pause at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Benchmark-Stopwatch-Pause. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Benchmark::Stopwatch::Pause

You can also look for information at:

ACKNOWLEDGMENTS

I couldn't have done this extention without Benchmark::Stopwatch, Thanks so much Edmund von der Burg <evdb@ecclestoad.co.uk>

COPYRIGHT & LICENSE

Copyright 2007 Ben Hengst, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.