forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
executable file
·401 lines (338 loc) · 14.2 KB
/
runner.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// There are tests for computeStatistics() located in LayoutTests/fast/harness/perftests
if (window.testRunner) {
testRunner.waitUntilDone();
testRunner.dumpAsText();
}
(function () {
var logLines = window.testRunner ? [] : null;
var verboseLogging = false;
var completedIterations;
var callsPerIteration = 1;
var currentTest = null;
var results;
var jsHeapResults;
var mallocHeapResults;
var iterationCount = undefined;
var lastResponsivenessTimestamp = 0;
var _longestResponsivenessDelay = 0;
var continueCheckingResponsiveness = false;
var PerfTestRunner = {};
// To make the benchmark results predictable, we replace Math.random with a
// 100% deterministic alternative.
PerfTestRunner.randomSeed = PerfTestRunner.initialRandomSeed = 49734321;
PerfTestRunner.resetRandomSeed = function() {
PerfTestRunner.randomSeed = PerfTestRunner.initialRandomSeed
}
PerfTestRunner.random = Math.random = function() {
// Robert Jenkins' 32 bit integer hash function.
var randomSeed = PerfTestRunner.randomSeed;
randomSeed = ((randomSeed + 0x7ed55d16) + (randomSeed << 12)) & 0xffffffff;
randomSeed = ((randomSeed ^ 0xc761c23c) ^ (randomSeed >>> 19)) & 0xffffffff;
randomSeed = ((randomSeed + 0x165667b1) + (randomSeed << 5)) & 0xffffffff;
randomSeed = ((randomSeed + 0xd3a2646c) ^ (randomSeed << 9)) & 0xffffffff;
randomSeed = ((randomSeed + 0xfd7046c5) + (randomSeed << 3)) & 0xffffffff;
randomSeed = ((randomSeed ^ 0xb55a4f09) ^ (randomSeed >>> 16)) & 0xffffffff;
PerfTestRunner.randomSeed = randomSeed;
return (randomSeed & 0xfffffff) / 0x10000000;
};
PerfTestRunner.now = window.performance && window.performance.now ? function () { return window.performance.now(); } : Date.now;
PerfTestRunner.logInfo = function (text) {
if (verboseLogging)
this.log(text);
}
PerfTestRunner.logDetail = function (label, value) {
if (verboseLogging)
this.log(' ' + label + ': ' + value);
}
PerfTestRunner.loadFile = function (path) {
var xhr = new XMLHttpRequest();
xhr.open("GET", path, false);
xhr.send(null);
return xhr.responseText;
}
PerfTestRunner.computeStatistics = function (times, unit) {
var data = times.slice();
// Add values from the smallest to the largest to avoid the loss of significance
data.sort(function(a,b){return a-b;});
var middle = Math.floor(data.length / 2);
var result = {
min: data[0],
max: data[data.length - 1],
median: data.length % 2 ? data[middle] : (data[middle - 1] + data[middle]) / 2,
};
// Compute the mean and variance using Knuth's online algorithm (has good numerical stability).
var squareSum = 0;
result.values = times;
result.mean = 0;
for (var i = 0; i < data.length; ++i) {
var x = data[i];
var delta = x - result.mean;
var sweep = i + 1.0;
result.mean += delta / sweep;
squareSum += delta * (x - result.mean);
}
result.variance = data.length <= 1 ? 0 : squareSum / (data.length - 1);
result.stdev = Math.sqrt(result.variance);
result.unit = unit || "ms";
return result;
}
PerfTestRunner.logStatistics = function (values, unit, title) {
var statistics = this.computeStatistics(values, unit);
this.log("");
this.log(title + " -> [" + statistics.values.join(", ") + "] " + statistics.unit);
["mean", "median", "stdev", "min", "max"].forEach(function (name) {
PerfTestRunner.logDetail(name, statistics[name] + ' ' + statistics.unit);
});
}
function getUsedMallocHeap() {
var stats = window.internals.mallocStatistics();
return stats.committedVMBytes - stats.freeListBytes;
}
function getUsedJSHeap() {
return window.internals.memoryInfo().usedJSHeapSize;
}
PerfTestRunner.gc = function () {
if (window.GCController)
window.GCController.collect();
else {
function gcRec(n) {
if (n < 1)
return {};
var temp = {i: "ab" + i + (i / 100000)};
temp += "foo";
gcRec(n-1);
}
for (var i = 0; i < 1000; i++)
gcRec(10);
}
};
function logInDocument(text) {
if (!document.getElementById("log")) {
var pre = document.createElement("pre");
pre.id = "log";
document.body.appendChild(pre);
}
document.getElementById("log").innerHTML += text + "\n";
window.scrollTo(0, document.body.height);
}
PerfTestRunner.log = function (text) {
if (logLines)
logLines.push(text);
else
logInDocument(text);
}
function logFatalError(text) {
PerfTestRunner.log(text);
finish();
}
function start(test, runner, doNotLogStart) {
if (!test) {
logFatalError("Got a bad test object.");
return;
}
currentTest = test;
// FIXME: We should be using multiple instances of test runner on Dromaeo as well but it's too slow now.
// FIXME: Don't hard code the number of in-process iterations to use inside a test runner.
iterationCount = test.customIterationCount || (window.testRunner ? 5 : 20);
completedIterations = -1;
results = [];
jsHeapResults = [];
mallocHeapResults = [];
verboseLogging = !window.testRunner;
if (!doNotLogStart) {
PerfTestRunner.logInfo('');
PerfTestRunner.logInfo("Running " + iterationCount + " times");
}
if (test.doNotIgnoreInitialRun)
completedIterations++;
if (runner)
scheduleNextRun(runner);
}
function scheduleNextRun(runner) {
PerfTestRunner.gc();
window.setTimeout(function () {
try {
if (currentTest.setup)
currentTest.setup();
var measuredValue = runner();
} catch (exception) {
logFatalError("Got an exception while running test.run with name=" + exception.name + ", message=" + exception.message);
return;
}
completedIterations++;
try {
ignoreWarmUpAndLog(measuredValue);
} catch (exception) {
logFatalError("Got an exception while logging the result with name=" + exception.name + ", message=" + exception.message);
return;
}
if (completedIterations < iterationCount)
scheduleNextRun(runner);
else
finish();
}, 0);
}
function ignoreWarmUpAndLog(measuredValue, doNotLogProgress) {
var labeledResult = measuredValue + " " + PerfTestRunner.unit;
if (completedIterations <= 0) {
if (!doNotLogProgress)
PerfTestRunner.logDetail(completedIterations, labeledResult + " (Ignored warm-up run)");
return;
}
results.push(measuredValue);
if (window.internals && !currentTest.doNotMeasureMemoryUsage) {
jsHeapResults.push(getUsedJSHeap());
mallocHeapResults.push(getUsedMallocHeap());
}
if (!doNotLogProgress)
PerfTestRunner.logDetail(completedIterations, labeledResult);
}
function finish() {
try {
var prefix = currentTest.name || '';
if (currentTest.description)
PerfTestRunner.log("Description: " + currentTest.description);
metric = {'fps': 'FrameRate', 'runs/s': 'Runs', 'pt': 'Score', 'ms': 'Time'}[PerfTestRunner.unit];
var suffix = currentTest.aggregator ? ':' + currentTest.aggregator : '';
PerfTestRunner.logStatistics(results, PerfTestRunner.unit, prefix + ":" + metric + suffix);
if (jsHeapResults.length) {
PerfTestRunner.logStatistics(jsHeapResults, "bytes", prefix + ":JSHeap");
PerfTestRunner.logStatistics(mallocHeapResults, "bytes", prefix + ":Malloc");
}
if (logLines && !currentTest.continueTesting)
logLines.forEach(logInDocument);
if (currentTest.done)
currentTest.done();
} catch (exception) {
logInDocument("Got an exception while finalizing the test with name=" + exception.name + ", message=" + exception.message);
}
if (!currentTest.continueTesting) {
if (window.testRunner)
testRunner.notifyDone();
return;
}
currentTest = null;
}
PerfTestRunner.prepareToMeasureValuesAsync = function (test) {
PerfTestRunner.unit = test.unit;
start(test);
}
PerfTestRunner.measureValueAsync = function (measuredValue) {
completedIterations++;
try {
ignoreWarmUpAndLog(measuredValue);
} catch (exception) {
logFatalError("Got an exception while logging the result with name=" + exception.name + ", message=" + exception.message);
return false;
}
if (completedIterations >= iterationCount) {
finish();
return false;
}
return true;
}
PerfTestRunner.reportValues = function (test, values) {
PerfTestRunner.unit = test.unit;
start(test, null, true);
for (var i = 0; i < values.length; i++) {
completedIterations++;
ignoreWarmUpAndLog(values[i], true);
}
finish();
}
PerfTestRunner.measureTime = function (test) {
PerfTestRunner.unit = "ms";
start(test, measureTimeOnce);
}
function measureTimeOnce() {
var start = PerfTestRunner.now();
var returnValue = currentTest.run();
var end = PerfTestRunner.now();
if (returnValue - 0 === returnValue) {
if (returnValue < 0)
PerfTestRunner.log("runFunction returned a negative value: " + returnValue);
return returnValue;
}
return end - start;
}
PerfTestRunner.measureRunsPerSecond = function (test) {
PerfTestRunner.unit = "runs/s";
start(test, measureRunsPerSecondOnce);
}
function measureRunsPerSecondOnce() {
var timeToRun = 750;
var totalTime = 0;
var numberOfRuns = 0;
while (totalTime < timeToRun) {
totalTime += callRunAndMeasureTime(callsPerIteration);
numberOfRuns += callsPerIteration;
if (completedIterations < 0 && totalTime < 100)
callsPerIteration = Math.max(10, 2 * callsPerIteration);
}
return numberOfRuns * 1000 / totalTime;
}
function callRunAndMeasureTime(callsPerIteration) {
var startTime = PerfTestRunner.now();
for (var i = 0; i < callsPerIteration; i++)
currentTest.run();
return PerfTestRunner.now() - startTime;
}
PerfTestRunner.startCheckingResponsiveness = function() {
lastResponsivenessTimestamp = PerfTestRunner.now();
_longestResponsivenessDelay = 0;
continueCheckingResponsiveness = true;
var timeoutFunction = function() {
var now = PerfTestRunner.now();
var delta = now - lastResponsivenessTimestamp;
if (delta > _longestResponsivenessDelay)
_longestResponsivenessDelay = delta;
lastResponsivenessTimestamp = now;
if (continueCheckingResponsiveness)
setTimeout(timeoutFunction, 0);
}
timeoutFunction();
}
PerfTestRunner.stopCheckingResponsiveness = function() {
continueCheckingResponsiveness = false;
}
PerfTestRunner.longestResponsivenessDelay = function() {
return _longestResponsivenessDelay;
}
PerfTestRunner.measurePageLoadTime = function(test) {
test.run = function() {
var file = PerfTestRunner.loadFile(test.path);
if (!test.chunkSize)
this.chunkSize = 50000;
var chunks = [];
// The smaller the chunks the more style resolves we do.
// Smaller chunk sizes will show more samples in style resolution.
// Larger chunk sizes will show more samples in line layout.
// Smaller chunk sizes run slower overall, as the per-chunk overhead is high.
var chunkCount = Math.ceil(file.length / this.chunkSize);
for (var chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++) {
var chunk = file.substr(chunkIndex * this.chunkSize, this.chunkSize);
chunks.push(chunk);
}
PerfTestRunner.logInfo("Testing " + file.length + " byte document in " + chunkCount + " " + this.chunkSize + " byte chunks.");
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
iframe.sandbox = ''; // Prevent external loads which could cause write() to return before completing the parse.
iframe.style.width = "600px"; // Have a reasonable size so we're not line-breaking on every character.
iframe.style.height = "800px";
iframe.contentDocument.open();
for (var chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
iframe.contentDocument.write(chunks[chunkIndex]);
// Note that we won't cause a style resolve until we've encountered the <body> element.
// Thus the number of chunks counted above is not exactly equal to the number of style resolves.
if (iframe.contentDocument.body)
iframe.contentDocument.body.clientHeight; // Force a full layout/style-resolve.
else if (iframe.documentElement.localName == 'html')
iframe.contentDocument.documentElement.offsetWidth; // Force the painting.
}
iframe.contentDocument.close();
document.body.removeChild(iframe);
};
PerfTestRunner.measureTime(test);
}
window.PerfTestRunner = PerfTestRunner;
})();