Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rename profile->* to timing->*
  • Loading branch information
kraih committed Feb 10, 2018
1 parent 80bd976 commit 99e86e7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Changes
@@ -1,7 +1,7 @@

7.65 2018-02-10
- Added EXPERIMENTAL profile->elapsed, profile->server_timing and
profile->start helpers to Mojolicious::Plugin::DefaultHelpers.
- Added EXPERIMENTAL timing->elapsed, timing->server_timing and timing->start
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->profile->start('mojo.timer');
$c->helpers->timing->start('mojo.timer');
}

# Routes
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -201,7 +201,7 @@ sub rendered {

# Disable auto rendering and stop timer
my $app = $self->render_later->app;
if (defined(my $elapsed = $self->helpers->profile->elapsed('mojo.timer'))) {
if (defined(my $elapsed = $self->helpers->timing->elapsed('mojo.timer'))) {
my $rps = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
my $code = $res->code;
my $msg = $res->message || $res->default_message($code);
Expand Down
60 changes: 30 additions & 30 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('profile.elapsed' => \&_profile_elapsed);
$app->helper('profile.server_timing' => \&_profile_server_timing);
$app->helper('profile.start' => \&_profile_start);
$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 @@ -148,30 +148,30 @@ sub _is_fresh {
return $c->app->static->is_fresh($c, \%options);
}

sub _profile_elapsed {
sub _static {
my ($c, $file) = @_;
return !!$c->rendered if $c->app->static->serve($c, $file);
$c->app->log->debug(qq{Static file "$file" not found});
return !$c->helpers->reply->not_found;
}

sub _timing_elapsed {
my ($c, $name) = @_;
return undef unless my $started = $c->stash->{'mojo.profile'}{$name};
return undef unless my $started = $c->stash->{'mojo.timing'}{$name};
return tv_interval($started, [gettimeofday()]);
}

sub _profile_server_timing {
sub _timing_server_timing {
my ($c, $metric, $desc, $name) = @_;
my $value = $metric;
$value .= qq{;desc="$desc"} if $desc;
if ($name && (my $d = _profile_elapsed($c, $name))) { $value .= ";dur=$d" }
if ($name && (my $d = _timing_elapsed($c, $name))) { $value .= ";dur=$d" }
$c->res->headers->append('Server-Timing' => $value);
}

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

sub _static {
my ($c, $file) = @_;
return !!$c->rendered if $c->app->static->serve($c, $file);
$c->app->log->debug(qq{Static file "$file" not found});
return !$c->helpers->reply->not_found;
$c->stash->{'mojo.timing'}{$name} = [gettimeofday];
}

sub _url_with {
Expand Down Expand Up @@ -485,38 +485,38 @@ Alias for L<Mojolicious::Controller/"stash">.
%= stash('name') // 'Somebody'
=head2 profile->elapsed
=head2 timing->elapsed
my $elapsed = $c->profile->elapsed('foo');
my $elapsed = $c->timing->elapsed('foo');
Return fractional number of seconds since named timstamp has been created with
L</"profile-E<gt>start"> or C<undef> if no such timestamp exists. Note that this
L</"timing-E<gt>start"> or C<undef> if no such timestamp exists. Note that this
helper is EXPERIMENTAL and might change without warning!
# Log profiling information
$c->profile->start('database_stuff');
$c->timing->start('database_stuff');
...
my $elapsed = $c->profile->elapsed('database_stuff');
my $elapsed = $c->timing->elapsed('database_stuff');
$c->app->log->debug("Database stuff took $elapsed seconds");
=head2 profile->server_timing
=head2 timing->server_timing
$c->profile->server_timing('metric');
$c->profile->server_timing('metric', 'Some Description');
$c->profile->server_timing('metric', 'Some Description', 'foo');
$c->timing->server_timing('metric');
$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</"profile-E<gt>start">. Note that this helper is EXPERIMENTAL and might change
L</"timing-E<gt>start">. Note that this helper is EXPERIMENTAL and might change
without warning!
# Forward profiling information to browser
$c->profile->start('database_stuff');
$c->timing->start('database_stuff');
...
$c->profile->server_timing('db', 'Database Stuff', 'database_stuff');
$c->timing->server_timing('db', 'Database Stuff', 'database_stuff');
=head2 profile->start
=head2 timing->start
$c->profile->start('foo');
$c->timing->start('foo');
Create named timestamp. Note that this helper is EXPERIMENTAL and might change
without warning!
Expand Down
20 changes: 10 additions & 10 deletions t/mojolicious/lite_app.t
Expand Up @@ -445,17 +445,17 @@ get '/dynamic/inline' => sub {
$c->render(inline => 'dynamic inline ' . $dynamic_inline++);
};

get '/profile' => sub {
get '/timing' => sub {
my $c = shift;
$c->profile->start('foo');
$c->profile->start('bar');
$c->timing->start('foo');
$c->timing->start('bar');
usleep 1000;
my $foo = $c->profile->elapsed('foo');
my $bar = $c->profile->elapsed('bar');
$c->profile->server_timing('miss');
$c->profile->server_timing('dc', 'atl');
$c->profile->server_timing('test', 'Some Test', 'foo');
$c->profile->server_timing('app', undef, 'foo');
my $foo = $c->timing->elapsed('foo');
my $bar = $c->timing->elapsed('bar');
$c->timing->server_timing('miss');
$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");
};

Expand Down Expand Up @@ -1044,7 +1044,7 @@ $t->get_ok('/dynamic/inline')->status_is(200)->content_is("dynamic inline 1\n");
$t->get_ok('/dynamic/inline')->status_is(200)->content_is("dynamic inline 2\n");

# Profiling
$t->get_ok('/profile')->status_is(200)
$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.]+/);
Expand Down

0 comments on commit 99e86e7

Please sign in to comment.