-
Notifications
You must be signed in to change notification settings - Fork 54
/
driver.js
254 lines (223 loc) · 8.98 KB
/
driver.js
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
/*
* Copyright (C) 2016-2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
class Driver {
constructor(statusCell, triggerCell, triggerLink, magicCell, summaryCell, key)
{
this._benchmarks = new Map();
this._statusCell = statusCell;
this._triggerCell = triggerCell;
this._triggerLink = triggerLink;
this._magicCell = magicCell;
this._summary = new Stats(summaryCell, "summary");
this._key = key;
this._values = [];
this._names = [];
if (isInBrowser)
window[key] = this;
}
addBenchmark(benchmark)
{
this._benchmarks.set(benchmark, new Results(benchmark));
}
readyTrigger()
{
if (isInBrowser) {
this._triggerCell.addEventListener('click', this._triggerLink);
this._triggerCell.classList.add('ready');
}
}
disableTrigger()
{
if (isInBrowser) {
this._triggerCell.removeEventListener('click', this._triggerLink);
this._triggerCell.classList.remove('ready');
}
}
start(numIterations)
{
this.disableTrigger();
this._updateIterations();
this._summary.reset();
for (let [benchmark, results] of this._benchmarks)
results.reset();
this._isRunning = true;
this._startIterations = this._numIterations = numIterations;
this._iterator = null;
this._iterate();
}
reportResult(...args)
{
this._benchmarks.get(this._benchmark).reportResult(...args);
this._recomputeSummary();
this._iterate();
}
reportError(...args)
{
if (isInBrowser)
console.log("Error encountered: ", ...args);
this._benchmarks.get(this._benchmark).reportError(...args);
if (isInBrowser) {
this._statusCell.innerHTML = "Test failure \u2014 error in console";
this._statusCell.classList.add("failed");
} else
print("Test failure");
}
_recomputeSummary()
{
class Geomean {
constructor()
{
this._count = 0;
this._sum = 0;
}
add(value)
{
this._count++;
this._sum += Math.log(value);
}
get result()
{
return Math.exp(this._sum * (1 / this._count));
}
}
let statses = [];
for (let [benchmark, results] of this._benchmarks) {
for (let subResult of Results.subResults) {
statses.push(results[subResult]);
let val = results[subResult].valueForIteration(results[subResult].numIterations - 1);
if (val > 0 && benchmark.name == this._benchmark.name) {
this._values.push(val);
this._names.push(benchmark.name + "_" + subResult);
}
}
}
let numIterations = Math.min(...statses.map(stats => stats.numIterations));
let data = new Array(numIterations);
for (let i = 0; i < data.length; ++i)
data[i] = new Geomean();
for (let stats of statses) {
for (let i = 0; i < data.length; ++i)
data[i].add(stats.valueForIteration(i));
}
let geomeans = data.map(geomean => geomean.result);
if (geomeans.length == this._startIterations) {
for (let iter = 0; iter < geomeans.length; iter++) {
this._values.push(geomeans[iter]);
this._names.push("geomean");
}
}
this._summary.reset(...geomeans);
}
_iterate()
{
this._benchmark = this._iterator ? this._iterator.next().value : null;
if (!this._benchmark) {
if (!this._numIterations) {
if (isInBrowser) {
this._statusCell.innerHTML = "Restart";
this.readyTrigger();
if (typeof tpRecordTime !== "undefined")
tpRecordTime(this._values.join(','), 0, this._names.join(','));
this.sendResultsToRaptor();
} else
print("Success! Benchmark is now finished.");
return;
}
this._numIterations--;
this._updateIterations();
this._iterator = this._benchmarks.keys();
this._benchmark = this._iterator.next().value;
}
this._benchmarks.get(this._benchmark).reportRunning();
let benchmark = this._benchmark;
if (isInBrowser) {
window.setTimeout(() => {
if (!this._isRunning)
return;
this._magicCell.contentDocument.body.textContent = "";
this._magicCell.contentDocument.body.innerHTML = "<iframe id=\"magicFrame\" frameborder=\"0\">";
let magicFrame = this._magicCell.contentDocument.getElementById("magicFrame");
magicFrame.contentDocument.open();
magicFrame.contentDocument.write(
`<!DOCTYPE html><head><title>benchmark payload</title></head><body><script>` +
`window.onerror = top.${this._key}.reportError.bind(top.${this._key});\n` +
`function reportResult()\n` +
`{\n` +
` var driver = top.${this._key};\n` +
` driver.reportResult.apply(driver, arguments);\n` +
`}\n` +
`</script>\n` +
`${this._benchmark.code}</body></html>`);
}, 100);
} else {
Promise.resolve(20).then(() => {
if (!this._isRunning)
return;
try {
print(`Running... ${this._benchmark.name} ( ${this._numIterations + 1} to go)`);
benchmark.run();
print("\n");
} catch(e) {
print(e);
print(e.stack);
}
});
}
}
_updateIterations()
{
if (isInBrowser) {
this._statusCell.innerHTML = "Running Tests... " + ( this._startIterations - this._numIterations ) + "/" + this._startIterations;
}
}
sendResultsToRaptor()
{
// this contains all test names = [ "Air_firstIteration", "Air_averageWorstCase", ...]
var allNames = this._names;
// this contains all test metrics = [ 111, 83.5, 21.78894472361809, ...]
var allValues = this._values;
// this object will store name:[value1, value2, ...] pairs for the arrays above
var measuredValuesByFullName = {};
for (var i = 0, len = allNames.length; i < len; i++) {
if (measuredValuesByFullName[allNames[i]] === undefined) {
measuredValuesByFullName[allNames[i]] = [];
}
}
allNames.map(function(name, index) {
// now we save all the values for each test
// ex.: measuredValuesByFullName['Air_firstIteration'].push(111);
// measuredValuesByFullName['Air_firstIteration'].push(107);
measuredValuesByFullName[name].push(allValues[index]);
});
// delete the geomean array - this will be calculated by raptor
delete measuredValuesByFullName.geomean;
if (location.search === '?raptor') {
var _data = ['raptor-benchmark', 'ares6', measuredValuesByFullName];
console.log('ares6 source is about to post results to the raptor webext');
window.postMessage(_data, '*');
}
}
}