File Coverage

lib/Egg/Util/BenchMark.pm
Criterion Covered Total %
statement 9 30 30.0
branch n/a
condition 0 5 0.0
subroutine 3 7 42.8
pod 3 3 100.0
total 15 45 33.3


line stmt bran cond sub pod time code
1             package Egg::Util::BenchMark;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: BenchMark.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 1     1   413 use strict;
  1         3  
  1         32  
8 1     1   5 use warnings;
  1         3  
  1         31  
9 1     1   5087 use Time::HiRes qw/ gettimeofday tv_interval /;
  1         2354  
  1         6  
10              
11             our $VERSION= '3.00';
12              
13             sub new {
14 0     0 1   my($class, $e)= @_;
15 0           my $self= bless { e=> $e, report=> [] }, $class;
16 0           $self->_settime;
17 0           $self;
18             }
19             sub stock {
20 0     0 1   my($self, $label)= @_;
21 0   0       my $elapsed= tv_interval ($self->{is_time} || $self->_settime);
22 0   0       push @{$self->{report}}, [$label, ($elapsed || 0)];
  0            
23 0           $self->_settime;
24             }
25             sub finish {
26 0     0 1   my($self)= @_;
27 0           my($label, $elapsed);
28 0           my $format= "* %-18s : %3.6f sec.\n";
29 0           my $report= "# >> simple bench = -------------------\n";
30 0           my $total= 0;
31 0           for (@{$self->{report}}) {
  0            
32 0           $total+= $_->[1];
33 0           $report.= sprintf $format, @$_;
34             }
35 0           $report.= sprintf $format, ('======= Total >>', $total);
36 0           $self->{e}->debug_out
37             ("${report}# -------------------------------------\n");
38             }
39             sub _settime {
40 0     0     $_[0]->{is_time}= [gettimeofday];
41             }
42              
43             1;
44              
45             __END__
46              
47             =head1 NAME
48              
49             Egg::Util::BenchMark - Easy bench mark class for Egg.
50              
51             =head1 SYNOPSIS
52              
53             my $bench= Egg::Util::BenchMark->new($e);
54            
55             $bench->stock('start');
56             $bench->stock('run');
57             $bench->stock('end');
58            
59             $bench->finish;
60            
61             # ¥×¥í¥¸¥§¥¯¥È¤«¤é»È¤¦¤Ê¤é
62             $e->bench('start');
63             $e->bench('run');
64             $e->bench('end');
65              
66             =head1 DESCRIPTION
67              
68             It is an easy bench mark class used with Debaccmord of Egg.
69              
70             Egg takes the bench mark at each the following method calls while operating by
71             debug mode.
72              
73             =over 4
74              
75             =item * _prepare
76              
77             =item * _dispatch
78              
79             =item * _action_start
80              
81             =item * _action_end
82              
83             =item * _finalize
84              
85             =item * _output
86              
87             =item * _finish
88              
89             =back
90              
91             And, it totals at the end and the report is output by $e-E<gt>debug_out.
92              
93             In addition, $e-E<gt>bench(LABEL_STRING) comes to be reported about the application
94             to take the bench mark in detail including the result when putting it at every step.
95              
96             When debug mode becomes invalid, arranged $e-E<gt>bench need not be especially
97             excluded because it is changed to the method of not doing anything.
98              
99             This module is set up by L<Egg::Util::Debug>. To use other bench mark classes,
100             it is set to environment variable EGG_BENCH_CLASS.
101              
102             =head1 METHODS
103              
104             =head2 new
105              
106             Constructor.
107              
108             my $bench= Egg::Util::BenchMark->new;
109              
110             =head2 stock
111              
112             The bench mark when called is recorded.
113              
114             $bench->stock('start');
115             $bench->stock('end');
116              
117             =head2 finish
118              
119             The data recorded by stock is totaled and the report is returned.
120              
121             print STDERR $bench->finish;
122              
123             =head2 SEE ALSO
124              
125             L<Egg::Release>,
126             L<Egg::Util::Debug>,
127             L<Time::HiRes>,
128              
129             =head1 AUTHOR
130              
131             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
136              
137             This library is free software; you can redistribute it and/or modify
138             it under the same terms as Perl itself, either Perl version 5.8.6 or,
139             at your option, any later version of Perl 5 you may have available.
140              
141             =cut
142