Skip to content

Commit

Permalink
benchmark: add undici websocket benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Chenyu Yang committed Nov 7, 2023
1 parent 33704c4 commit f2cc56d
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions benchmark/websocket/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const common = require('../common.js');
const http = require('http');
const WebSocketServer = require('ws');
const { WebSocket } = require('undici');

const port = 8181;
const path = '';

const configs = {
useBinary: ['true', 'false'],
roundtrips: [5000, 1000, 100, 1],
size: [64, 16 * 1024, 128 * 1024, 1024 * 1024],
};

const bench = common.createBenchmark(main, configs);

function main(conf) {
const server = http.createServer();
const wss = new WebSocketServer.Server({
maxPayload: 600 * 1024 * 1024,
perMessageDeflate: false,
clientTracking: false,
server,
});

wss.on('connection', (ws) => {
ws.on('message', (data, isBinary) => {
ws.send(data, { binary: isBinary });
});
});

server.listen(path ? { path } : { port });
const url = path ? `ws+unix://${path}` : `ws://localhost:${port}`;
const wsc = new WebSocket(url);
const data = Buffer.allocUnsafe(conf.size).fill('.'); // Pre-fill data for testing

let roundtrip = 0;

wsc.addEventListener('error', (err) => {
throw err;
});

bench.start();

wsc.addEventListener('open', () => {
wsc.send(data, { binary: conf.useBinary });
});

wsc.addEventListener('close', () => {
wss.close(() => {
server.close();
});
});

wsc.addEventListener('message', () => {
if (++roundtrip !== conf.roundtrips) {
wsc.send(data, { binary: conf.useBinary });
} else {
bench.end(conf.roundtrips);
wsc.close();
}
});
}

0 comments on commit f2cc56d

Please sign in to comment.