-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
websocket: optimize masking using WebAssembly
- Loading branch information
Showing
5 changed files
with
194 additions
and
5 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,89 @@ | ||
// --------------------SERVER-------------------- | ||
// -> /server/simple.mjs | ||
// ---------------------------------------------- | ||
|
||
import { WebSocket as WsWebSocket } from 'ws' | ||
import { WebSocket as UndiciWebSocket } from '../../index.js' | ||
import { randomBytes } from 'node:crypto' | ||
import { bench, run, group } from 'mitata' | ||
|
||
process.env.UNDICI_USE_WS_MASKING = true | ||
|
||
const __GLOBAL_WEBSOCKET__ = false | ||
const __BINARY_SIZE__ = 1024 * 256 | ||
|
||
let GlobalWebSocket = null | ||
|
||
if (__GLOBAL_WEBSOCKET__ && typeof globalThis.WebSocket === 'function') { | ||
GlobalWebSocket = globalThis.WebSocket | ||
} | ||
|
||
const binary = randomBytes(__BINARY_SIZE__) | ||
|
||
const url = 'http://localhost:5001' | ||
|
||
const connections = [] | ||
|
||
group('send', () => { | ||
{ | ||
const ws = new WsWebSocket(url) | ||
let _resolve | ||
ws.addEventListener('message', () => { | ||
_resolve() | ||
}) | ||
bench('ws', () => { | ||
return new Promise((resolve, reject) => { | ||
ws.send(binary) | ||
_resolve = resolve | ||
}) | ||
}) | ||
connections.push(ws) | ||
} | ||
{ | ||
const ws = new UndiciWebSocket(url) | ||
let _resolve | ||
ws.addEventListener('message', () => { | ||
_resolve() | ||
}) | ||
bench('undici', () => { | ||
return new Promise((resolve, reject) => { | ||
ws.send(binary) | ||
_resolve = resolve | ||
}) | ||
}) | ||
connections.push(ws) | ||
} | ||
if (typeof GlobalWebSocket === 'function') { | ||
const ws = new GlobalWebSocket(url) | ||
let _resolve | ||
ws.addEventListener('message', () => { | ||
_resolve() | ||
}) | ||
bench('undici - global', () => { | ||
return new Promise((resolve, reject) => { | ||
ws.send(binary) | ||
_resolve = resolve | ||
}) | ||
}) | ||
connections.push(ws) | ||
} | ||
}) | ||
|
||
for (const ws of connections) { | ||
// for fairness | ||
ws.binaryType = 'arraybuffer' | ||
await new Promise((resolve, reject) => { | ||
ws.addEventListener('open', () => { | ||
resolve() | ||
}) | ||
ws.addEventListener('error', (err) => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
|
||
await run() | ||
|
||
for (const ws of connections) { | ||
ws.close() | ||
} |
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
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
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
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,50 @@ | ||
'use strict' | ||
|
||
const { once } = require('node:events') | ||
const { WebSocket } = require('../..') | ||
const { test } = require('node:test') | ||
const { closeServerAsPromise } = require('../utils/node-http') | ||
const { strictEqual } = require('node:assert') | ||
const { WebSocketServer } = require('ws') | ||
|
||
test('WebSocket optimization using `ws-masking` is experimental, emit warning', async (t) => { | ||
process.env.UNDICI_USE_WS_MASKING = true | ||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (socket) => { | ||
socket.onmessage = (ev) => { | ||
socket.send('Hi') | ||
socket.close() | ||
} | ||
}) | ||
|
||
await once(server, 'listening') | ||
|
||
t.after(closeServerAsPromise(server)) | ||
|
||
let warningEmitted = false | ||
function onWarning () { | ||
warningEmitted = true | ||
} | ||
process.on('warning', onWarning) | ||
t.after(() => { | ||
delete process.env.UNDICI_USE_WS_MASKING | ||
process.off('warning', onWarning) | ||
}) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
|
||
await new Promise((resolve, reject) => { | ||
ws.onopen = () => { | ||
ws.send('Hi') | ||
} | ||
ws.onmessage = (ev) => { | ||
resolve() | ||
} | ||
ws.onerror = reject | ||
}) | ||
|
||
ws.close() | ||
|
||
strictEqual(warningEmitted, true) | ||
}) |