forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat-client.mjs
182 lines (169 loc) · 3.74 KB
/
chat-client.mjs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const env =
"process" in globalThis
? process.env
: "Deno" in globalThis
? Deno.env.toObject()
: {};
const SERVER = env.SERVER || "ws://0.0.0.0:4001";
const WebSocket = globalThis.WebSocket || (await import("ws")).WebSocket;
const LOG_MESSAGES = env.LOG_MESSAGES === "1";
const CLIENTS_TO_WAIT_FOR = parseInt(env.CLIENTS_COUNT || "", 10) || 16;
const DELAY = 64;
const MESSAGES_TO_SEND = Array.from({ length: 32 }, () => [
"Hello World!",
"Hello World! 1",
"Hello World! 2",
"Hello World! 3",
"Hello World! 4",
"Hello World! 5",
"Hello World! 6",
"Hello World! 7",
"Hello World! 8",
"Hello World! 9",
"What is the meaning of life?",
"where is the bathroom?",
"zoo",
"kangaroo",
"erlang",
"elixir",
"bun",
"mochi",
"typescript",
"javascript",
"Hello World! 7",
"Hello World! 8",
"Hello World! 9",
"What is the meaning of life?",
"where is the bathroom?",
"zoo",
"kangaroo",
"erlang",
"elixir",
"bun",
"mochi",
"typescript",
"javascript",
"Hello World! 7",
"Hello World! 8",
"Hello World! 9",
"What is the meaning of life?",
"Hello World! 7",
"Hello World! 8",
"Hello World! 9",
"What is the meaning of life?",
"where is the bathroom?",
"zoo",
"kangaroo",
"erlang",
"elixir",
"bun",
"mochi",
"typescript",
"javascript",
]).flat();
const NAMES = Array.from({ length: 50 }, (a, i) => [
"Alice" + i,
"Bob" + i,
"Charlie" + i,
"David" + i,
"Eve" + i,
"Frank" + i,
"Grace" + i,
"Heidi" + i,
"Ivan" + i,
"Judy" + i,
"Karl" + i,
"Linda" + i,
"Mike" + i,
"Nancy" + i,
"Oscar" + i,
"Peggy" + i,
"Quentin" + i,
"Ruth" + i,
"Steve" + i,
"Trudy" + i,
"Ursula" + i,
"Victor" + i,
"Wendy" + i,
"Xavier" + i,
"Yvonne" + i,
"Zach" + i,
])
.flat()
.slice(0, CLIENTS_TO_WAIT_FOR);
console.log(`Connecting ${CLIENTS_TO_WAIT_FOR} WebSocket clients...`);
console.time(`All ${CLIENTS_TO_WAIT_FOR} clients connected`);
var remainingClients = CLIENTS_TO_WAIT_FOR;
var promises = [];
const clients = new Array(CLIENTS_TO_WAIT_FOR);
for (let i = 0; i < CLIENTS_TO_WAIT_FOR; i++) {
clients[i] = new WebSocket(`${SERVER}?name=${NAMES[i]}`);
promises.push(
new Promise((resolve, reject) => {
clients[i].onmessage = (event) => {
resolve();
};
})
);
}
await Promise.all(promises);
console.timeEnd(`All ${clients.length} clients connected`);
var received = 0;
var total = 0;
var more = false;
var remaining;
for (let i = 0; i < CLIENTS_TO_WAIT_FOR; i++) {
clients[i].onmessage = (event) => {
if (LOG_MESSAGES) console.log(event.data);
received++;
remaining--;
if (remaining === 0) {
more = true;
remaining = total;
}
};
}
// each message is supposed to be received
// by each client
// so its an extra loop
for (let i = 0; i < CLIENTS_TO_WAIT_FOR; i++) {
for (let j = 0; j < MESSAGES_TO_SEND.length; j++) {
for (let k = 0; k < CLIENTS_TO_WAIT_FOR; k++) {
total++;
}
}
}
remaining = total;
function restart() {
for (let i = 0; i < CLIENTS_TO_WAIT_FOR; i++) {
for (let j = 0; j < MESSAGES_TO_SEND.length; j++) {
clients[i].send(MESSAGES_TO_SEND[j]);
}
}
}
var runs = [];
setInterval(() => {
const last = received;
runs.push(last);
received = 0;
console.log(
last,
`messages per second (${CLIENTS_TO_WAIT_FOR} clients x ${MESSAGES_TO_SEND.length} msg, min delay: ${DELAY}ms)`
);
if (runs.length >= 10) {
console.log("10 runs");
console.log(JSON.stringify(runs, null, 2));
if ("process" in globalThis) process.exit(0);
runs.length = 0;
}
}, 1000);
var isRestarting = false;
setInterval(() => {
if (more && !isRestarting) {
more = false;
isRestarting = true;
restart();
isRestarting = false;
}
}, DELAY);
restart();