Skip to content

Commit

Permalink
renamed start_timer/stop_timer to profile
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 16, 2011
1 parent f26f3c5 commit 4cce790
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,7 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

1.99 2011-09-16 00:00:00
- Added EXPERIMENTAL start_timer and stop_timer helpers.
- Added EXPERIMENTAL profile helper.
- Improved documentation.
- Fixed small redirect_to bug. (judofyr, sri)
- Fixed small attribute selector bug in Mojo::DOM::CSS.
Expand Down
43 changes: 13 additions & 30 deletions lib/Mojolicious/Plugin/RequestTimer.pm
Expand Up @@ -8,25 +8,14 @@ use Time::HiRes qw/gettimeofday tv_interval/;
sub register {
my ($self, $app) = @_;

# Add "start_timer" helper
# Add "profile" helper
$app->helper(
start_timer => sub {
profile => sub {
my ($self, $name) = @_;
$self->stash->{'mojo.timer'}->{$name} = [gettimeofday()];
}
);

# Add "stop_timer" helper
$app->helper(
stop_timer => sub {
my ($self, $name) = @_;
my $elapsed = sprintf '%f',
tv_interval($self->stash->{'mojo.timer'}->{$name} || [0, 0],
[gettimeofday()]);
return
wantarray
? ($elapsed, $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed)
: $elapsed;
my $profile = $self->stash->{'mojo.time'}->{$name} ||= [gettimeofday()];
my $elapsed = sprintf '%f', tv_interval($profile, [gettimeofday()]);
return $elapsed unless wantarray;
return $elapsed, $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
}
);

Expand All @@ -42,7 +31,7 @@ sub register {
my $path = $req->url->path->to_abs_string;
my $ua = $req->headers->user_agent || 'Anonymojo';
$self->app->log->debug("$method $path ($ua).");
$self->start_timer('request');
$self->profile('mojo.request');
}
);

Expand All @@ -56,7 +45,7 @@ sub register {
my $res = $self->res;
my $code = $res->code || 200;
my $message = $res->message || $res->default_message($code);
my ($elapsed, $rps) = $self->stop_timer('request');
my ($elapsed, $rps) = $self->profile('mojo.request');
$self->app->log->debug("$code $message (${elapsed}s, $rps/s).");
}
);
Expand Down Expand Up @@ -88,19 +77,13 @@ example for learning to build new plugins.
L<Mojolicious::Plugin::RequestTimer> implements the following helpers.
=head2 C<start_timer>
<% start_timer 'page'; %>
Start timer.
Note that this helper is EXPERIMENTAL and might change without warning!
=head2 C<stop_timer>
=head2 C<profile>
<%= stop_timer 'page' %>
<%= my ($elapsed, $rps) = stop_timer 'page'; %>
<% profile 'page'; %>
<%= profile 'page' %>
<%= my ($elapsed, $rps) = profile 'page'; %>
Stop timer and return elapsed time in seconds.
Start profile and return results.
Note that this helper is EXPERIMENTAL and might change without warning!
=head1 METHODS
Expand Down
8 changes: 4 additions & 4 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -44,7 +44,7 @@ get 'form/:test' => 'form';
put 'selection';

# GET /timed
get '/timed' => sub { shift->start_timer('page') };
get '/timed' => sub { shift->profile('page') };

# GET /rps
get '/rps';
Expand Down Expand Up @@ -407,9 +407,9 @@ __DATA__
%= end
@@ timed.html.ep
<%= stop_timer "page" %> seconds
<%= profile "page" %> seconds
@@ rps.html.ep
% start_timer 'foo';
% profile 'foo';
lalala
<%= (stop_timer('foo'))[0] %>s (<%= (stop_timer('foo'))[1] %>/s)
<%= (profile('foo'))[0] %>s (<%= (profile('foo'))[1] %>/s)

0 comments on commit 4cce790

Please sign in to comment.