File Coverage

blib/lib/Plack/ServerTiming.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition 2 2 100.0
subroutine 8 8 100.0
pod 3 3 100.0
total 36 36 100.0


line stmt bran cond sub pod time code
1             package Plack::ServerTiming;
2 3     3   185112 use strict;
  3         25  
  3         70  
3 3     3   12 use warnings;
  3         5  
  3         401  
4              
5             sub new {
6 2     2 1 97 my ($class, $env) = @_;
7 2         10 return bless {
8             env => $env,
9             } => $class;
10             }
11              
12             sub record_timing {
13 4     4 1 23 my ($self, $name, $field) = @_;
14 4   100     15 $field ||= {};
15              
16 4         4 push @{ $self->{env}->{'psgix.server-timing'} } => [$name, $field];
  4         15  
17             }
18              
19             sub guard {
20 1     1 1 7 my $self = shift;
21 1         9 return Plack::ServerTiming::Guard->new($self->{env}, @_);
22             }
23              
24             package # hide from pause
25             Plack::ServerTiming::Guard;
26 3     3   1342 use Time::HiRes;
  3         3324  
  3         10  
27              
28             sub new {
29 1     1   4 my ($class, $env, $name, $desc) = @_;
30 1         18 return bless {
31             env => $env,
32             start => [Time::HiRes::gettimeofday],
33             name => $name,
34             desc => $desc,
35             }, $class;
36             }
37              
38             sub DESTROY {
39 1     1   1000129 my $self = shift;
40 1         20 my $dur = Time::HiRes::tv_interval($self->{start}) * 1000;
41 1         29 push @{ $self->{env}->{'psgix.server-timing'} } => [$self->{name}, {dur => $dur, desc => $self->{desc}}];
  1         14  
42             }
43              
44             1;
45             __END__