Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit 5277758ef45ee709e6740bb45a6adc44c9d82a55 @redhotpenguin committed Jan 13, 2011
Showing with 631 additions and 0 deletions.
  1. +8 −0 Changes
  2. +6 −0 MANIFEST
  3. +12 −0 Makefile.PL
  4. +87 −0 README
  5. +178 −0 lib/RHP/Timer.pm
  6. +8 −0 t/Changes
  7. +6 −0 t/MANIFEST
  8. +12 −0 t/Makefile.PL
  9. +87 −0 t/README
  10. +49 −0 t/RHP-Timer.t
  11. +178 −0 t/lib/RHP/Timer.pm
8 Changes
@@ -0,0 +1,8 @@
+Revision history for Perl extension RHP::Timer.
+0.1 Weds May 10 04::20:00 2007
+ - Fixed some unrealistic tests
+ - Fiddle fiddle
+ - initial tests for accuracy and precision
+ - original version; created by h2xs 1.23 with options
+ -X RHP-Timer
+
6 MANIFEST
@@ -0,0 +1,6 @@
+Changes
+Makefile.PL
+MANIFEST
+README
+t/RHP-Timer.t
+lib/RHP/Timer.pm
12 Makefile.PL
@@ -0,0 +1,12 @@
+use 5.009006;
+use ExtUtils::MakeMaker;
+WriteMakefile(
+ NAME => 'RHP::Timer',
+ VERSION => '0.1',
+ PREREQ_PM => {Time::HiRes => '1.65'},
+ (
+ ABSTRACT => 'The RedHotPenguin high resolution timer abstration',
+ AUTHOR => 'Fred Moyer <fred@redhotpenguin.com'
+ ),
+ );
+1;
87 README
@@ -0,0 +1,87 @@
+NAME
+ RHP::Timer - A high resolution timer abstraction
+
+SYNOPSIS
+ use RHP::Timer ();
+ use My::Logger ();
+
+ $logger = My::Logger->new;
+ $timer = RHP::Timer->new();
+
+ # timing data from the point of the caller
+ $timer->start('fizzbin');
+ fizzbin(); # how fast is fizzbin?
+ $logger->info(
+ sprintf("Timing caller: %s %s %d, timer_name: %s, time: %s",
+ @{$timer->checkpoint}));
+
+ # or simpler
+ $timer->start('foobin');
+ foobin();
+ $logger->info("pid $$ timer " . $timer->current .
+ " took " . $timer->stop . " seconds");
+
+ # what was the last timing block?
+ $logger->info("Last timing block " . $timer->current .
+ " took " . $timer->last_interval . " seconds");
+
+DESCRIPTION
+ RHP::Timer is a wrapper around Time::HiRes. I wrote it because I needed
+ some simple abstractions for timing programs to determine bottlenecks in
+ running programs.
+
+ The goals of RHP::Timer is to be easy to use, accurate, and simple.
+
+METHODS
+ new()
+ $timer = RHP::Timer->new();
+
+ Constructor which takes no arguments and returns a timer object
+
+ start()
+ $timer->start('fizzbin');
+
+ Starts the timer for 'fizzbin'
+
+ stop()
+ $interval = $timer->stop;
+
+ Stops the last timer started, and returns the number of seconds
+ between start and stop.
+
+ current()
+ $timer_name = $timer->current();
+ # $timer_name is 'fizzbin' from previous pod
+
+ Returns the name of the most recent timer started.
+
+ checkpoint()
+ [ caller(), $timer_name, $interval ] = $timer->checkpoint();
+
+ Stops the current timer and returns an array reference containing
+ caller() information, the name of the timer stopped, and the
+ interval of the last timing run. Useful for passing to a logfile in
+ sprintf or other format.
+
+ last_interval()
+ $last_interval = $timer->last_interval;
+
+ Returns the last timing interval recorded by the timer object.
+
+BUGS
+ None known yet. If you find any, or want a feature, email the author.
+
+SEE ALSO
+ Time::HiRes(3)
+
+AUTHOR
+ Fred Moyer <fred@redhotpenguin.com>
+
+COPYRIGHT
+ Copyright 2007 Red Hot Penguin Consulting LLC
+
+LICENSE
+ 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.6 or, at
+ your option, any later version of Perl 5 you may have available.
+
178 lib/RHP/Timer.pm
@@ -0,0 +1,178 @@
+package RHP::Timer;
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+ RHP::Timer - A high resolution timer abstraction
+
+=head1 SYNOPSIS
+
+ use RHP::Timer ();
+ use My::Logger ();
+
+ $logger = My::Logger->new;
+ $timer = RHP::Timer->new();
+
+ # timing data from the point of the caller
+ $timer->start('fizzbin');
+ fizzbin(); # how fast is fizzbin?
+ $logger->info(
+ sprintf("Timing caller: %s %s %d, timer_name: %s, time: %s",
+ @{$timer->checkpoint}));
+
+ # or simpler
+ $timer->start('foobin');
+ foobin();
+ $logger->info("pid $$ timer " . $timer->current .
+ " took " . $timer->stop . " seconds");
+
+ # what was the last timing block?
+ $logger->info("Last timing block " . $timer->current .
+ " took " . $timer->last_interval . " seconds");
+
+=head1 DESCRIPTION
+
+RHP::Timer is a wrapper around Time::HiRes. I wrote it because I
+needed some simple abstractions for timing programs to
+determine bottlenecks in running programs.
+
+The goals of RHP::Timer is to be easy to use, accurate, and simple.
+
+=cut
+
+use Time::HiRes ();
+
+our $VERSION = 0.1;
+
+=head1 METHODS
+
+=over 4
+
+=item new()
+
+ $timer = RHP::Timer->new();
+
+Constructor which takes no arguments and returns a timer object
+
+=cut
+
+sub new {
+ my ($class) = @_;
+ my $self = {};
+ bless $self, $class;
+ return $self;
+}
+
+=item start()
+
+ $timer->start('fizzbin');
+
+Starts the timer for 'fizzbin'
+
+=cut
+
+sub start {
+ my ($self, $name) = @_;
+ $self->{$name}->{_start} = [Time::HiRes::gettimeofday];
+ $self->{_current} = $name;
+ return 1;
+}
+
+=item stop()
+
+ $interval = $timer->stop;
+
+Stops the last timer started, and returns the number of seconds between
+start and stop.
+
+=cut
+
+sub stop {
+ my ($self) = @_;
+ no strict 'refs';
+ $self->{$self->{_current}}->{_stop} = [Time::HiRes::gettimeofday];
+ $self->{$self->{_current}}->{interval} =
+ Time::HiRes::tv_interval($self->{$self->{_current}}->{_start},
+ $self->{$self->{_current}}->{_stop});
+ return $self->{$self->{_current}}->{interval};
+}
+
+=item current()
+
+ $timer_name = $timer->current();
+ # $timer_name is 'fizzbin' from previous pod
+
+Returns the name of the most recent timer started.
+
+=cut
+
+sub current {
+ my $self = shift;
+ return $self->{_current};
+}
+
+=item checkpoint()
+
+ [ caller(), $timer_name, $interval ] = $timer->checkpoint();
+
+Stops the current timer and returns an array reference containing caller()
+information, the name of the timer stopped, and the interval of the last
+timing run. Useful for passing to a logfile in sprintf or other format.
+
+=cut
+
+sub checkpoint {
+ my $self = shift;
+ my $stop = $self->stop;
+ my @summary = ( caller, $self->current, $self->stop);
+ return \@summary;
+}
+
+=item last_interval()
+
+ $last_interval = $timer->last_interval;
+
+Returns the last timing interval recorded by the timer object.
+
+=cut
+
+sub last_interval {
+ my $self = shift;
+ return $self->{$self->{_current}}->{interval};
+}
+
+=pod
+
+=back
+
+=cut
+
+1;
+
+__END__
+
+=head1 BUGS
+
+None known yet. If you find any, or want a feature, email the author.
+
+=head1 SEE ALSO
+
+Time::HiRes(3)
+
+=head1 AUTHOR
+
+Fred Moyer <fred@redhotpenguin.com>
+
+=head1 COPYRIGHT
+
+Copyright 2007 Red Hot Penguin Consulting LLC
+
+=head1 LICENSE
+
+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.6 or,
+at your option, any later version of Perl 5 you may have available.
+
+=cut
8 t/Changes
@@ -0,0 +1,8 @@
+Revision history for Perl extension RHP::Timer.
+0.1 Weds May 10 04::20:00 2007
+ - Fixed some unrealistic tests
+ - Fiddle fiddle
+ - initial tests for accuracy and precision
+ - original version; created by h2xs 1.23 with options
+ -X RHP-Timer
+
6 t/MANIFEST
@@ -0,0 +1,6 @@
+Changes
+Makefile.PL
+MANIFEST
+README
+t/RHP-Timer.t
+lib/RHP/Timer.pm
12 t/Makefile.PL
@@ -0,0 +1,12 @@
+use 5.009006;
+use ExtUtils::MakeMaker;
+WriteMakefile(
+ NAME => 'RHP::Timer',
+ VERSION => '0.1',
+ PREREQ_PM => {Time::HiRes => '1.65'},
+ (
+ ABSTRACT => 'The RedHotPenguin high resolution timer abstration',
+ AUTHOR => 'Fred Moyer <fred@redhotpenguin.com'
+ ),
+ );
+1;
87 t/README
@@ -0,0 +1,87 @@
+NAME
+ RHP::Timer - A high resolution timer abstraction
+
+SYNOPSIS
+ use RHP::Timer ();
+ use My::Logger ();
+
+ $logger = My::Logger->new;
+ $timer = RHP::Timer->new();
+
+ # timing data from the point of the caller
+ $timer->start('fizzbin');
+ fizzbin(); # how fast is fizzbin?
+ $logger->info(
+ sprintf("Timing caller: %s %s %d, timer_name: %s, time: %s",
+ @{$timer->checkpoint}));
+
+ # or simpler
+ $timer->start('foobin');
+ foobin();
+ $logger->info("pid $$ timer " . $timer->current .
+ " took " . $timer->stop . " seconds");
+
+ # what was the last timing block?
+ $logger->info("Last timing block " . $timer->current .
+ " took " . $timer->last_interval . " seconds");
+
+DESCRIPTION
+ RHP::Timer is a wrapper around Time::HiRes. I wrote it because I needed
+ some simple abstractions for timing programs to determine bottlenecks in
+ running programs.
+
+ The goals of RHP::Timer is to be easy to use, accurate, and simple.
+
+METHODS
+ new()
+ $timer = RHP::Timer->new();
+
+ Constructor which takes no arguments and returns a timer object
+
+ start()
+ $timer->start('fizzbin');
+
+ Starts the timer for 'fizzbin'
+
+ stop()
+ $interval = $timer->stop;
+
+ Stops the last timer started, and returns the number of seconds
+ between start and stop.
+
+ current()
+ $timer_name = $timer->current();
+ # $timer_name is 'fizzbin' from previous pod
+
+ Returns the name of the most recent timer started.
+
+ checkpoint()
+ [ caller(), $timer_name, $interval ] = $timer->checkpoint();
+
+ Stops the current timer and returns an array reference containing
+ caller() information, the name of the timer stopped, and the
+ interval of the last timing run. Useful for passing to a logfile in
+ sprintf or other format.
+
+ last_interval()
+ $last_interval = $timer->last_interval;
+
+ Returns the last timing interval recorded by the timer object.
+
+BUGS
+ None known yet. If you find any, or want a feature, email the author.
+
+SEE ALSO
+ Time::HiRes(3)
+
+AUTHOR
+ Fred Moyer <fred@redhotpenguin.com>
+
+COPYRIGHT
+ Copyright 2007 Red Hot Penguin Consulting LLC
+
+LICENSE
+ 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.6 or, at
+ your option, any later version of Perl 5 you may have available.
+
49 t/RHP-Timer.t
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More tests => 12;
+
+my $pkg;
+
+BEGIN {
+ $pkg = 'RHP::Timer';
+ use_ok($pkg);
+}
+
+can_ok($pkg, qw( new start stop current checkpoint last_interval));
+
+my $supervisor_timer = $pkg->new();
+my $timer = $pkg->new();
+isa_ok($timer, $pkg, 'constructor');
+
+my $interval = 1;
+$supervisor_timer->start('supervisor');
+$timer->start('foo');
+sleep $interval;
+my $stop = $timer->stop();
+my $super_stop = $supervisor_timer->stop();
+
+cmp_ok($timer->current(), 'eq', 'foo', 'current ok');
+cmp_ok($stop-$interval, '<', 0.005, 'precise to 0.005 seconds');
+
+# accuracy
+my $error = $super_stop - $stop;
+cmp_ok($error, '<', 0.005, 'accurate to 0.005 seconds');
+
+
+# checkpoint
+$timer->start('checkpoint');
+sleep 2;
+my $checkpoint_ary_ref = $timer->checkpoint();
+cmp_ok($checkpoint_ary_ref->[0], 'eq', __PACKAGE__, 'package ok');
+cmp_ok($checkpoint_ary_ref->[1], 'eq', $0, 'filename ok');
+cmp_ok($checkpoint_ary_ref->[2], '==', 39, 'file line ok'); # line number-1?
+cmp_ok($checkpoint_ary_ref->[3], 'eq', 'checkpoint', 'correct timer name');
+cmp_ok($checkpoint_ary_ref->[4], '==', $timer->last_interval);
+
+# last interval
+like($timer->last_interval, qr/^2\.0\d+/, 'last_interval');
+
+1;
178 t/lib/RHP/Timer.pm
@@ -0,0 +1,178 @@
+package RHP::Timer;
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+ RHP::Timer - A high resolution timer abstraction
+
+=head1 SYNOPSIS
+
+ use RHP::Timer ();
+ use My::Logger ();
+
+ $logger = My::Logger->new;
+ $timer = RHP::Timer->new();
+
+ # timing data from the point of the caller
+ $timer->start('fizzbin');
+ fizzbin(); # how fast is fizzbin?
+ $logger->info(
+ sprintf("Timing caller: %s %s %d, timer_name: %s, time: %s",
+ @{$timer->checkpoint}));
+
+ # or simpler
+ $timer->start('foobin');
+ foobin();
+ $logger->info("pid $$ timer " . $timer->current .
+ " took " . $timer->stop . " seconds");
+
+ # what was the last timing block?
+ $logger->info("Last timing block " . $timer->current .
+ " took " . $timer->last_interval . " seconds");
+
+=head1 DESCRIPTION
+
+RHP::Timer is a wrapper around Time::HiRes. I wrote it because I
+needed some simple abstractions for timing programs to
+determine bottlenecks in running programs.
+
+The goals of RHP::Timer is to be easy to use, accurate, and simple.
+
+=cut
+
+use Time::HiRes ();
+
+our $VERSION = 0.1;
+
+=head1 METHODS
+
+=over 4
+
+=item new()
+
+ $timer = RHP::Timer->new();
+
+Constructor which takes no arguments and returns a timer object
+
+=cut
+
+sub new {
+ my ($class) = @_;
+ my $self = {};
+ bless $self, $class;
+ return $self;
+}
+
+=item start()
+
+ $timer->start('fizzbin');
+
+Starts the timer for 'fizzbin'
+
+=cut
+
+sub start {
+ my ($self, $name) = @_;
+ $self->{$name}->{_start} = [Time::HiRes::gettimeofday];
+ $self->{_current} = $name;
+ return 1;
+}
+
+=item stop()
+
+ $interval = $timer->stop;
+
+Stops the last timer started, and returns the number of seconds between
+start and stop.
+
+=cut
+
+sub stop {
+ my ($self) = @_;
+ no strict 'refs';
+ $self->{$self->{_current}}->{_stop} = [Time::HiRes::gettimeofday];
+ $self->{$self->{_current}}->{interval} =
+ Time::HiRes::tv_interval($self->{$self->{_current}}->{_start},
+ $self->{$self->{_current}}->{_stop});
+ return $self->{$self->{_current}}->{interval};
+}
+
+=item current()
+
+ $timer_name = $timer->current();
+ # $timer_name is 'fizzbin' from previous pod
+
+Returns the name of the most recent timer started.
+
+=cut
+
+sub current {
+ my $self = shift;
+ return $self->{_current};
+}
+
+=item checkpoint()
+
+ [ caller(), $timer_name, $interval ] = $timer->checkpoint();
+
+Stops the current timer and returns an array reference containing caller()
+information, the name of the timer stopped, and the interval of the last
+timing run. Useful for passing to a logfile in sprintf or other format.
+
+=cut
+
+sub checkpoint {
+ my $self = shift;
+ my $stop = $self->stop;
+ my @summary = ( caller, $self->current, $self->stop);
+ return \@summary;
+}
+
+=item last_interval()
+
+ $last_interval = $timer->last_interval;
+
+Returns the last timing interval recorded by the timer object.
+
+=cut
+
+sub last_interval {
+ my $self = shift;
+ return $self->{$self->{_current}}->{interval};
+}
+
+=pod
+
+=back
+
+=cut
+
+1;
+
+__END__
+
+=head1 BUGS
+
+None known yet. If you find any, or want a feature, email the author.
+
+=head1 SEE ALSO
+
+Time::HiRes(3)
+
+=head1 AUTHOR
+
+Fred Moyer <fred@redhotpenguin.com>
+
+=head1 COPYRIGHT
+
+Copyright 2007 Red Hot Penguin Consulting LLC
+
+=head1 LICENSE
+
+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.6 or,
+at your option, any later version of Perl 5 you may have available.
+
+=cut

0 comments on commit 5277758

Please sign in to comment.