-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (39 loc) · 1.19 KB
/
main.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
import { performance } from "node:perf_hooks";
import cluster from "node:cluster";
import { cpus } from "node:os";
import express from "express";
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running.`);
for (let i = 0; i < cpus().length; i++) {
cluster.fork();
}
cluster.on("exit", (worker) => {
console.log(`Worker ${worker.process.pid} died!`, "Forking new worker!");
cluster.fork();
});
} else {
console.log(`Worker ${process.pid} started!`);
const app = express();
app.get("/", (req, res) => {
const start = performance.now();
let count = 0;
while (count < 100_000_000) {
count++;
}
if (Math.floor(Math.random() * 10) + 1 === 5) {
process.exit(1);
}
const end = performance.now();
const duration = (end - start).toFixed(2).concat(" ms");
const response = {
message: "End of blocking operation",
worker: process.pid,
duration
};
console.log(response);
res.send(response);
});
app.listen(3000, () => {
console.log(`App listening on Port ${3000}`);
});
}