| 1 | # $Id: /mirror/gungho/lib/Gungho/Plugin/Statistics.pm 4238 2007-10-29T15:08:17.605700Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endework.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Plugin::Statistics; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Plugin); | ||
| 10 | |||
| 11 | __PACKAGE__->mk_accessors($_) for qw(storage dump_interval _next_update); | ||
| 12 | |||
| 13 | sub setup | ||
| 14 | { | ||
| 15 | my ($self, $c) = @_; | ||
| 16 | |||
| 17 | # $self->dump_interval( $self->config->{dump_interval} || 60 ); | ||
| 18 | |||
| 19 | # Create the storage | ||
| 20 | $self->_setup_storage($c); | ||
| 21 | |||
| 22 | $c->register_hook( | ||
| 23 | # 'engine.end_loop' => sub { $self->dump_statistics(@_) }, | ||
| 24 | 'engine.send_request' => sub { $self->log_start_request(@_) }, | ||
| 25 | 'engine.handle_response' => sub { $self->log_end_request(@_) }, | ||
| 26 | ); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _setup_storage | ||
| 30 | { | ||
| 31 | my ($self, $c) = @_; | ||
| 32 | |||
| 33 | my $config = $self->config->{storage} || {}; | ||
| 34 | $config->{module} ||= 'SQLite'; | ||
| 35 | my $pkg = $c->load_gungho_module( $config->{module}, 'Plugin::Statistics::Storage'); | ||
| 36 | my $storage = $pkg->new(config => $self->config); | ||
| 37 | $storage->setup($c); | ||
| 38 | $self->storage($storage); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub log_start_request | ||
| 42 | { | ||
| 43 | my ($self, $c, $data) = @_; | ||
| 44 | $self->storage->incr("active_requests"); | ||
| 45 | foreach my $name qw(active_requests finished_requests) { | ||
| 46 | my $value = $self->storage->get($name); | ||
| 47 | print STDERR $name, " = ", defined $value ? $value : '(undef)', "\n"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | sub log_end_request | ||
| 52 | { | ||
| 53 | my ($self, $c, $data) = @_; | ||
| 54 | $self->storage->decr("active_requests"); | ||
| 55 | $self->storage->incr("finished_requests"); | ||
| 56 | |||
| 57 | foreach my $name qw(active_requests finished_requests) { | ||
| 58 | my $value = $self->storage->get($name); | ||
| 59 | print STDERR $name, " = ", defined $value ? $value : '(undef)', "\n"; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | # sub dump_statistics | ||
| 64 | # { | ||
| 65 | # my ($self, $c) = @_; | ||
| 66 | # | ||
| 67 | # if (time() <= ($self->_next_update || 0)) { | ||
| 68 | # $c->log->debug("Dumping stats"); | ||
| 69 | # $self->_next_update(time() + $self->dump_interval); | ||
| 70 | # } | ||
| 71 | # } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ |