Skip to content

Commit

Permalink
make timing->* helpers a little less magical for now
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 11, 2018
1 parent 2bfdba6 commit 1a65c1f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
20 changes: 12 additions & 8 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -167,10 +167,10 @@ sub _timing_elapsed {
sub _timing_rps { $_[1] == 0 ? undef : sprintf '%.3f', 1 / $_[1] }

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

Expand Down Expand Up @@ -526,11 +526,10 @@ without warning!
$c->timing->server_timing('metric');
$c->timing->server_timing('metric', 'Some Description');
$c->timing->server_timing('metric', 'Some Description', 'foo');
$c->timing->server_timing('metric', 'Some Description', '0.001');
Create C<Server-Timing> header with optional description and time from
L</"timing-E<gt>elapsed">. Note that this helper is EXPERIMENTAL and might
change without warning!
Create C<Server-Timing> header with optional description and duration. Note that
this helper is EXPERIMENTAL and might change without warning!
# "Server-Timing: miss"
$c->timing->server_timing('miss');
Expand All @@ -541,7 +540,12 @@ change without warning!
# "Server-Timing: db;desc=Database;dur=0.0001"
$c->timing->begin('database_stuff');
...
$c->timing->server_timing('db', 'Database', 'database_stuff');
my $elapsed = $c->timing->elapsed('database_stuff');
$c->timing->server_timing('db', 'Database', $elapsed);
# "Server-Timing: miss, dc;desc=atl"
$c->timing->server_timing('miss');
$c->timing->server_timing('dc', 'atl');
=head2 title
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/lite_app.t
Expand Up @@ -454,8 +454,8 @@ get '/timing' => sub {
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->timing->server_timing('test', 'Some Test', '0.002');
$c->timing->server_timing('app', undef, '0.001');
my $rps = $c->timing->rps($bar);
$c->render(text => "Foo: $foo, Bar: $bar ($rps)");
};
Expand Down Expand Up @@ -1047,7 +1047,7 @@ $t->get_ok('/dynamic/inline')->status_is(200)->content_is("dynamic inline 2\n");
# Profiling
$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.]+/)
qr/miss, dc;desc="atl", test;desc="Some Test";dur=0.002, app;dur=0.001/)
->content_like(qr/Foo: [0-9.]+, Bar: [0-9.]+ \([0-9.?]+\)/);

done_testing();
Expand Down

0 comments on commit 1a65c1f

Please sign in to comment.