lib/PAGI/Middleware/Runtime.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 4 / 4 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 100% 58 / 58 CC 5 SCAR 5.7
CC
5
Cov
100%
_init
3.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.8
« blib/lib/PAGI/Middleware/Session/State/Bearer.pm ↑ Summary blib/lib/PAGI/Context/SSE.pm »
1 package PAGI::Middleware::Runtime;
2 1 $PAGI::Middleware::Runtime::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 PAGI::Middleware::Runtime - Request timing middleware
12  
13 =head1 SYNOPSIS
14  
15 use PAGI::Middleware::Builder;
16  
17 my $app = builder {
18 enable 'Runtime',
19 header => 'X-Runtime',
20 precision => 6;
21 $my_app;
22 };
23  
24 =head1 DESCRIPTION
25  
26 PAGI::Middleware::Runtime measures the time taken to process a request
27 and adds it as a response header. This is useful for performance
28 monitoring and debugging.
29  
30 =head1 CONFIGURATION
31  
32 =over 4
33  
34 =item * header (default: 'X-Runtime')
35  
36 The header name to use for the runtime value.
37  
38 =item * precision (default: 6)
39  
40 Number of decimal places for the duration in seconds.
41  
42 =back
43  
44 =cut
45  
46 3 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
47 3 my ($self, $config) = @_;
48  
49 3 ▶ $self->{header} = $config->{header} // 'X-Runtime';
Condition100%2/2
$config->{"header"} // "X-Runtime"
l!l
12
MC/DC100%1/1
$config->{"header"} // "X-Runtime"
$config->{"header"}
Truth table100%2/2
A
A= $config->{"header"}
Aresult
11
00
50 3 ▶ $self->{precision} = $config->{precision} // 6;
Condition100%2/2
$config->{"precision"} // 6
l!l
12
MC/DC100%1/1
$config->{"precision"} // 6
$config->{"precision"}
Truth table100%2/2
A
A= $config->{"precision"}
Aresult
11
00
51 }
52  
53 3 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
54 3 my ($self, $app) = @_;
55  
56 3 ▶ return async sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
57 3 my ($scope, $receive, $send) = @_;
58 # Only handle HTTP requests
59 3 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 1 2 3
60 1 await $app->($scope, $receive, $send);
61 1 return;
62 }
63  
64 2 my $start_time = time();
65  
66 # Intercept send to add runtime header
67 4 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 4 my ($event) = @_;
69 4 ▶ if ($event->{type} eq 'http.response.start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.response.start") 2 2 4
70 2 my $duration = time() - $start_time;
71 2 my $formatted = sprintf('%.*f', $self->{precision}, $duration);
72 2 push @{$event->{headers}}, [$self->{header}, $formatted];
73 }
74 4 await $send->($event);
75 2 };
76  
77 2 await $app->($scope, $receive, $wrapped_send);
78 3 };
79 }
80  
81 1 1;
82  
83 __END__
« blib/lib/PAGI/Middleware/Session/State/Bearer.pm ↑ Summary blib/lib/PAGI/Context/SSE.pm »