Permalink
Browse files

Dancer::Timer depends hardly on Time::HiRes (which is core since Perl…

… 5.7)
  • Loading branch information...
1 parent 2b77d79 commit 128752074757f902ad1e78bd61edbd32cc8d4fa9 Alexis Sukrieh committed Apr 6, 2010
Showing with 11 additions and 56 deletions.
  1. +1 −0 Makefile.PL
  2. +2 −2 lib/Dancer/Logger/Abstract.pm
  3. +7 −24 lib/Dancer/Timer.pm
  4. +0 −23 t/16_timer/01_basic.t
  5. +1 −7 t/16_timer/02_hires.t
View
1 Makefile.PL
@@ -32,6 +32,7 @@ WriteMakefile1(
'File::Basename' => '0',
'File::Spec' => '0',
'Encode' => '0',
+ 'Time::HiRes' => '0', # in core since 5.7
},
test => {TESTS => join( ' ', (glob( 't/*.t'), glob('t/*/*.t')))},
View
4 lib/Dancer/Logger/Abstract.pm
@@ -41,10 +41,10 @@ sub format_message {
$file ||= '-';
$line ||= '-';
- my $time = Dancer::SharedData->timer->to_string();
+ my $time = Dancer::SharedData->timer->tick;
my $r = Dancer::SharedData->request;
if (defined $r) {
- return "[$$] $level \@$time> [Req. #".$r->id."] $message in $file l. $line\n";
+ return "[$$] $level \@$time> [hit #".$r->id."] $message in $file l. $line\n";
}
else {
return "[$$] $level \@$time> $message in $file l. $line\n";
View
31 lib/Dancer/Timer.pm
@@ -3,43 +3,26 @@ package Dancer::Timer;
use strict;
use warnings;
use base 'Dancer::Object';
+use Time::HiRes 'gettimeofday', 'tv_interval';
use Dancer::ModuleLoader;
-Dancer::Timer->attributes('mode', 'start_time');
+Dancer::Timer->attributes('start_time');
sub init {
my ($self) = @_;
- if (Dancer::ModuleLoader->load('Time::HiRes')) {
- $self->mode('hires');
- $self->start_time([ Time::HiRes::gettimeofday() ]);
- }
- else {
- $self->mode('seconds');
- $self->start_time(time());
- }
+ $self->start_time([ gettimeofday() ]);
}
sub tick {
my ($self) = @_;
- if ($self->mode eq 'hires') {
- my $now = [ Time::HiRes::gettimeofday() ];
- my $delay = Time::HiRes::tv_interval($self->start_time, $now);
- return sprintf('%0f', $delay);
- }
- else {
- my $now = time();
- return $now - $self->start_time;
- }
+ my $now = [ gettimeofday() ];
+ my $delay = tv_interval($self->start_time, $now);
+ return sprintf('%0f', $delay);
}
sub to_string {
my ($self) = @_;
- if ($self->mode eq 'hires') {
- $self->tick;
- }
- else {
- localtime(time());
- }
+ $self->tick;
}
1;
View
23 t/16_timer/01_basic.t
@@ -1,23 +0,0 @@
-use Test::More tests => 7;
-use strict;
-use warnings;
-
-# we don't want Time::HiRes to get loaded
-use t::lib::EasyMocker;
-mock 'Dancer::ModuleLoader'
- => method 'load'
- => should sub { 0 };
-
-use Dancer::Timer;
-
-my $timer = Dancer::Timer->new;
-ok(defined($timer), "timer is defined");
-isa_ok($timer, 'Dancer::Object');
-isa_ok($timer, 'Dancer::Timer');
-can_ok($timer, 'tick');
-is($timer->mode, 'seconds', "timer is on mode 'seconds'");
-
-ok(defined($timer->start_time), "start_time is defined");
-sleep 1;
-ok(($timer->tick > 0), "tick has been increased");
-
View
8 t/16_timer/02_hires.t
@@ -2,20 +2,14 @@ use Test::More;
use strict;
use warnings;
+plan tests => 6;
use Dancer::Timer;
-use Dancer::ModuleLoader;
-
-plan skip_all => "Time::HiRes is needed to run this test"
- unless Dancer::ModuleLoader->load('Time::HiRes');
-plan tests => 7;
my $timer = Dancer::Timer->new;
ok(defined($timer), "timer is defined");
isa_ok($timer, 'Dancer::Object');
isa_ok($timer, 'Dancer::Timer');
can_ok($timer, 'tick');
-is($timer->mode, 'hires', "timer is on mode 'hires'");
-
ok(defined($timer->start_time), "start_time is defined");
sleep 1;
ok(($timer->tick > 1), "tick has been increased: ".$timer->tick);

0 comments on commit 1287520

Please sign in to comment.