Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Comment thread
Ch3nYuY marked this conversation as resolved.
Comment thread
Ch3nYuY marked this conversation as resolved.

const common = require('../common.js');
const http = require('http');
const WebSocketServer = require('ws');
Comment thread
Ch3nYuY marked this conversation as resolved.
Outdated
const { WebSocket } = require('undici');

const port = 8181;
const path = '';

const configs = {
useBinary: ['true', 'false'],
Comment thread
Ch3nYuY marked this conversation as resolved.
roundtrips: [5000, 1000, 100, 1],
Comment thread
Ch3nYuY marked this conversation as resolved.
Outdated
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}`;
Comment thread
Ch3nYuY marked this conversation as resolved.
Outdated
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 });
Comment thread
Ch3nYuY marked this conversation as resolved.
Outdated
} else {
bench.end(conf.roundtrips);
wsc.close();
}
});
}
Comment thread
Ch3nYuY marked this conversation as resolved.