| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Benchmark::Timer::Class - Perl module for timing the execution of methods in a specified object | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use Benchmark::Timer::Class; | ||
| 8 | use The_Real_Module; | ||
| 9 | $obj = new The_Real_Module(); | ||
| 10 | $th = new Benchmark::Timer::Class($obj); | ||
| 11 | $th->method1_name_from_real_module(); | ||
| 12 | $th->method2_name_from_real_module(); | ||
| 13 | $th->method1_name_from_real_module(); | ||
| 14 | $th->report(); | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | The Benchmark::Timer::Class enables you to determine elapsed | ||
| 19 | times for calls to methods of a specified object during normal | ||
| 20 | running of your program with minimal amount of editing. | ||
| 21 | |||
| 22 | =head2 Methods | ||
| 23 | |||
| 24 | =over 10 | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | package Benchmark::Timer::Class; | ||
| 29 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | 1 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 0 | ▶ | use Benchmark::Timer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 33 | |||
| 34 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||
| 35 | @ISA = qw(Exporter); | ||
| 36 | @EXPORT = qw(); | ||
| 37 | $VERSION = '0.02'; | ||
| 38 | |||
| 39 | use vars qw($AUTOLOAD); | ||
| 40 | |||
| 41 | =item $th = Benchmark::Timer::Class->new($original_object); | ||
| 42 | |||
| 43 | Takes an object reference and returns a reference to a Benchmark::Timer::Class object | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my ($class,$timed_class) = @_; | ||
| 49 | my $self = {}; | ||
| 50 | $self->{timed_class} = $timed_class; | ||
| 51 | return bless $self, $class; | ||
| 52 | } | ||
| 53 | |||
| 54 | =item $th->report; | ||
| 55 | |||
| 56 | Outputs a timing report to STDERR | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub report { | ||
| 61 | my $self = shift; | ||
| 62 | $self->{stats}->report(); | ||
| 63 | } | ||
| 64 | |||
| 65 | =item $th->result($methodname); | ||
| 66 | |||
| 67 | Returns the mean time for all calls to method $methodname. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub result { | ||
| 72 | my $self = shift; | ||
| 73 | return $self->{stats}->result(@_); | ||
| 74 | } | ||
| 75 | |||
| 76 | =item $th->results; | ||
| 77 | |||
| 78 | Returns the timing data as a hash keyed on object method names. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub results { | ||
| 83 | my $self = shift; | ||
| 84 | return $self->{stats}->results(); | ||
| 85 | } | ||
| 86 | |||
| 87 | =item $th->data($methodname), $th->data; | ||
| 88 | |||
| 89 | When called with an $methodname returns the raw timing data as an array. | ||
| 90 | When called with no arguments returns the raw timing data as hash keyed on | ||
| 91 | object method names, where the values of the hash are lists of timings for | ||
| 92 | calls to that object method. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub data { | ||
| 97 | my $self = shift; | ||
| 98 | return $self->{stats}->data(@_); | ||
| 99 | } | ||
| 100 | |||
| 101 | # | ||
| 102 | # Internal Routine(s) | ||
| 103 | # | ||
| 104 | # AUTOLOAD catches all method calls destined for the object being timed | ||
| 105 | # and wraps them in start() and stop() calls to a Time::Timer object. | ||
| 106 | # | ||
| 107 | sub AUTOLOAD { | ||
| 108 | my $self = shift; | ||
| 109 | # Get the name of the routine that the user wanted to call | ||
| 110 | # in the first place. | ||
| 111 | my $routine = $AUTOLOAD; | ||
| 112 | # Strip off the package/module stuff at the start | ||
| 113 | $routine =~ s/.*:://; | ||
| 114 | # Dont pass on the DESTROY call | ||
| 115 | return if $routine eq 'DESTROY'; | ||
| 116 | # Create a new Timer object if we dont already have one | ||
| 117 | if (!exists $self->{stats}) { | ||
| 118 | $self->{stats} = new Benchmark::Timer(); | ||
| 119 | } | ||
| 120 | # Start the timer, call the routine, then stop the timer. | ||
| 121 | # Finally return the results to the user. | ||
| 122 | my ($result,@results); | ||
| 123 | if (wantarray) { | ||
| 124 | # User called a routine that required an array/hash return value | ||
| 125 | $self->{stats}->start($routine); | ||
| 126 | @results = $self->{timed_class}->$routine(@_); | ||
| 127 | $self->{stats}->stop($routine); | ||
| 128 | return @results; | ||
| 129 | } else { | ||
| 130 | # User called a routine that required a scalar return value | ||
| 131 | $self->{stats}->start($routine); | ||
| 132 | $result = $self->{timed_class}->$routine(@_); | ||
| 133 | $self->{stats}->stop($routine); | ||
| 134 | return $result; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =head1 AUTHOR | ||
| 141 | |||
| 142 | D. Neil, E<lt>perl@dougneil.co.ukE<gt> | ||
| 143 | |||
| 144 | =head1 SEE ALSO | ||
| 145 | |||
| 146 | L<Time::HiRes>, L<Benchmark::Timer> | ||
| 147 | |||
| 148 | =head1 COPYRIGHT | ||
| 149 | |||
| 150 | Copyright(c) 2001 Doug Neil. | ||
| 151 | |||
| 152 | This library is free software; you can redistribute it and/or modify | ||
| 153 | it under the same terms as Perl itself. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | 1; | ||
| 158 | __END__ | ||