File Coverage

blib/lib/Benchmark/Apps.pm
Criterion Covered Total %
statement 9 44 20.4
branch 0 18 0.0
condition n/a
subroutine 3 9 33.3
pod 3 3 100.0
total 15 74 20.2


line stmt bran cond sub pod time code
1             package Benchmark::Apps;
2              
3 1     1   22538 use warnings;
  1         3  
  1         32  
4 1     1   6 use strict;
  1         2  
  1         41  
5              
6 1     1   1063 use Time::HiRes qw.gettimeofday tv_interval.;
  1         2198  
  1         5  
7              
8             =head1 NAME
9              
10             Benchmark::Apps - Simple interface to benchmark applications.
11              
12             =cut
13              
14             our $VERSION = '0.04';
15              
16              
17             =head1 SYNOPSIS
18              
19             This module provides a simple interface to benchmark applications (not
20             necessarily Perl applications).
21              
22             use Benchmark::Apps;
23              
24             my $commands = {
25             cmd1 => 'run_command_1 with arguments',
26             cmd2 => 'run_command_2 with other arguments',
27             };
28              
29             my $conf = { pretty_print=>1, iters=>5 };
30              
31             Benchmark:Apps::run( $commands, $conf );
32              
33             =head1 DESCRIPTION
34              
35             This module can be used to perform simple benchmarks on programs. Basically,
36             it can be used to benchmark any program that can be called with a system
37             call.
38              
39             =head1 FUNCTIONS
40              
41             =head2 run
42              
43             This method is used to run benchmarks. It runs the commands described in
44             the hash passed as argument. It returns an hash of the results each command.
45             A second hash reference can be passed to this method: a configuration
46             hash reference. The values passed in this hash override the default
47             behaviour of the run method. The configuration options available at this
48             moment are:
49              
50             =over 4
51              
52             =item C
53              
54             When enabled it will print to stdout, in a formatted way the results
55             of the benchmarks as they finish running. This option should de used
56             when you want to run benchmarks and want to see the results progress
57             as the tests run. You can disable it, so you can perform automated
58             benchmarks.
59              
60             Options: true (1) or false (0)
61              
62             Default: false (0)
63              
64             =item C
65              
66             This is the number of iterations that each test will run.
67              
68             Options: integer greater than 1
69              
70             Default: 5
71              
72             =item C
73              
74             This is a reference to an anonymous function that will calculate the
75             command argument based on the iteraction number.
76              
77             Options: any function reference that returns a string
78              
79             Default: empty function: always returns an empty string, which means no
80             arguments will be given to the command
81              
82             =back
83              
84             =head2 run
85              
86             This method runs the commands described in the hash passed as argument.
87             It returns an hash of the results and return codes for each command.
88              
89             =cut
90              
91 0     0     sub _empty { '' }
92              
93             my %cfg = ( pretty_print => 1,
94             iters => 5 ,
95             args => \&_empty );
96             my %command = ();
97             my %res = ();
98              
99             sub run {
100 0     0 1   my @args = @_;
101              
102 0 0         @args == 0 and die 'At least one hash reference needs to be passed as argument';
103 0 0         @args > 2 and die 'A maximum of two arguments (hash refs) should be passed to this function';
104             # in case we got the second argument (configuration hash ref)
105 0 0         if (@args > 1) {
106 0 0         if (ref $args[1] eq 'HASH') {
107 0           my @l = keys %{$args[1]};
  0            
108 0           foreach (@l) {
109 0 0         if (defined $args[1]{$_}) { # XXX and validate args
110 0           $cfg{$_} = $args[1]{$_};
111             }
112             }
113             }
114 0           else { warn 'Second argument to run should be an hash ref'; }
115             }
116              
117 0           %command = %{$args[0]};
  0            
118              
119 0           for my $iter (1..$cfg{'iters'}) {
120 0           for my $c (keys %command) {
121 0           $res{$c}{'run'} = $command{$c};
122 0           my $time = time_this($command{$c}.' '.&{$cfg{'args'}}($iter));
  0            
123 0           $res{$c}{'result'}{$iter} = $time;
124             }
125             }
126              
127 0 0         pretty_print(%res) if $cfg{'pretty_print'};
128              
129 0           return +{%res};
130             }
131              
132             sub _validate_option {
133 0     0     my ($option, $value) = @_;
134              
135             # TODO do some validations
136             # everything ok for now
137              
138 0           return 1;
139             }
140              
141             =head2 pretty_print
142              
143             This method is used to print the final result to STDOUT before returning
144             from the C method.
145              
146             =cut
147              
148             sub pretty_print {
149 0     0 1   my $self = shift;
150              
151 0           for my $iter (1..$cfg{'iters'}) {
152 0           _show_iter($iter);
153              
154 0           for my $c (keys %command) {
155 0           printf " %8s => %8.4f s\n", $c, $res{$c}{'result'}{$iter};
156             }
157             }
158             }
159              
160             sub _show_iter {
161 0     0     my $i = shift;
162 0 0         printf "%d%s iteration:\n", $i, $i==1?"st":$i==2?"nd":$i==3?"rd":"th";
    0          
    0          
163             }
164              
165             =head2 time_this
166              
167             This method is not meant to be used directly, although it can be useful.
168             It receives a command line and executes it via system, taking care
169             of registering the elapsed time.
170              
171             =cut
172              
173             sub time_this {
174 0     0 1   my $cmd_line = shift;
175 0           my $start_time = [gettimeofday];
176 0           system("$cmd_line 2>&1 > /dev/null");
177 0           return tv_interval($start_time);
178             }
179              
180              
181             =head1 EXAMPLES
182              
183             Check files in C.
184              
185             =head1 AUTHOR
186              
187             Aberto Simoes (aka ambs), C<< >>
188             Nuno Carvalho (aka smash), C<< >>
189              
190             =head1 BUGS
191              
192             Please report any bugs or feature requests to C, or through
193             the web interface at L. I will be notified, and then you'll
194             automatically be notified of progress on your bug as I make changes.
195              
196             =head1 SUPPORT
197              
198             You can find documentation for this module with the perldoc command.
199              
200             perldoc Benchmark::Apps
201              
202             You can also look for information at:
203              
204             =over 4
205              
206             =item * RT: CPAN's request tracker
207              
208             L
209              
210             =item * AnnoCPAN: Annotated CPAN documentation
211              
212             L
213              
214             =item * CPAN Ratings
215              
216             L
217              
218             =item * Search CPAN
219              
220             L
221              
222             =back
223              
224             =head1 COPYRIGHT & LICENSE
225              
226             Copyright 2008 Aberto Simoes, Nuno Carvalho, all rights reserved.
227              
228             This program is free software; you can redistribute it and/or modify it
229             under the same terms as Perl itself.
230              
231              
232             =cut
233              
234             !!1; # End of Benchmark::Apps
235              
236             __END__