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
Show file tree
Hide file tree
Changes from 5 commits
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
101 changes: 101 additions & 0 deletions benchmark/websocket/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'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 crypto = require('crypto');
const http = require('http');
const { WebSocket } = require('undici');

const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';

const configs = {
size: [64, 16 * 1024, 128 * 1024, 1024 * 1024],
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
};

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

function createFrame(data, opcode) {
let infoLength = 2;
let payloadLength = data.length;

if (payloadLength >= 65536) {
infoLength += 8;
payloadLength = 127;
} else if (payloadLength > 125) {
infoLength += 2;
payloadLength = 126;
}

const info = Buffer.alloc(infoLength);

info[0] = opcode | 0x80;
info[1] = payloadLength;

if (payloadLength === 126) {
info.writeUInt16BE(data.length, 2);
} else if (payloadLength === 127) {
info[2] = info[3] = 0;
info.writeUIntBE(data.length, 4, 6);
}

return Buffer.concat([info, data]);
}

function main(conf) {
const frame = createFrame(Buffer.alloc(conf.size).fill('.'), conf.useBinary === 'true' ? 2 : 1);
const server = http.createServer();
server.on('upgrade', (req, socket) => {
const key = crypto
.createHash('sha1')
.update(req.headers['sec-websocket-key'] + GUID)
.digest('base64');

let bytesReceived = 0;
let roundtrip = 0;

socket.on('data', function onData(chunk) {
bytesReceived += chunk.length;

if (bytesReceived === frame.length + 4) { // +4 for the mask.
// Message completely received.
bytesReceived = 0;

if (++roundtrip === conf.roundtrips) {
socket.removeListener('data', onData);
socket.resume();
socket.end();
server.close();

bench.end(conf.roundtrips);
} else {
socket.write(frame);
}
}
});

socket.write(
[
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
`Sec-WebSocket-Accept: ${key}`,
'\r\n',
].join('\r\n'),
);

socket.write(frame);
});

server.listen(8080, () => {
const ws = new WebSocket('ws://localhost:8080');
Ch3nYuY marked this conversation as resolved.
Show resolved Hide resolved

ws.addEventListener('open', () => {
bench.start();
});

ws.addEventListener('message', (event) => {
ws.send(event.data);
});
});
}
13 changes: 13 additions & 0 deletions test/benchmark/test-benchmark-websocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

Check failure on line 1 in test/benchmark/test-benchmark-websocket.js

View workflow job for this annotation

GitHub Actions / test-asan

--- stderr --- node:internal/modules/cjs/loader:1147 throw err; ^ Error: Cannot find module 'undici' Require stack: - /home/runner/work/node/node/benchmark/websocket/simple.js at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15) at Module._load (node:internal/modules/cjs/loader:985:27) at Module.require (node:internal/modules/cjs/loader:1235:19) at require (node:internal/modules/helpers:176:18) at Object.<anonymous> (/home/runner/work/node/node/benchmark/websocket/simple.js:6:23) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/node/node/benchmark/websocket/simple.js' ] } Node.js v22.0.0-pre node:assert:126 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 0 at ChildProcess.<anonymous> (/home/runner/work/node/node/test/common/benchmark.js:28:12) at ChildProcess.emit (node:events:519:28) at ChildProcess._handle.onexit (node:internal/child_process:294:12) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 0, operator: 'strictEqual' } Node.js v22.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/benchmark/test-benchmark-websocket.js

Check failure on line 1 in test/benchmark/test-benchmark-websocket.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:internal/modules/cjs/loader:1147 throw err; ^ Error: Cannot find module 'undici' Require stack: - /home/runner/work/node/node/benchmark/websocket/simple.js at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15) at Module._load (node:internal/modules/cjs/loader:985:27) at Module.require (node:internal/modules/cjs/loader:1235:19) at require (node:internal/modules/helpers:176:18) at Object.<anonymous> (/home/runner/work/node/node/benchmark/websocket/simple.js:6:23) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/node/node/benchmark/websocket/simple.js' ] } Node.js v22.0.0-pre node:assert:126 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 0 at ChildProcess.<anonymous> (/home/runner/work/node/node/test/common/benchmark.js:28:12) at ChildProcess.emit (node:events:519:28) at ChildProcess._handle.onexit (node:internal/child_process:294:12) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 0, operator: 'strictEqual' } Node.js v22.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/benchmark/test-benchmark-websocket.js

Check failure on line 1 in test/benchmark/test-benchmark-websocket.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:internal/modules/cjs/loader:1147 throw err; ^ Error: Cannot find module 'undici' Require stack: - /Users/runner/work/node/node/benchmark/websocket/simple.js at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15) at Module._load (node:internal/modules/cjs/loader:985:27) at Module.require (node:internal/modules/cjs/loader:1235:19) at require (node:internal/modules/helpers:176:18) at Object.<anonymous> (/Users/runner/work/node/node/benchmark/websocket/simple.js:6:23) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) { code: 'MODULE_NOT_FOUND', requireStack: [ '/Users/runner/work/node/node/benchmark/websocket/simple.js' ] } Node.js v22.0.0-pre node:assert:126 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 1 !== 0 at ChildProcess.<anonymous> (/Users/runner/work/node/node/test/common/benchmark.js:28:12) at ChildProcess.emit (node:events:519:28) at ChildProcess._handle.onexit (node:internal/child_process:294:12) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 1, expected: 0, operator: 'strictEqual' } Node.js v22.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/benchmark/test-benchmark-websocket.js

const common = require('../common');
if (!common.enoughTestMem)
common.skip('Insufficient memory for Websocket benchmark test');

// Because the websocket benchmarks use hardcoded ports, this should be in sequential
// rather than parallel to make sure it does not conflict with tests that choose
// random available ports.
Ch3nYuY marked this conversation as resolved.
Show resolved Hide resolved

const runBenchmark = require('../common/benchmark');

runBenchmark('websocket', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
Loading