File Coverage

blib/lib/Plack/Middleware/ProcessTimes.pm
Criterion Covered Total %
statement 38 38 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Plack::Middleware::ProcessTimes;
2             $Plack::Middleware::ProcessTimes::VERSION = '1.000000';
3 1     1   2582 use strict;
  1         3  
  1         28  
4 1     1   4 use warnings;
  1         2  
  1         33  
5              
6             # ABSTRACT: Include process times of a request in the Plack env
7              
8 1     1   21 use Time::HiRes qw(time);
  1         2  
  1         10  
9 1     1   140 use parent 'Plack::Middleware';
  1         2  
  1         7  
10 1     1   59 use Plack::Util::Accessor qw( measure_children );
  1         1  
  1         12  
11              
12             sub call {
13 2     2 1 28814 my ($self, $env) = @_;
14              
15 2         24 my @times = (time, times);
16              
17 2         11 my $res = $self->app->($env);
18              
19             return $self->response_cb($res, sub{
20 2     2   35 my $inner = shift;
21              
22 2 100       7 if ($self->measure_children) {
23 1         16 1 while waitpid(-1, 1) > 0;
24             }
25              
26 2         20 @times = map { $_ - shift @times } time, times;
  10         21  
27              
28 2         4 my $CPU = 0;
29 2         11 $CPU += $times[$_] for 1..4;
30 2         4 push @times, $CPU;
31              
32 2         3 @times = map { sprintf "%.3f", $_ } @times;
  12         68  
33              
34 2         7 $env->{'pt.real'} = $times[0];
35 2         10 $env->{'pt.cpu-user'} = $times[1];
36 2         4 $env->{'pt.cpu-sys'} = $times[2];
37              
38 2 100       6 if ($self->measure_children) {
39 1         6 $env->{'pt.cpu-cuser'} = $times[3];
40 1         2 $env->{'pt.cpu-csys'} = $times[4];
41             } else {
42 1         8 $env->{'pt.cpu-cuser'} = '-';
43 1         3 $env->{'pt.cpu-csys'} = '-';
44             };
45              
46 2         5 return;
47 2         90 });
48             }
49              
50             1;
51              
52             __END__