Skip to content

Commit

Permalink
rename start to begin and add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 11, 2018
1 parent 45e46fe commit 9e30e69
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

7.65 2018-02-10
- Added EXPERIMENTAL timing->elapsed, timing->server_timing and timing->start
- Added EXPERIMENTAL timing->begin, timing->elapsed 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
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -128,7 +128,7 @@ sub dispatch {
my $method = $req->method;
my $path = $req->url->path->to_abs_string;
$self->log->debug(qq{$method "$path"});
$c->helpers->timing->start('mojo.timer');
$c->helpers->timing->begin('mojo.timer');
}

# Routes
Expand Down
45 changes: 24 additions & 21 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -42,9 +42,9 @@ sub register {
$app->helper('reply.exception' => sub { _development('exception', @_) });
$app->helper('reply.not_found' => sub { _development('not_found', @_) });

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

$app->helper(ua => sub { shift->app->ua });
}
Expand Down Expand Up @@ -155,6 +155,8 @@ sub _static {
return !$c->helpers->reply->not_found;
}

sub _timing_begin { shift->stash->{'mojo.timing'}{shift()} = [gettimeofday] }

sub _timing_elapsed {
my ($c, $name) = @_;
return undef unless my $started = $c->stash->{'mojo.timing'}{$name};
Expand All @@ -169,11 +171,6 @@ sub _timing_server_timing {
$c->res->headers->append('Server-Timing' => $value);
}

sub _timing_start {
my ($c, $name) = @_;
$c->stash->{'mojo.timing'}{$name} = [gettimeofday];
}

sub _url_with {
my $c = shift;
return $c->url_for(@_)->query($c->req->url->query->clone);
Expand Down Expand Up @@ -485,16 +482,23 @@ Alias for L<Mojolicious::Controller/"stash">.
%= stash('name') // 'Somebody'
=head2 timing->begin
$c->timing->begin('foo');
Create named timestamp. Note that this helper is EXPERIMENTAL and might change
without warning!
=head2 timing->elapsed
my $elapsed = $c->timing->elapsed('foo');
Return fractional number of seconds since named timstamp has been created with
L</"timing-E<gt>start"> or C<undef> if no such timestamp exists. Note that this
helper is EXPERIMENTAL and might change without warning!
Return fractional amount of time in seconds since named timstamp has been
created with L</"timing-E<gt>begin"> or C<undef> if no such timestamp exists.
Note that this helper is EXPERIMENTAL and might change without warning!
# Log timing information
$c->timing->start('database_stuff');
$c->timing->begin('database_stuff');
...
my $elapsed = $c->timing->elapsed('database_stuff');
$c->app->log->debug("Database stuff took $elapsed seconds");
Expand All @@ -505,21 +509,20 @@ helper is EXPERIMENTAL and might change without warning!
$c->timing->server_timing('metric', 'Some Description');
$c->timing->server_timing('metric', 'Some Description', 'foo');
Create C<Server-Timing> header with or without named timestamp created with
L</"timing-E<gt>start">. Note that this helper is EXPERIMENTAL and might change
Create C<Server-Timing> header with optional description and time from
</"timing-E<gt>elapsed">. Note that this helper is EXPERIMENTAL and might change
without warning!
# Forward timing information to browser
$c->timing->start('database_stuff');
...
$c->timing->server_timing('db', 'Database Stuff', 'database_stuff');
=head2 timing->start
# "Server-Timing: miss"
$c->timing->server_timing('miss');
$c->timing->start('foo');
# "Server-Timing: dc;desc=atl"
$c->timing->server_timing('dc', 'atl');
Create named timestamp. Note that this helper is EXPERIMENTAL and might change
without warning!
# "Server-Timing: db;desc=Database;dur=0.0001"
$c->timing->begin('database_stuff');
...
$c->timing->server_timing('db', 'Database', 'database_stuff');
=head2 title
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/lite_app.t
Expand Up @@ -447,8 +447,8 @@ get '/dynamic/inline' => sub {

get '/timing' => sub {
my $c = shift;
$c->timing->start('foo');
$c->timing->start('bar');
$c->timing->begin('foo');
$c->timing->begin('bar');
usleep 1000;
my $foo = $c->timing->elapsed('foo');
my $bar = $c->timing->elapsed('bar');
Expand Down

0 comments on commit 9e30e69

Please sign in to comment.