Newer Older
79 lines | 1.997kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojolicious::Plugin::RequestTimer;
2
use Mojo::Base 'Mojolicious::Plugin';
3

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
4
use Time::HiRes qw(gettimeofday tv_interval);
copy gitweblite soruce code
root authored on 2012-11-23
5

            
6
sub register {
7
  my ($self, $app) = @_;
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
8
  $app->hook(before_routes  => \&_start);
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
9
  $app->hook(after_dispatch => \&_end);
10
}
11

            
12
sub _end {
13
  my $self = shift;
14

            
15
  # Ignore static files
16
  my $stash = $self->stash;
17
  return unless my $started = delete $stash->{'mojo.started'};
18
  return if $stash->{'mojo.static'};
19
  my $elapsed = sprintf '%f',
20
    Time::HiRes::tv_interval($started, [Time::HiRes::gettimeofday()]);
21
  my $rps  = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
22
  my $res  = $self->res;
23
  my $code = $res->code || 200;
24
  my $msg  = $res->message || $res->default_message($code);
25
  $self->app->log->debug("$code $msg (${elapsed}s, $rps/s).");
26
}
27

            
28
sub _start {
29
  my $self = shift;
30

            
31
  # Ignore static files
32
  my $stash = $self->stash;
33
  return if $stash->{'mojo.static'} || $stash->{'mojo.started'};
34
  my $req    = $self->req;
35
  my $method = $req->method;
36
  my $path   = $req->url->path->to_abs_string;
37
  my $ua     = $req->headers->user_agent || 'Anonymojo';
38
  $self->app->log->debug("$method $path ($ua).");
39
  $stash->{'mojo.started'} = [Time::HiRes::gettimeofday()];
copy gitweblite soruce code
root authored on 2012-11-23
40
}
41

            
42
1;
43

            
44
=head1 NAME
45

            
46
Mojolicious::Plugin::RequestTimer - Request timer plugin
47

            
48
=head1 SYNOPSIS
49

            
50
  # Mojolicious
51
  $self->plugin('RequestTimer');
52

            
53
  # Mojolicious::Lite
54
  plugin 'RequestTimer';
55

            
56
=head1 DESCRIPTION
57

            
58
L<Mojolicious::Plugin::RequestTimer> is a plugin to gather and log request
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
59
timing information.
60

            
61
This is a core plugin, that means it is always enabled and its code a good
62
example for learning to build new plugins, you're welcome to fork it.
copy gitweblite soruce code
root authored on 2012-11-23
63

            
64
=head1 METHODS
65

            
66
L<Mojolicious::Plugin::RequestTimer> inherits all methods from
67
L<Mojolicious::Plugin> and implements the following new ones.
68

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
69
=head2 register
copy gitweblite soruce code
root authored on 2012-11-23
70

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
71
  $plugin->register(Mojolicious->new);
copy gitweblite soruce code
root authored on 2012-11-23
72

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
73
Register hooks in L<Mojolicious> application.
copy gitweblite soruce code
root authored on 2012-11-23
74

            
75
=head1 SEE ALSO
76

            
77
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
78

            
79
=cut