-
Notifications
You must be signed in to change notification settings - Fork 138
/
parrotbench.pl
265 lines (225 loc) · 7.36 KB
/
parrotbench.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#! perl
use strict;
use warnings;
use Config::IniFiles;
use File::Basename;
use File::Find;
use File::Spec;
use FindBin;
use Getopt::Long;
use Pod::Usage;
require POSIX;
=head1 NAME
parrotbench - Parrot benchmark
=head1 SYNOPSIS
parrotbench.pl [options]
Options:
-b -benchmarks use benchmarks matching regexes (multiple)
-c -conf path to configuration file
-d -directory path to benchmarks directory
-h -? -help display this help and exits
-list list available benchmarks and exits
-m -method method of time from times()
1 $cuser + $csystem from times() (default)
2 Real time using POSIX::times()
-n -nobench skip benchmarks matching regexes (multiple)
-time show times instead of percentage
=head1 DESCRIPTION
Benchmark Parrot against other interpreters.
=head1 CONFIGURATION
You must specify paths to executables in a configuration file.
That file may be placed as parrotbench.conf in the same directory
as parrotbench.pl or otherwise explicitly specified with the
-conf option. You may set any command line option in the file with
the exception of the configuration file name itself. In the event
you have specified an option both in the configuration file and the
command line, the command line takes precedence.
Here is an example parrotbench.conf:
[global]
directory = ../../examples/benchmarks
list = 0
help = 0
method = 2
time = 1
[regexes]
include = ^gc
include = ^oo
exclude = header
exclude = waves
[benchmark parrotj]
exe = ../../parrot -j
type = .pasm
type = .pir
[benchmark perl_585_th]
exe = /usr/bin/perl585-th
type = .pl
[benchmark python]
exe = /usr/local/bin/python
type = .py
[benchmark ruby]
exe = /usr/bin/ruby
type = .rb
=head1 BUGS
While every effort was made to ensure this script is portable,
it is likely that it will break somewhere.
If a benchmark has multiple extensions associated with the same
executable, the last one will be used. For instance, with the
configuration file above, foo.pir would be selected over foo.pasm
=head1 AUTHOR
Joshua Gatcomb, C<Limbic_Region_2000@Yahoo.com>
Originally written by:
Sebastian Riedel, C<sri@oook.de>
=cut
# Create Default Configuration
my %cfg = (
config_file => File::Spec->catdir( $FindBin::Bin, 'parrotbench.conf' ),
bench_path => undef,
list_only => undef,
use_times => undef,
display_help => undef,
method => undef,
run_bench => [],
skip_bench => [],
);
# Read Command Line Options
GetOptions(
'conf=s' => \$cfg{config_file},
'directory=s' => \$cfg{bench_path},
'list' => \$cfg{list_only},
'time' => \$cfg{use_times},
'help|?' => \$cfg{display_help},
'method=s' => \$cfg{method},
'benchmarks=s' => $cfg{run_bench},
'nobench=s' => $cfg{skip_bench},
);
# Read Configuration File
die 'Unable to access configuration file ', $cfg{config_file} unless -r $cfg{config_file};
my $ini = Config::IniFiles->new( -file => $cfg{config_file} );
# Merge Configuration
if ( !defined $cfg{bench_path} ) {
$cfg{bench_path} = $ini->val( global => 'directory' );
}
if ( !defined $cfg{list_only} ) {
$cfg{list_only} = $ini->val( global => 'list' );
}
if ( !defined $cfg{use_times} ) {
$cfg{use_times} = $ini->val( global => 'time' );
}
if ( !defined $cfg{display_help} ) {
$cfg{display_help} = $ini->val( global => 'help' );
}
pod2usage 1 if $cfg{display_help};
if ( !defined $cfg{method} ) {
$cfg{method} = $ini->val( global => 'method', 1 );
}
if ( !@{ $cfg{run_bench} } ) {
my @regexes = grep defined, $ini->val( regexes => 'include' );
@{ $cfg{run_bench} } = @regexes ? @regexes : '[\d\D]';
}
if ( !@{ $cfg{skip_bench} } ) {
my @regexes = grep defined, $ini->val( regexes => 'exclude' );
@{ $cfg{skip_bench} } = @regexes ? @regexes : '[^\d\D]';
}
# Frequently Used Variables
my %bench;
my @section = sort $ini->GroupMembers('benchmark');
my @program = map { /^benchmark\s+(.*)$/ } @section;
my %suffix;
$suffix{$_} = [ map quotemeta, $ini->val( $_, 'type' ) ] for @section;
my $ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK);
my %Get_Time = (
1 => sub { my @times = times(); return $times[2] + $times[3] },
2 => sub { return ( POSIX::times() )[0] / $ticks },
);
# Find And Build Benchmarks
find sub {
my $pass;
for my $regex ( @{ $cfg{run_bench} } ) {
$pass++ and last if /$regex/;
}
return if !$pass;
my $fail;
for my $regex ( @{ $cfg{skip_bench} } ) {
$fail++ and last if /$regex/;
}
return if $fail;
for my $index ( 0 .. $#section ) {
my ( $name, $p, $ext ) = fileparse( $_, @{ $suffix{ $section[$index] } } );
next if !$ext;
$bench{$name}{ $program[$index] } = $ext;
}
}, $cfg{bench_path};
die "No benchmarks found" if !keys %bench;
# List Names Of Benchmarks With Pretty Output
if ( $cfg{list_only} ) {
my @rows;
push @rows, [ 'Benchmark', @program ];
for my $name ( sort keys %bench ) {
push @rows, [ $name, map { $bench{$name}{$_} || '-' } @program ];
}
my @max;
for ( 0 .. @program ) {
for my $row (@rows) {
Longest( $max[$_], length $row->[$_] );
}
}
for my $col (@rows) {
print map { sprintf( "%-$max[$_]s ", $col->[$_] ) } 0 .. $#$col;
print "\n";
}
exit;
}
# Run The Benchmarks With Pretty Output
if ( !$cfg{use_times} && @program < 2 ) {
print "WARNING: Switching percentage to time - not enough executables\n";
$cfg{use_times} = 1;
}
if ( $cfg{use_times} ) {
my $type = $cfg{method} == 1 ? 'CPU' : 'wall-clock';
print "Times are in $type seconds. (lower is better)\n";
}
else {
print "Numbers are relative to the first one. (lower is better)\n";
}
print "\n";
open( COPYOUT, ">&STDOUT" ) or die "Unable to copy STDOUT";
open( STDOUT, '>', File::Spec->devnull ) or die "Unable to redirect STDOUT";
select COPYOUT;
$| = 1;
my @max = $cfg{method} == 1 ? (5) x @program : (6) x @program;
Longest( $max[0], length $_ ) for 'Benchmark', keys %bench;
Longest( $max[ $_ + 1 ], length $program[$_] ) for 0 .. $#program;
printf( "%-$max[0]s ", 'Benchmark' );
printf( "%-$max[$_ + 1]s ", $program[$_] ) for 0 .. $#program;
for my $name ( sort keys %bench ) {
my $base = 0;
printf( "\n%-$max[0]s ", $name );
for ( 0 .. $#section ) {
my ( $prog, $sect ) = ( $program[$_], $section[$_] );
if ( $bench{$name}{$prog} ) {
my $start = $Get_Time{ $cfg{method} }->();
system( $ini->val( $sect, 'exe' ) . " "
. File::Spec->catdir( $cfg{bench_path}, $name . $bench{$name}{$prog} ) );
my $stop = $Get_Time{ $cfg{method} }->();
my $used = $stop - $start;
$base ||= $used;
printf( "%-$max[$_ + 1]s ",
$cfg{use_times}
? sprintf( "%.3f", $used )
: sprintf( "%d%%", $used / ( $base / 100 ) ) );
}
else {
printf( "%-$max[$_ + 1]s ", '-' );
}
}
}
sub Longest {
$_[0] = $_[1] and return if !defined $_[0];
$_[0] = $_[1] if $_[1] > $_[0];
}
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: