]> Dogcows Code - chaz/p5-HTTP-AnyUA/blobdiff - lib/HTTP/AnyUA/Middleware/Runtime.pm
add middleware
[chaz/p5-HTTP-AnyUA] / lib / HTTP / AnyUA / Middleware / Runtime.pm
diff --git a/lib/HTTP/AnyUA/Middleware/Runtime.pm b/lib/HTTP/AnyUA/Middleware/Runtime.pm
new file mode 100644 (file)
index 0000000..ed1643c
--- /dev/null
@@ -0,0 +1,59 @@
+package HTTP::AnyUA::Middleware::Runtime;
+# ABSTRACT: Middleware to determine response time
+
+=head1 SYNOPSIS
+
+    $any_ua->apply_middleware('Runtime');
+
+=head1 DESCRIPTION
+
+This middleware adds a "runtime" field to the response, the value of which is the number of seconds
+it took to make the request and finish the response.
+
+=head1 SEE ALSO
+
+=for :list
+* L<HTTP::AnyUA::Middleware>
+
+=cut
+
+use warnings;
+use strict;
+
+our $VERSION = '9999.999'; # VERSION
+
+use parent 'HTTP::AnyUA::Middleware';
+
+use Time::HiRes;
+
+
+sub request {
+    my $self = shift;
+    my ($method, $url, $args) = @_;
+
+    my $start = [Time::HiRes::gettimeofday];
+
+    my $resp = $self->backend->request($method, $url, $args);
+
+    my $handle_response = sub {
+        my $resp = shift;
+
+        $resp->{runtime} = sprintf('%.6f', Time::HiRes::tv_interval($start));
+
+        return $resp;
+    };
+
+    if ($self->response_is_future) {
+        $resp->transform(
+            done => $handle_response,
+            fail => $handle_response,
+        );
+    }
+    else {
+        $resp = $handle_response->($resp);
+    }
+
+    return $resp;
+}
+
+1;
This page took 0.018619 seconds and 4 git commands to generate.