-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-fork.js
35 lines (30 loc) · 845 Bytes
/
test-fork.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
const { fork } = require("child_process");
const text = process.argv[2] || "Woohooo 😍 ✌️";
let i = 0;
function runService(workerData) {
return new Promise((resolve, reject) => {
const worker = fork("./worker-fork.js", [workerData]);
worker.on("message", resolve);
worker.on("error", reject);
worker.on("exit", (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
});
}
const startDateTime = new Date();
const next = async () => {
try {
console.log(`calling with "${text}"`);
const result = await runService(text);
i++;
const iterationsPerSecond = i / ((new Date() - startDateTime) / 1000);
console.log({ result, i, iterationsPerSecond });
} catch (err) {
console.error(err);
}
// global.gc();
next();
};
next();