forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add undici websocket benchmark
Refs: nodejs/performance#114
- Loading branch information
Chenyu Yang
committed
Nov 7, 2023
1 parent
33704c4
commit f2cc56d
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} |