-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadless-socket.js
64 lines (51 loc) · 1.72 KB
/
headless-socket.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
const { io } = require("socket.io-client");
const URL = process.env.URL || "http://localhost:5555";
const MAX_CLIENTS = 1000;
const POLLING_PERCENTAGE = 0.05;
const CLIENT_CREATION_INTERVAL_IN_MS = 10;
const EMIT_INTERVAL_IN_MS = 1000;
let clientCount = 0;
let lastReport = new Date().getTime();
let packetsSinceLastReport = 0;
const createClient = () => {
// for demonstration purposes, some clients stay stuck in HTTP long-polling
const transports =
Math.random() < POLLING_PERCENTAGE ? ["polling"] : ["polling", "websocket"];
const socket = io(URL, {
transports,
});
setInterval(() => {
socket.emit("message:create", {
message: {
body: "gg",
thread: "6692faad0b9c03aa56eb9df8",
},
userId: "66b3b15f218a4409d1568b4b",
token:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmIzYjE1ZjIxOGE0NDA5ZDE1NjhiNGIiLCJpYXQiOjE3MjMwNTg1NTYsImV4cCI6MTcyMzA2MDM1NiwidHlwZSI6ImFjY2VzcyJ9.bf21Be7fu1ehFRp9ft0xPibakl_5FC45q7vgye6VuHk",
});
}, EMIT_INTERVAL_IN_MS);
socket.on("server to client event", () => {
packetsSinceLastReport++;
});
socket.on("disconnect", (reason) => {
console.log(`disconnect due to ${reason}`);
});
if (++clientCount < MAX_CLIENTS) {
setTimeout(createClient, CLIENT_CREATION_INTERVAL_IN_MS);
}
};
createClient();
const printReport = () => {
const now = new Date().getTime();
const durationSinceLastReport = (now - lastReport) / 1000;
const packetsPerSeconds = (
packetsSinceLastReport / durationSinceLastReport
).toFixed(2);
console.log(
`client count: ${clientCount} ; average packets received per second: ${packetsPerSeconds}`
);
packetsSinceLastReport = 0;
lastReport = now;
};
setInterval(printReport, 5000);