-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
61 lines (52 loc) · 1.57 KB
/
server.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
const express = require("express");
const app = express();
const util = require('util');
const exec = util.promisify(require("child_process").exec);
const workerpool = require('workerpool');
const pool = workerpool.pool();
const axios = require('axios');
app.get("/", (req, res) => {
return res.send("Hello World!");
});
app.get("/default-request/", (req, res) => {
let sum = 0;
for (let i = 0; i < 999999999; i++) {
sum += i;
}
return res.send("request completed! result: " + sum);
})
app.get("/worker-request/", (req, res) => {
pool.exec(() => {
let sum = 0;
for (let i = 0; i < 999999999; i++) {
sum += i;
}
return sum;
}).then((data) => {
return res.send("worker request completed! result: " + data);
}).then(() => {
pool.terminate(); // terminate all workers when done
});
})
app.get("/child-process-request/", async (req, res) => {
const { stdout, stderr } = await exec("./another-process");
if (stderr) {
throw new Error(stderr);
}
return res.send("another process request completed! result: " + stdout);
})
app.get("/http-process-request/", async (req, res) => {
const rq = await axios.get("http://localhost:7000/");
return res.send("another process request completed! result: " + rq.data);
})
app.listen(8000, () => {
console.log("server running on port 8000!");
})
//default request took 1062.30 ms
//499999998067109000
//worker process took 1088.67 ms concurrent tho
//499999998067109000
//child process request took 2519.53 ms also concurrent
//499999998500000001
//http broker request took 307.81 ms
//499999998500000001