-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathbenchmark-lite.js
More file actions
65 lines (63 loc) · 1.98 KB
/
benchmark-lite.js
File metadata and controls
65 lines (63 loc) · 1.98 KB
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
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
export default class Suite {
constructor(name, { iterations = 10, timeLimit = 5000 } = {}) {
this.name = name;
this.iterations = iterations;
this.timeLimit = timeLimit;
this.tests = [];
}
add(name, executor) {
this.tests.push({ name, executor });
return this;
}
async run() {
console.log(` ${this.name}:`);
const results = [];
let fastest = 0;
for (const test of this.tests) {
await sleep(50);
for (let i = 0; i < 5; i++) test.executor(i);
await sleep(10);
const result = this.runOne(test);
if (result.hz > fastest) fastest = result.hz;
results.push({ ...test, ...result });
}
const winners = results.filter((r) => r.hz === fastest).map((r) => r.name);
console.log(` Fastest: \x1b[32m${winners}\x1b[39m\n`);
return this;
}
runOne({ name, executor }) {
const { iterations, timeLimit } = this;
let count = 5;
let now = performance.now(),
start = now,
prev = now;
const times = [];
do {
// oxlint-disable-next-line no-unused-vars
for (let i = iterations; i--; ) executor(++count);
prev = now;
now = performance.now();
times.push((now - prev) / iterations);
} while (now - start < timeLimit);
const elapsed = now - start;
const hz = Math.round((count / elapsed) * 1000);
const average = toFixed(elapsed / count);
const middle = Math.floor(times.length / 2);
const middle2 = Math.ceil(times.length / 2);
times.sort((a, b) => a - b);
const median = toFixed((times[middle] + times[middle2]) / 2);
const hzStr = hz.toLocaleString();
const averageStr = average.toLocaleString();
const n = times.length;
const stdev = Math.sqrt(
times.reduce((c, t) => c + (t - average) ** 2, 0) / (n - 1)
);
const p95 = toFixed((1.96 * stdev) / Math.sqrt(n));
console.log(
` \x1b[36m${name}:\x1b[0m ${hzStr}/s, average: ${averageStr}ms ±${p95} (median: ${median}ms)`
);
return { elapsed, hz, average, median };
}
}
const toFixed = (v) => ((v * 100) | 0) / 100;