examples/custom-middleware/lib/Example/Middleware/RequestTimer.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 2 / 2 Conditioncond 57.1% 4 / 7 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 88.0% 44 / 50 CC 5 SCAR 10.0
CC
5
Cov
92%
__ANON__
5.1
__ANON__
1.0
wrap
1.0
CRAP 2.7
« examples/flow-control/app.pl ↑ Summary examples/duplex-http-stream/app.pl »
1 package Example::Middleware::RequestTimer;
2 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Event-layer middleware (subclass PAGI::Middleware, implement wrap). It times
9 # the request and injects an X-Response-Time-Ms header by wrapping $send: when
10 # the response start event passes through, it appends the header. Wrapping the
11 # channel like this is how a middleware shapes the response without the handler
12 # knowing or caring.
13  
14 1 ▶sub wrap ($self, $app) {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
15 3 ▶ return async sub ($scope, $receive, $send) {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 3 my $t0 = Time::HiRes::time();
17  
18 6 ▶ my $timing_send = async sub ($event) {
Subroutine __ANON__: called CC=5 SCAR=16.4
19 6 ▶ if (ref $event eq 'HASH' && ($event->{type} // '') eq 'http.response.start') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq "HASH" and ($event->{"type"} // "") eq "http.response.start") 3 3 6
Condition60%3/5
ref $event eq "HASH" and ($event->{"type"} // "") eq "http.response.start"
!ll&&!rl&&r
033
$event->{"type"} // ""
l!l
60
MC/DC0%0/2
ref $event eq "HASH" and ($event->{"type"} // "") eq "http.response.start"
ref $event eq "HASH" $event->{"type"}
Truth table33%1/3
A and B
A= ref $event eq "HASH"B= $event->{"type"}
ABresult
0X0
100
111
20 3 my $ms = sprintf('%.1f', (Time::HiRes::time() - $t0) * 1000);
21 3 ▶ my @headers = (@{ $event->{headers} // [] }, ['x-response-time-ms', $ms]);
Condition50%1/2
$event->{"headers"} // []
l!l
30
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
Aresult
11
00
22 3 $event = { %$event, headers => \@headers };
23 }
24 6 await $send->($event);
25 3 };
26  
27 3 await $app->($scope, $receive, $timing_send);
28 1 };
29 }
30  
31 1 1;
« examples/flow-control/app.pl ↑ Summary examples/duplex-http-stream/app.pl »