-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
190 lines (171 loc) · 4.63 KB
/
worker.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
const { spawn } = require('node:child_process');
const assert = require('node:assert');
const autocannon = require('autocannon');
const { Bench } = require('tinybench');
const { Suite } = require('bench-node');
const { setTimeout: delay } = require('node:timers/promises');
const runner = {
autocannon: async (opts, aggregated) => {
if (!aggregated.sortKey) {
aggregated.sortKey = 'requests'
}
const url = `http://localhost:${opts.http.serverPort}`
const results = await autocannon({
url,
connections: 100,
pipelining: 1,
duration: 10 * opts.http.routes.length,
requests: opts.http.routes,
})
if (!aggregated[url]) {
aggregated[url] = [results]
} else {
aggregated[url].push(results)
}
},
'bench-node': async (opts, aggregated) => {
if (!aggregated.sortKey) {
aggregated.sortKey = 'opsSec'
}
const suite = new Suite({ reporter: false });
for (const operation of opts.operations) {
suite.add(operation.name, operation.fn);
}
const results = await suite.run();
for (const result of results) {
if (!aggregated[result.name]) {
aggregated[result.name] = [{
opsSec: result.opsSec,
samples: result.iterations,
sd: '',
variance: '',
}]
} else {
aggregated[result.name].push({
opsSec: result.opsSec,
samples: result.iterations,
sd: '',
variance: '',
})
}
}
},
tinybench: async (opts, aggregated) => {
if (!aggregated.sortKey) {
aggregated.sortKey = 'opsSec'
}
const suite = new Bench({ time: 100 });
for (const operation of opts.operations) {
suite.add(operation.name, operation.fn);
}
await suite.warmup();
await suite.run();
const tasks = suite.tasks;
for (const task of tasks) {
const result = task.result
if (!aggregated[task.name]) {
aggregated[task.name] = [{
opsSec: result.hz,
samples: result.samples.length,
sd: result.sd,
variance: result.variance,
}]
} else {
aggregated[task.name].push({
opsSec: result.hz,
samples: result.samples.length,
sd: result.sd,
variance: result.variance,
})
}
}
},
}
const parser = {
autocannon: (settings, result) => {
return {
name: settings.name,
method: 'autocannon',
http: {
totalReq: asNumber(result.requests),
duration: result.duration,
errors: result.errors,
}
};
},
tinybench: (settings, result) => {
return {
name: settings.name,
method: 'tinybench',
operations: result,
}
},
'bench-node': (settings, result) => {
return {
name: settings.name,
method: 'bench-node',
operations: result,
}
}
}
const ALLOWED_BENCHMARKER = ['autocannon', 'tinybench', 'bench-node'];
function asNumber (stat) {
const result = Object.create(null)
for (const k of Object.keys(stat)) {
result[k] = stat[k].toLocaleString(undefined, {
// to show all digits
maximumFractionDigits: 20
})
}
return result
}
function spawnServer(settings) {
const server = spawn(
process.execPath,
[settings.http.server],
{ stdio: 'inherit' },
);
return server;
}
function findMedian(aggregated) {
const results = []
// Select median
const sortKey = aggregated.sortKey
for (const k of Object.keys(aggregated)) {
if (aggregated[k] === sortKey) continue
aggregated[k].sort((a, b) => a[sortKey] > b[sortKey])
const middleIndex = Math.floor(aggregated[k].length / 2);
results.push({
name: k,
...aggregated[k][middleIndex]
})
}
return results
}
async function runBenchmark(settings) {
assert.ok(ALLOWED_BENCHMARKER.includes(settings.benchmarker), 'Invalid settings.benchmarker');
let server = undefined;
if (settings.type === 'http') {
assert.ok(settings.http.server, 'HTTP Benchmark must have a server to be spawned');
server = spawnServer(settings);
// TODO: replace this workaround to use IPC to know when server is up
await delay(1000);
}
const benchRunner = runner[settings.benchmarker];
const benchParser = parser[settings.benchmarker];
const aggregated = {};
for (let i = 0; i < 10; ++i) {
await benchRunner(settings, aggregated)
}
const results = findMedian(aggregated)
if (server) {
server.kill('SIGINT');
}
return benchParser(settings, results);
}
async function main (benchFile) {
const bench = require(benchFile);
const result = await runBenchmark(bench);
return result;
}
module.exports = main;