]> Dogcows Code - chaz/p5-HTTP-AnyUA/blob - lib/HTTP/AnyUA/Middleware/Runtime.pm
ed1643cb6cbba60da75751425fee764e9df07e65
[chaz/p5-HTTP-AnyUA] / lib / HTTP / AnyUA / Middleware / Runtime.pm
1 package HTTP::AnyUA::Middleware::Runtime;
2 # ABSTRACT: Middleware to determine response time
3
4 =head1 SYNOPSIS
5
6 $any_ua->apply_middleware('Runtime');
7
8 =head1 DESCRIPTION
9
10 This middleware adds a "runtime" field to the response, the value of which is the number of seconds
11 it took to make the request and finish the response.
12
13 =head1 SEE ALSO
14
15 =for :list
16 * L<HTTP::AnyUA::Middleware>
17
18 =cut
19
20 use warnings;
21 use strict;
22
23 our $VERSION = '9999.999'; # VERSION
24
25 use parent 'HTTP::AnyUA::Middleware';
26
27 use Time::HiRes;
28
29
30 sub request {
31 my $self = shift;
32 my ($method, $url, $args) = @_;
33
34 my $start = [Time::HiRes::gettimeofday];
35
36 my $resp = $self->backend->request($method, $url, $args);
37
38 my $handle_response = sub {
39 my $resp = shift;
40
41 $resp->{runtime} = sprintf('%.6f', Time::HiRes::tv_interval($start));
42
43 return $resp;
44 };
45
46 if ($self->response_is_future) {
47 $resp->transform(
48 done => $handle_response,
49 fail => $handle_response,
50 );
51 }
52 else {
53 $resp = $handle_response->($resp);
54 }
55
56 return $resp;
57 }
58
59 1;
This page took 0.037009 seconds and 3 git commands to generate.