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