Skip to content

Commit

Permalink
removed profile helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 1, 2011
1 parent 087ed7d commit 7816434
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.21 2011-11-01 00:00:00
2.21 2011-11-02 00:00:00
- Removed profile helper.
- Updated CSS4 selectors in Mojo::DOM::CSS with changes from the
latest editor's draft.
- Improved Mojo::ByteStream to generate most Mojo::Util based methods
Expand Down
37 changes: 9 additions & 28 deletions lib/Mojolicious/Plugin/RequestTimer.pm
Expand Up @@ -8,30 +8,20 @@ use Time::HiRes qw/gettimeofday tv_interval/;
sub register {
my ($self, $app) = @_;

# Add "profile" helper
$app->helper(
profile => sub {
my ($self, $name) = @_;
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;
}
);

# Start timer
$app->hook(
after_static_dispatch => sub {
my $self = shift;

# Ignore static files
return if $self->stash->{'mojo.static'};
my $stash = $self->stash;
return if $stash->{'mojo.static'} || $stash->{'mojo.started'};
my $req = $self->req;
my $method = $req->method;
my $path = $req->url->path->to_abs_string;
my $ua = $req->headers->user_agent || 'Anonymojo';
$self->app->log->debug("$method $path ($ua).");
$self->profile('mojo.request');
$stash->{'mojo.started'} = [Time::HiRes::gettimeofday()];
}
);

Expand All @@ -41,11 +31,15 @@ sub register {
my $self = shift;

# Ignore static files
return if $self->stash->{'mojo.static'};
my $stash = $self->stash;
return unless my $started = delete $stash->{'mojo.started'};
return if $stash->{'mojo.static'};
my $elapsed = sprintf '%f',
Time::HiRes::tv_interval($started, [Time::HiRes::gettimeofday()]);
my $rps = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
my $res = $self->res;
my $code = $res->code || 200;
my $message = $res->message || $res->default_message($code);
my ($elapsed, $rps) = $self->profile('mojo.request');
$self->app->log->debug("$code $message (${elapsed}s, $rps/s).");
}
);
Expand Down Expand Up @@ -73,19 +67,6 @@ timing information.
This is a core plugin, that means it is always enabled and its code a good
example for learning to build new plugins.
=head1 HELPERS
L<Mojolicious::Plugin::RequestTimer> implements the following helpers.
=head2 C<profile>
% profile 'page';
%= profile 'page'
% my ($elapsed, $rps) = profile 'page';
Start profile and return results.
Note that this helper is EXPERIMENTAL and might change without warning!
=head1 METHODS
L<Mojolicious::Plugin::RequestTimer> inherits all methods from
Expand Down
23 changes: 1 addition & 22 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 57;
use Test::More tests => 51;

# "Hey! Bite my glorious golden ass!"
use Mojolicious::Lite;
Expand Down Expand Up @@ -43,12 +43,6 @@ get 'form/:test' => 'form';
# PUT /selection
put 'selection';

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

# GET /rps
get '/rps';

my $t = Test::Mojo->new;

# GET /tags
Expand Down Expand Up @@ -302,13 +296,6 @@ $t->put_ok('/selection?foo=bar&a=e&foo=baz&bar=d')->status_is(200)
. '</form>'
. "\n");

# GET /timed
$t->get_ok('/timed')->status_is(200)->content_like(qr/\d+\.\d+\ seconds/);

# GET /rps
$t->get_ok('/rps')->status_is(200)
->content_like(qr#^lalala\n\d+\.\d+s\ \(\d+\.\d+/s\)#);

__DATA__
@@ tags.html.ep
<%= tag 'foo' %>
Expand Down Expand Up @@ -405,11 +392,3 @@ __DATA__
%= select_field bar => [['D' => 'd', disabled => 'disabled'], 'baz']
%= submit_button
%= end
@@ timed.html.ep
<%= profile "page" %> seconds
@@ rps.html.ep
% profile 'foo';
lalala
<%= (profile('foo'))[0] %>s (<%= (profile('foo'))[1] %>/s)

0 comments on commit 7816434

Please sign in to comment.