-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (36 loc) · 1.02 KB
/
index.js
File metadata and controls
41 lines (36 loc) · 1.02 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
"use strict";
const childProc = require("child_process");
const CHILD_PROCESSES = 20;
const URL = 'https://www.google.com/';
(async () => {
let times = [];
let children = [];
for (let i = 0; i < CHILD_PROCESSES; i++) {
let childProcess = childProc.spawn("node", ["child.js", `--url=${URL}`])
children.push(childProcess);
}
let responses = children.map(function wait(child) {
return new Promise(function c(res) {
child.stdout.on('data', (data) => {
console.log(`child stdout: ${data}`);
times.push(parseInt(data));
});
child.on("exit", function (code) {
if (code === 0) {
res(true);
} else {
res(false);
}
});
});
});
responses = await Promise.all(responses);
if (responses.filter(Boolean).length == responses.length) {
const sum = times.reduce((a, b) => a + b, 0);
const avg = (sum / times.length) || 0;
console.log(`average: ${avg}`);
console.log("success!");
} else {
console.log("failures!");
}
})();