Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Last active November 8, 2022 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattSandy/7a5ca0314b343407d30a6fd9affe7cfd to your computer and use it in GitHub Desktop.
Save MattSandy/7a5ca0314b343407d30a6fd9affe7cfd to your computer and use it in GitHub Desktop.
SocketIO Client and Server Performance

SocketIO Performance Tests for Server and Client

Logs milliseconds for each leg of a websocket event.

const io = require("socket.io-client");
const socket = io.connect('http://127.0.0.1:3000', { transports: ['websocket'] });
let startTime;
const now = () => {
const hrTime = process.hrtime();
return hrTime[0] * 1000 + hrTime[1] / 1000000;
};
setInterval(function () {
startTime = now();
socket.emit('ping',now());
}, 2000);
socket.on('pong', function (time) {
let t = now();
console.log({
'ping': t - startTime,
'pong': t - time
});
});
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const now = () => {
const hrTime = process.hrtime();
return hrTime[0] * 1000 + hrTime[1] / 1000000;
};
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.on('ping', function (time) {
console.log(time);
socket.emit('pong', now());
console.log({
'ping': now() - time,
})
});
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment