Skip to content

Commit

Permalink
add EXPERIMENTAL timing->rps helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 11, 2018
1 parent 9e30e69 commit 5d39cd6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Changes
@@ -1,7 +1,7 @@

7.65 2018-02-10
- Added EXPERIMENTAL timing->begin, timing->elapsed and timing->server_timing
helpers to Mojolicious::Plugin::DefaultHelpers.
7.65 2018-02-11
- Added EXPERIMENTAL timing->begin, timing->elapsed, timing->rps and
timing->server_timing helpers to Mojolicious::Plugin::DefaultHelpers.
- Added EXPERIMENTAL server_timing method to Mojo::Headers.
- Added support for new HTTP status code.

Expand Down
7 changes: 4 additions & 3 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -200,9 +200,10 @@ sub rendered {
if (!$stash->{'mojo.finished'} && ++$stash->{'mojo.finished'}) {

# Disable auto rendering and stop timer
my $app = $self->render_later->app;
if (defined(my $elapsed = $self->helpers->timing->elapsed('mojo.timer'))) {
my $rps = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
my $app = $self->render_later->app;
my $timing = $self->helpers->timing;
if (defined(my $elapsed = $timing->elapsed('mojo.timer'))) {
my $rps = $timing->rps($elapsed) // '??';
my $code = $res->code;
my $msg = $res->message || $res->default_message($code);
$app->log->debug("$code $msg (${elapsed}s, $rps/s)");
Expand Down
23 changes: 21 additions & 2 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -44,6 +44,7 @@ sub register {

$app->helper('timing.begin' => \&_timing_begin);
$app->helper('timing.elapsed' => \&_timing_elapsed);
$app->helper('timing.rps' => \&_timing_rps);
$app->helper('timing.server_timing' => \&_timing_server_timing);

$app->helper(ua => sub { shift->app->ua });
Expand Down Expand Up @@ -163,6 +164,8 @@ sub _timing_elapsed {
return tv_interval($started, [gettimeofday()]);
}

sub _timing_rps { $_[1] == 0 ? undef : sprintf '%.3f', 1 / $_[1] }

sub _timing_server_timing {
my ($c, $metric, $desc, $name) = @_;
my $value = $metric;
Expand Down Expand Up @@ -486,8 +489,8 @@ Alias for L<Mojolicious::Controller/"stash">.
$c->timing->begin('foo');
Create named timestamp. Note that this helper is EXPERIMENTAL and might change
without warning!
Create named timestamp for L<"timing-E<gt>elapsed">. Note that this helper is
EXPERIMENTAL and might change without warning!
=head2 timing->elapsed
Expand All @@ -503,6 +506,22 @@ Note that this helper is EXPERIMENTAL and might change without warning!
my $elapsed = $c->timing->elapsed('database_stuff');
$c->app->log->debug("Database stuff took $elapsed seconds");
=head2 timing->rps
my $rps = $c->timing->rps('0.001');
Return fractional number of requests that could be performed in one second if
every singe one took the given amount of time in seconds or C<undef> if the
number is too low. Note that this helper is EXPERIMENTAL and might change
without warning!
# Log more timing information
$c->timing->begin('web_stuff');
...
my $elapsed = $c->timing->elapsed('web_stuff');
my $rps = $c->timing->rps($elapsed);
$c->app->log->debug("Web stuff took $elapsed seconds ($rps per second)");
=head2 timing->server_timing
$c->timing->server_timing('metric');
Expand Down
5 changes: 3 additions & 2 deletions t/mojolicious/lite_app.t
Expand Up @@ -456,7 +456,8 @@ get '/timing' => sub {
$c->timing->server_timing('dc', 'atl');
$c->timing->server_timing('test', 'Some Test', 'foo');
$c->timing->server_timing('app', undef, 'foo');
$c->render(text => "Foo: $foo, Bar: $bar");
my $rps = $c->timing->rps($bar);
$c->render(text => "Foo: $foo, Bar: $bar ($rps)");
};

my $t = Test::Mojo->new;
Expand Down Expand Up @@ -1047,7 +1048,7 @@ $t->get_ok('/dynamic/inline')->status_is(200)->content_is("dynamic inline 2\n");
$t->get_ok('/timing')->status_is(200)
->header_like('Server-Timing' =>
qr/miss, dc;desc="atl", test;desc="Some Test";dur=[0-9.]+, app;dur=[0-9.]+/)
->content_like(qr/Foo: [0-9.]+, Bar: [0-9.]+/);
->content_like(qr/Foo: [0-9.]+, Bar: [0-9.]+ \([0-9.?]+\)/);

done_testing();

Expand Down

0 comments on commit 5d39cd6

Please sign in to comment.