-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbench.pl
executable file
·169 lines (119 loc) · 3.12 KB
/
bench.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
#!/usr/bin/env perl
use lib qw(bench/lib);
use strict;
use warnings;
use Data::Dumper;
$ENV{EVENT_SEND_COUNT} = 500000 unless exists $ENV{EVENT_SEND_COUNT};
$ENV{MIN_TEST_TIME} = 60 unless exists $ENV{MIN_TEST_TIME};
$ENV{BENCH_DIR} = 'bench' unless exists $ENV{BENCH_DIR};
logger("Configuration: $ENV{EVENT_SEND_COUNT} events and $ENV{MIN_TEST_TIME} seconds minimum test time\n");
my @results = doTests();
my $error = shift(@results);
doLog(@results);
exit($error);
exit(0);
sub logger {
print STDERR @_;
}
sub genFiles {
my ($dir) = @_;
my $dh;
my @paths;
die "$dir is not a directory or is not accessable" unless -d $dir;
die "Could not opendir($dir): $!" unless opendir($dh, $dir);
foreach(readdir($dh)) {
next if m/^\./;
next if -d $_;
push(@paths, "$dir/$_");
}
die "Could not closedir($dir): $!" unless closedir($dh);
return @paths;
}
sub genTests {
my @files;
my @buf;
if (scalar(@ARGV) > 0) {
@files = @ARGV;
} else {
logger("Loading benchmarks from $ENV{BENCH_DIR}/... ");
@files = genFiles($ENV{BENCH_DIR});
logger("found ", scalar(@files), " files\n");
}
foreach(@files) {
my %test = ( file => $_ );
logger("loading $_... ");
$test{cb} = require $_;
logger("done\n");
push(@buf, \%test);
}
return @buf;
}
sub randomInt {
return int(rand(10));
}
sub genData {
my @buf = @_;
logger("Generating test data... ");
foreach(1 .. $ENV{EVENT_SEND_COUNT}) {
push(@buf, [ randomInt(), randomInt() ]);
}
logger("done\n");
return @buf;
}
sub sumData {
my $sum = 0;
foreach(@_) {
$sum += $_->[0] + $_->[1];
}
return $sum;
}
sub doTests {
my @tests = genTests();
my @testData = genData();
my $sum = sumData(@testData);
my $error = 0;
foreach(@tests) {
my $seconds = 0;
my $eventCount = 0;
my $iterations = 0;
logger("Measuring $_->{file}... ");
while(1) {
my ($results, $bench);
$iterations++;
$results = $_->{cb}(@testData);
$bench = $results->{bench};
if ($results->{sum} != $sum) {
logger("INVALID SUM $results->{sum} != $sum ");
$error = 1;
}
$seconds += $bench->[0];
$eventCount += $ENV{EVENT_SEND_COUNT};
if ($seconds >= $ENV{MIN_TEST_TIME}) {
last;
}
logger("$iterations ");
}
$_->{analysis} = {
cpuTime => $seconds,
eventCount => $eventCount,
eventsPerSecond => $eventCount / $seconds,
};
logger("done\n");
}
return ($error, @tests);
}
sub doLog {
my (@tests) = @_;
logger("Report format: file\tevents per second\tcost of solution\n");
foreach(sort({ $b->{analysis}->{eventsPerSecond} <=> $a->{analysis}->{eventsPerSecond} } @tests)) {
my $analysis = $_->{analysis};
my $eventsPerSecond = int($analysis->{eventsPerSecond});
our $fastest;
my $cost;
if (! defined($fastest)) {
$fastest = $eventsPerSecond;
}
$cost = $fastest / $eventsPerSecond;
print "$_->{file}\t$eventsPerSecond\t$cost\n";
}
}