Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 170 lines (119 sloc) 3.194 kb
5b3c101 created benchmark suite for object delivery and consumption
Tyler Riddle authored
1 #!/usr/bin/env perl
2
3 use lib qw(bench/lib);
4
5 use strict;
6 use warnings;
7 use Data::Dumper;
8
9 $ENV{EVENT_SEND_COUNT} = 500000 unless exists $ENV{EVENT_SEND_COUNT};
10 $ENV{MIN_TEST_TIME} = 60 unless exists $ENV{MIN_TEST_TIME};
11 $ENV{BENCH_DIR} = 'bench' unless exists $ENV{BENCH_DIR};
12
13 logger("Configuration: $ENV{EVENT_SEND_COUNT} events and $ENV{MIN_TEST_TIME} seconds minimum test time\n");
14
15 my @results = doTests();
16 my $error = shift(@results);
17 doLog(@results);
18 exit($error);
19
20 exit(0);
21
22 sub logger {
23 print STDERR @_;
24 }
25
26 sub genFiles {
27 my ($dir) = @_;
28 my $dh;
29 my @paths;
30
31 die "$dir is not a directory or is not accessable" unless -d $dir;
32
33 die "Could not opendir($dir): $!" unless opendir($dh, $dir);
34
35 foreach(readdir($dh)) {
36 next if m/^\./;
37 next if -d $_;
38
39 push(@paths, "$dir/$_");
40 }
41
42 die "Could not closedir($dir): $!" unless closedir($dh);
43
44 return @paths;
45 }
46
47 sub genTests {
48 my @files;
49 my @buf;
50
51 if (scalar(@ARGV) > 0) {
52 @files = @ARGV;
53 } else {
54 logger("Loading benchmarks from $ENV{BENCH_DIR}/... ");
55 @files = genFiles($ENV{BENCH_DIR});
56 logger("found ", scalar(@files), " files\n");
57 }
58
59 foreach(@files) {
60 my %test = ( file => $_ );
61
62 logger("loading $_... ");
63
64 $test{cb} = require $_;
65
66 logger("done\n");
67
68 push(@buf, \%test);
69 }
70
71 return @buf;
72 }
73
74 sub randomInt {
75 return int(rand(10));
76 }
77
78 sub genData {
79 my @buf = @_;
80
81 logger("Generating test data... ");
82
83 foreach(1 .. $ENV{EVENT_SEND_COUNT}) {
84 push(@buf, [ randomInt(), randomInt() ]);
85 }
86
87 logger("done\n");
88 return @buf;
89 }
90
91 sub sumData {
92 my $sum = 0;
93
94 foreach(@_) {
95 $sum += $_->[0] + $_->[1];
96 }
97
98 return $sum;
99 }
100
101 sub doTests {
102 my @tests = genTests();
103 my @testData = genData();
104 my $sum = sumData(@testData);
105 my $error = 0;
106
107 foreach(@tests) {
108 my $seconds = 0;
109 my $eventCount = 0;
110 my $iterations = 0;
111
112 logger("Measuring $_->{file}... ");
113
114 while(1) {
115 my ($results, $bench);
116
117 $iterations++;
118
119 $results = $_->{cb}(@testData);
120 $bench = $results->{bench};
121
122 if ($results->{sum} != $sum) {
123 logger("INVALID SUM $results->{sum} != $sum ");
124 $error = 1;
125 }
126
127 $seconds += $bench->[0];
128 $eventCount += $ENV{EVENT_SEND_COUNT};
129
130 if ($seconds >= $ENV{MIN_TEST_TIME}) {
131 last;
132 }
133
134 logger("$iterations ");
135 }
136
137 $_->{analysis} = {
138 cpuTime => $seconds,
139 eventCount => $eventCount,
140 eventsPerSecond => $eventCount / $seconds,
141 };
142
143 logger("done\n");
144 }
145
146 return ($error, @tests);
147 }
148
149 sub doLog {
150 my (@tests) = @_;
151
152 logger("Report format: file\tevents per second\tcost of solution\n");
153
154 foreach(sort({ $b->{analysis}->{eventsPerSecond} <=> $a->{analysis}->{eventsPerSecond} } @tests)) {
155 my $analysis = $_->{analysis};
156 my $eventsPerSecond = int($analysis->{eventsPerSecond});
157 our $fastest;
158 my $cost;
159
160 if (! defined($fastest)) {
161 $fastest = $eventsPerSecond;
162 }
163
164 $cost = $fastest / $eventsPerSecond;
165
166 print "$_->{file}\t$eventsPerSecond\t$cost\n";
167 }
168 }
169
Something went wrong with that request. Please try again.