Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: add undici websocket benchmark #50586

Merged
merged 8 commits into from
Dec 6, 2023
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';
Ch3nYuY marked this conversation as resolved.
Show resolved Hide resolved
Ch3nYuY marked this conversation as resolved.
Show resolved Hide resolved

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

const port = 8181;
const path = '';

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