-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.pl
executable file
·197 lines (180 loc) · 6.01 KB
/
benchmark.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
#!/usr/bin/perl
# Process the raw output from benchmark.sh into Javascript-ified HTML.
use strict;
use warnings;
my @presets = ();
my %presets = ();
my $maxval = 0;
while (<<>>) {
chomp;
if (/^(.*)(#.*): ([\d\.]+)$/) {
push @presets, $1 unless defined $presets{$1};
push @{$presets{$1}}, $3;
$maxval = $3 if $maxval < $3;
}
}
print <<EOF;
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ASCII" />
<title>Puzzle generation-time benchmarks</title>
<script type="text/javascript">
//<![CDATA[
function choose_scale_ticks(scale) {
var nscale = 1, j = 0, factors = [2,2.5,2];
while (scale / nscale > 20) {
nscale *= factors[j];
j = (j+1) % factors.length;
}
return nscale;
}
function initPlots() {
var canvases = document.getElementsByTagName('canvas');
for (var i = 0; i < canvases.length; i++) {
var canvas = canvases[i];
var scale = eval(canvas.getAttribute("data-scale"));
var add = 20.5, mult = (canvas.width - 2*add) / scale;
var data = eval(canvas.getAttribute("data-points"));
var ctx = canvas.getContext('2d');
ctx.lineWidth = '1px';
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.strokeStyle = ctx.fillStyle = '#000000';
if (data === "scale") {
// Draw scale.
ctx.font = "16px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "alphabetic";
var nscale = choose_scale_ticks(scale);
for (var x = 0; x <= scale; x += nscale) {
ctx.beginPath();
ctx.moveTo(add+mult*x, canvas.height);
ctx.lineTo(add+mult*x, canvas.height - 3);
ctx.stroke();
ctx.fillText(x + "s", add+mult*x, canvas.height - 6);
}
} else {
// Draw a box plot.
function quantile(x) {
var n = (data.length * x) | 0;
return (data[n-1] + data[n]) / 2;
}
var q1 = quantile(0.25), q2 = quantile(0.5), q3 = quantile(0.75);
var iqr = q3 - q1;
var top = 0.5, bot = canvas.height - 1.5, mid = (top+bot)/2;
var wlo = null, whi = null; // whisker ends
ctx.strokeStyle = '#bbbbbb';
var nscale = choose_scale_ticks(scale);
for (var x = 0; x <= scale; x += nscale) {
ctx.beginPath();
ctx.moveTo(add+mult*x, 0);
ctx.lineTo(add+mult*x, canvas.height);
ctx.stroke();
}
ctx.strokeStyle = '#000000';
for (var j in data) {
var x = data[j];
if (x >= q1 - 1.5 * iqr && x <= q3 + 1.5 * iqr) {
if (wlo === null || wlo > x)
wlo = x;
if (whi === null || whi < x)
whi = x;
} else {
ctx.beginPath();
ctx.arc(add+mult*x, mid, 2, 0, 2*Math.PI);
ctx.stroke();
if (x >= q1 - 3 * iqr && x <= q3 + 3 * iqr)
ctx.fill();
}
}
ctx.beginPath();
// Box
ctx.moveTo(add+mult*q1, top);
ctx.lineTo(add+mult*q3, top);
ctx.lineTo(add+mult*q3, bot);
ctx.lineTo(add+mult*q1, bot);
ctx.closePath();
// Line at median
ctx.moveTo(add+mult*q2, top);
ctx.lineTo(add+mult*q2, bot);
// Lower whisker
ctx.moveTo(add+mult*q1, mid);
ctx.lineTo(add+mult*wlo, mid);
ctx.moveTo(add+mult*wlo, top);
ctx.lineTo(add+mult*wlo, bot);
// Upper whisker
ctx.moveTo(add+mult*q3, mid);
ctx.lineTo(add+mult*whi, mid);
ctx.moveTo(add+mult*whi, top);
ctx.lineTo(add+mult*whi, bot);
ctx.stroke();
}
}
document.getElementById('sort_orig').onclick = function() {
sort(function(e) {
return parseFloat(e.getAttribute("data-index"));
});
};
document.getElementById('sort_median').onclick = function() {
sort(function(e) {
return -parseFloat(e.getAttribute("data-median"));
});
};
document.getElementById('sort_mean').onclick = function() {
sort(function(e) {
return -parseFloat(e.getAttribute("data-mean"));
});
};
}
function sort(keyfn) {
var rows = document.getElementsByTagName("tr");
var trs = [];
for (var i = 0; i < rows.length; i++)
trs.push(rows[i]);
trs.sort(function(a,b) {
var akey = keyfn(a);
var bkey = keyfn(b);
return akey < bkey ? -1 : akey > bkey ? +1 : 0;
});
var parent = trs[0].parentElement;
for (var i = 0; i < trs.length; i++)
parent.removeChild(trs[i]);
for (var i = 0; i < trs.length; i++)
parent.appendChild(trs[i]);
}
//]]>
</script>
</head>
<body onLoad="initPlots();">
<h1 align=center>Puzzle generation-time benchmarks</h1>
<p>Sort order:
<button id="sort_orig">Original</button>
<button id="sort_median">Median</button>
<button id="sort_mean">Mean</button>
<table>
<tr><th>Preset</th><td><canvas width=700 height=30 data-points='"scale"' data-scale="$maxval"></td></tr>
EOF
my $index = 0;
for my $preset (@presets) {
my @data = sort { $a <=> $b } @{$presets{$preset}};
my $median = ($#data % 2 ?
($data[($#data-1)/2]+$data[($#data+1)/2])/2 :
$data[$#data/2]);
my $mean = 0; map { $mean += $_ } @data; $mean /= @data;
print "<tr data-index=\"$index\" data-mean=\"$mean\" data-median=\"$median\"><td>", &escape($preset), "</td><td><canvas width=700 height=15 data-points=\"[";
print join ",", @data;
print "]\" data-scale=\"$maxval\"></td></tr>\n";
$index++;
}
print <<EOF;
</body>
</html>
EOF
sub escape {
my ($text) = @_;
$text =~ s/&/&/g;
$text =~ s/</</g;
$text =~ s/>/>/g;
return $text;
}