Skip to content

Instantly share code, notes, and snippets.

@shahidcodes
Created June 28, 2022 15:53
Show Gist options
  • Save shahidcodes/99326d3b98d2af9080719d72070c932c to your computer and use it in GitHub Desktop.
Save shahidcodes/99326d3b98d2af9080719d72070c932c to your computer and use it in GitHub Desktop.
Socket.io benchmark
const io = require("socket.io-client");
const createLogger =
(index) =>
(...rest) =>
console.log(`[${index}]`, ...rest);
const token = "";
const url = "";
let result = {
connected: 0,
clientCreated: 0,
};
function createClient(id) {
const log = createLogger(id);
const socket = io(url, {
auth: {
token,
},
transports: ["websocket"],
});
socket.on("connect", () => {
result.connected += 1;
console.log("connected", result.connected);
});
socket.on("error", (err) => {
log("socket error", err);
console.log(err);
});
socket.on("disconnect", () => {
// log("disconnect");
result.connected -= 1;
console.log("disconnected, remining", result.connected);
});
// socket.on("message", (args) => {
// console.log("message from server", args);
// });
socket.onAny((event, args) => {
console.log(`any ${event},`, args);
});
// setInterval(() => {
// socket.emit("ping");
// }, 4000);
result.clientCreated += 1;
}
let clientIds = [];
for (let index = 0; index < 2000; index++) {
clientIds.push(index);
}
setInterval(() => {
console.log(`created=${result.clientCreated}; connected=${result.connected}`);
}, 1000);
clientIds.map((id) => createClient(id));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment