-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathtestWorker.js
70 lines (68 loc) · 1.98 KB
/
testWorker.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
var haveScripts = false, timeout;
function getScripts(msg) {
if (haveScripts) return;
importScripts.apply(null, ["benchmark.js"].concat(msg.url));
var start = new Function(msg.onStart);
start();
haveScripts = true;
}
onmessage = function (e) {
var msg = e.data;
getScripts(msg);
if (!msg.testsToRun) {
postMessage({
type: "loaded"
});
return;
}
Benchmark.options.minTime = 1 / 64;
Benchmark.options.maxTime = 1 / 2;
Benchmark.options.minSamples = 5;
var suite = new Benchmark.Suite();
for (var i = 0; i < msg.testsToRun.length; i++) {
var name = msg.testsToRun[i];
suite.add(name, msg.tests[name], {
onCycle: msg.onCycle ? new Function(msg.onCycle) : function () { }
});
}
suite.on("cycle", function (e) {
var target = e.target,
name = target.name,
stats = target.stats,
desc = createDescription(stats);
if (target.aborted) {
if (!stats.mean)
desc = "Test timed out.";
}
postMessage({
type: "cycle",
name: name,
stats: stats,
desc: desc
});
clearTimeout(timeout);
})
.on("complete", function (e) {
postMessage({
type: "complete"
});
}).run({
async: true
});
timeout = setTimeout(function () { // abort tests after 10 seconds
suite.abort();
}, msg.timeout);
};
function createDescription(stats) {
var runs = stats.sample.length;
var rme = stats.rme.toFixed(2);
var mean = 1 / stats.mean || 0;
if (mean >= 100) mean = Math.round(mean) + "";
else if (mean >= 10) mean = mean.toFixed(1);
else if (mean >= 1) mean = mean.toFixed(2);
else mean = mean.toFixed(3);
while (/(\d+)(\d{3})/.test(mean)) {
mean = mean.replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
}
return mean + " ops/sec ±" + rme + "% (" + runs + " samples)";
}