forked from RafaelGSS/bench-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-runner.js
More file actions
51 lines (46 loc) · 1.15 KB
/
worker-runner.js
File metadata and controls
51 lines (46 loc) · 1.15 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
const { parentPort } = require("node:worker_threads");
const {
runBenchmark,
getInitialIterations,
runWarmup,
} = require("./lifecycle");
const { debugBench } = require("./clock");
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
// Deserialize the benchmark function
function deserializeBenchmark(benchmark) {
const { isAsync, hasArg } = benchmark;
const fnPrototype = isAsync ? AsyncFunction : Function;
if (hasArg) {
benchmark.fn = new fnPrototype("timer", benchmark.fn);
} else {
benchmark.fn = new fnPrototype(benchmark.fn);
}
}
parentPort.on(
"message",
async ({
benchmark,
initialIterations,
benchmarkMode,
repeatSuite,
minSamples,
}) => {
deserializeBenchmark(benchmark);
debugBench(
`Warmup ${benchmark.name} with minTime=${benchmark.minTime}, maxTime=${benchmark.maxTime}`,
);
const initialIteration = await getInitialIterations(benchmark);
await runWarmup(benchmark, initialIteration, {
minTime: 0.005,
maxTime: 0.05,
});
const result = await runBenchmark(
benchmark,
initialIterations,
benchmarkMode,
repeatSuite,
minSamples,
);
parentPort.postMessage(result);
},
);