-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (63 loc) · 1.67 KB
/
index.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
const workestrator = require("../../dist");
const farmsOptions = {
numberOfWorkers: 1,
module: require.resolve("./child"),
maxConcurrentCallsPerWorker: 1,
maxRetries: 0,
};
function waitForWorkersToLoad(farm) {
return new Promise((resolve) => {
const itv = setInterval(() => {
const readyWorkers = farm.workers.reduce((acc, w) => acc + Number(w.isAvailable()), 0);
if (readyWorkers === farm.workers.length) {
clearInterval(itv);
resolve();
}
}, 50);
});
}
const jsonFarm = workestrator.create({
...farmsOptions,
serializerPath: workestrator.serializers.JSON,
});
const cborFarm = workestrator.create({
...farmsOptions,
serializerPath: workestrator.serializers.CBOR,
});
const iterations = 500;
async function run(farm) {
const calls = [];
const data = {
number: 1,
string: "",
object: {
a: 1,
},
null: null,
array: [1, "2", 3],
};
const startTime = process.hrtime();
for (let i = 0; i < iterations; i++) {
calls.push(farm.run(data))
}
await Promise.all(calls);
const endTime = process.hrtime(startTime);
const diff = (endTime[0] * 1000) + (endTime[1] / 1000000);
console.log("Done in", diff, "ms");
}
/*
For this benchmarks we compare simple data types as:
- Number
- String
- Object (with null)
- Array
*/
(async () => {
await waitForWorkersToLoad(jsonFarm);
console.log("JSON Serializer :");
await run(jsonFarm);
await waitForWorkersToLoad(cborFarm);
console.log("\nCBOR Serializer :");
await run(cborFarm);
process.exit(0);
})();