blib/lib/Dancer/Plugin/TimeRequests.pm

filter: Statementstmt 39.7% 27 / 68 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 63.6% 7 / 11 Podpod n/a totaltotal 35.0% 34 / 97 CC 14 SCAR 37.1
CC
14
Cov
32%
_get_routes
72.0
__ANON__
56.0
__ANON__
2.0
CRAP 40.8
↑ Summary
1 package Dancer::Plugin::TimeRequests;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Dancer qw(:syntax);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::MoreUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.06';
12  
13 =head1 NAME
14  
15 Dancer::Plugin::TimeRequests - log how long requests take and which routes are slow
16  
17 =head1 DESCRIPTION
18  
19 A simple Dancer plugin to log how long each request took to process, and also to
20 gather stats on the average response time for each route - so you can see at a
21 glance which routes are taking longer than you'd like, therefore where you ought
22 to start looking to improve performance.
23  
24 Provides a statistics page giving you a list of your routes, along with their
25 response times.
26  
27  
28 =head1 SYNOPSIS
29  
30 In your Dancer app, load this module:
31  
32 use Dancer::Plugin::TimeRequests;
33  
34 Then, when your app is logging in debug mode, log messages will be generated
35 showing how logn each request took:
36  
37 Request to /foo completed in 4.0011 seconds in ....
38  
39 To see which routes are slow, hit the URL C</plugin-timerequests>.
40  
41 =cut
42  
43 1 my %request_times;
44  
45 0 ▶hook before => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 my $route_handler = shift;
47 0 var current_handler => $route_handler;
48 0 var request_start_time => Time::HiRes::time();
49 1 };
50  
51 0 ▶hook after => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 Dancer::Logger::debug(sprintf "Request to %s completed in %.4f seconds",
53 request->path,
54 Time::HiRes::time() - vars->{request_start_time}
55 0 );
56 0 push @{ $request_times{ vars->{current_handler} } },
57 0 Time::HiRes::time() - vars->{request_start_time};
58 1 };
59  
60 0 ▶get '/plugin-timerequests' => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
61 # Get the list of routes, and for each one, match up the coderef with our
62 # recorded times, and add the timing info, so we can then sort routes by
63 # average execution time to produce the output
64 0 my $routes = _get_routes();
65 0 for my $route (@$routes) {
66 0 my $route_times = $request_times{ $route->{route} };
67 0 ▶ next unless defined $route_times && scalar @$route_times;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $route_times and scalar @$route_times 0 0 0
Condition0%0/2
defined $route_times and scalar @$route_times
!ll
00
MC/DC0%0/1
defined $route_times and scalar @$route_times
defined $route_times
Truth table0%0/2
A
A= defined $route_times
Aresult
00
11
68  
69 0 my ($min, $max) = List::MoreUtils::minmax(@$route_times);
70 $route->{times} = {
71 0 avg => List::Util::sum(@$route_times) / @$route_times,
72 min => $min,
73 max => $max,
74 };
75 }
76  
77 # Now, we can loop through all routes, slowest first, and output the timing
78 # info
79 0 my $table = HTML::Table->new;
80 0 $table->addRow('Route pattern', 'Average', 'Best', 'Worst');
81 0 $table->setRowHead(1);
82 0 for my $route (
83 0 sort { $b->{times}{avg} <=> $a->{times}{avg} }
84 0 grep { exists $_->{times} } @$routes
85 ) {
86 0 ▶ next unless exists $route->{times};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $route->{"times"} 0 0 0
87 0 my $times = $route->{times};
88 $table->addRow(
89 $route->{pattern},
90 0 ▶ map { sprintf '%.3f', $_ || 0 } @$times{qw(avg min max)},
Condition0%0/2
$_ || 0
l!l
00
MC/DC0%0/1
$_ || 0
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
91 );
92 }
93 0 return $table->getTable;
94  
95 1 };
96  
97 # Fetch all routes defined. (Loosely based on code lovingly stolen with no
98 # shame from Dancer::Plugin::SiteMap - cheers James Ronan (JNRONAN)
99 # Returns an arrayref of hashrefs describing all routes (with keys pattern
100 # and handler)
101 0 ▶sub _get_routes {
Subroutine _get_routes: not called CC=8 SCAR=42.8
102 0 ▶ my $version = (exists &dancer_version) ? int( dancer_version() ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists &Dancer::Plugin::TimeRequests::dancer_version ? : 0 0 0
103 0 ▶ my @apps = ($version == 2) ? @{ runner->server->apps }
Branch: 1 branch
BranchTrueFalseTotal
$version == 2 ? : 0 0 0
104 : Dancer::App->applications;
105
106 0 my @routes;
107 0 for my $app ( @apps ) {
108 my $app_routes = ($version == 2) ? $app->routes
109 0 ▶ : $app->{registry}->{routes};
Branch: 1 branch
BranchTrueFalseTotal
$version == 2 ? : 0 0 0
110
111 0 for my $route_type (keys %$app_routes) {
112 0 for my $route (@{ $app_routes->{$route_type} }) {
113 0 my ($pattern, $handler);
114 0 ▶ if ($version == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 2) { } 0 0 0
115 0 $pattern = $route->spec_route;
116 0 $handler = $route->handler;
117 } else {
118 0 $pattern = $route->pattern;
119 0 $handler = $route->code;
120 }
121 0 push @routes, {
122 route => $route,
123 pattern => $pattern,
124 handler => $handler,
125 };
126 }
127 }
128 }
129 0 debug "list of routes being returned:", \@routes;
130 0 return \@routes;
131 }
132  
133  
134 =head1 AUTHOR
135  
136 David Precious, C<< <davidp at preshweb.co.uk> >>
137  
138 =head1 BUGS
139  
140 Please report any bugs or feature requests to C<bug-dancer-plugin-timerequests at rt.cpan.org>, or through
141 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Plugin-TimeRequests>. I will be notified, and then you'll
142 automatically be notified of progress on your bug as I make changes.
143  
144  
145  
146  
147 =head1 SUPPORT
148  
149 You can find documentation for this module with the perldoc command.
150  
151 perldoc Dancer::Plugin::TimeRequests
152  
153  
154 You can also look for information at:
155  
156 =over 4
157  
158 =item * RT: CPAN's request tracker
159  
160 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-TimeRequests>
161  
162 =item * AnnoCPAN: Annotated CPAN documentation
163  
164 L<http://annocpan.org/dist/Dancer-Plugin-TimeRequests>
165  
166 =item * CPAN Ratings
167  
168 L<http://cpanratings.perl.org/d/Dancer-Plugin-TimeRequests>
169  
170 =item * Search CPAN
171  
172 L<http://search.cpan.org/dist/Dancer-Plugin-TimeRequests/>
173  
174 =back
175  
176  
177 =head1 ACKNOWLEDGEMENTS
178  
179  
180 =head1 LICENSE AND COPYRIGHT
181  
182 Copyright 2011 David Precious.
183  
184 This program is free software; you can redistribute it and/or modify it
185 under the terms of either: the GNU General Public License as published
186 by the Free Software Foundation; or the Artistic License.
187  
188 See http://dev.perl.org/licenses/ for more information.
189  
190  
191 =cut
192  
193 1 1; # End of Dancer::Plugin::TimeRequests
↑ Summary