Files
bun.sh/bench/socketio/client.js
Ciro Spaciari fe74c948cd feat(WebSocketServer) WebSocketServer wrapper + socket.io initial support (#2880)
* WebSocketServer wrapper + socket.io initial support

* fix up backpressure

* fix up backpressure

* fix http address

* add socket.io tests

* add closing tests

* add connection state recovery tests for socket.io

* add handshake test

* add middeware tests for socket.io

* added socket.io socket middleware tests

* add more socket.io test comment/skip  hang tests

* add pending package for tests

* add server attachment servers for socket.io

* add utility-methods tests for socket.io

* rename

* rename

* add messaging-many socket.io tests

* add namespaces tests to socket.io

* skip some tests

* fmt

* add packages to general package.json
2023-05-16 08:48:17 -07:00

62 lines
1.5 KiB
JavaScript

const { io } = require("socket.io-client");
const port = process.env.PORT || 3000;
const URL = `ws://localhost:${port}`;
const MAX_CLIENTS = 250;
const BATCHSIZE = MAX_CLIENTS / 10;
const BATCH_INTERVAL_IN_MS = 1000;
const EMIT_INTERVAL_IN_MS = 50;
let clientCount = 0;
let lastReport = new Date().getTime();
let packetsSinceLastReport = 0;
const clients = [];
const createClient = () => {
const socket = io(URL);
clients.push(socket);
socket.on("server to client event", () => {
packetsSinceLastReport++;
});
socket.on("disconnect", reason => {
console.log(`disconnect due to ${reason}`);
});
};
let emitInterval = null;
const createClients = () => {
for (let i = 0; i < BATCHSIZE; i++) {
createClient();
clientCount++;
}
if (clientCount < MAX_CLIENTS) {
setTimeout(createClients, BATCH_INTERVAL_IN_MS);
}
if (!emitInterval) {
emitInterval = setInterval(() => {
clients.forEach(socket => {
socket.emit("client to server event", "hello world");
});
}, EMIT_INTERVAL_IN_MS);
}
};
createClients();
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, 1000);