-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathrunner.js
86 lines (76 loc) · 2.39 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
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { basename } from 'node:path';
import { promises as fs } from 'node:fs';
import * as hdr from 'hdr-histogram-js';
hdr.initWebAssemblySync();
const { path, times, concurrency, 'redis-server-host': host } = yargs(hideBin(process.argv))
.option('path', {
type: 'string',
demandOption: true
})
.option('times', {
type: 'number',
default: 1_000_000,
demandOption: true
})
.option('concurrency', {
type: 'number',
default: 100,
demandOption: true
})
.option('redis-server-host', {
type: 'string'
})
.parseSync();
const [ { metadata, timestamp }, module ] = await Promise.all([
new Promise(resolve => {
process.once('message', resolve);
process.send('ready');
}),
import(path)
]),
{ benchmark, teardown } = await module.default(host, metadata);
async function run(times) {
return new Promise(resolve => {
const histogram = hdr.build({ useWebAssembly: true });
let num = 0,
inProgress = 0;
async function run() {
++inProgress;
++num;
const start = process.hrtime.bigint();
await benchmark(metadata);
histogram.recordValue(Number(process.hrtime.bigint() - start));
--inProgress;
if (num < times) {
run();
} else if (inProgress === 0) {
resolve(histogram);
}
}
const toInitiate = Math.min(concurrency, times);
for (let i = 0; i < toInitiate; i++) {
run();
}
});
}
// warmup
await run(Math.min(times * 0.1, 10_000));
// benchmark
const benchmarkStart = process.hrtime.bigint(),
histogram = await run(times),
benchmarkNanoseconds = process.hrtime.bigint() - benchmarkStart,
json = {
// timestamp,
operationsPerSecond: times / Number(benchmarkNanoseconds) * 1_000_000_000,
p0: histogram.getValueAtPercentile(0),
p50: histogram.getValueAtPercentile(50),
p95: histogram.getValueAtPercentile(95),
p99: histogram.getValueAtPercentile(99),
p100: histogram.getValueAtPercentile(100)
};
console.log(`[${basename(path)}]:`);
console.table(json);
await fs.writeFile(`${path}.json`, JSON.stringify(json));
await teardown();