Skip to content

Commit e249b3b

Browse files
committed
refactor
1 parent 9823678 commit e249b3b

3 files changed

Lines changed: 62 additions & 62 deletions

File tree

File renamed without changes.

benchmarks/websocket-benchmark.mjs

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
import { bench } from './_util/runner.js'
44
import { WebSocket } from '../index.js'
5+
import { WebSocket as WsWebSocket } from 'ws'
56

67
/**
78
* @typedef {object} WebSocketImpl
89
* @property {(buffer: Uint8Array | string) => void} send
9-
* @property {(name: string, listener: (...args: any) => void, options?: { once: boolean }) => void} addEventListener
10-
* @property {(name: string, listener: (...args: any) => void) => void} removeEventListener
1110
* @property {(code?: number, reason?: string) => void} close
1211
* @property {string} binaryType
1312
*/
1413

1514
/**
16-
* @type {Record<string, { fn: (ws: WebSocketImpl, binary: string | Uint8Array) => import('./_util/runner.js').BenchMarkHandler; connect: () => Promise<WebSocketImpl>; binaries: (string | Uint8Array)[] }>}
15+
* @type {Record<string, { fn: (ws: WebSocketImpl, binary: string | Uint8Array) => import('./_util/runner.js').BenchMarkHandler; connect: (url: string) => Promise<WebSocketImpl>; binaries: (string | Uint8Array)[] }>}
1716
*/
1817
const experiments = {}
1918
/**
@@ -27,38 +26,42 @@ const experimentsInfo = {}
2726
const connections = []
2827

2928
const binary = Buffer.alloc(1024 * 256, '_')
29+
const binaries = [binary, binary.toString('utf-8')]
3030

3131
experiments['undici'] = {
32-
fn: (ws, binary) => async (ev) => {
33-
await /** @type {Promise<void>} */(new Promise((resolve, _reject) => {
32+
fn: (ws, binary) => {
33+
if (!(ws instanceof WebSocket)) {
34+
throw new Error("'undici' websocket are expected.")
35+
}
36+
37+
return (ev) => {
3438
ws.addEventListener(
3539
'message',
3640
() => {
3741
ev.end()
38-
resolve()
3942
},
4043
{ once: true }
4144
)
4245

4346
ev.start()
4447
ws.send(binary)
45-
}))
48+
}
4649
},
4750

48-
connect: async () => {
49-
const ws = new WebSocket('ws://localhost:5001')
51+
connect: async (url) => {
52+
const ws = new WebSocket(url)
5053

5154
await /** @type {Promise<void>} */ (
5255
new Promise((resolve, reject) => {
5356
function onOpen () {
5457
resolve()
55-
this.removeEventListener('open', onOpen)
56-
this.removeEventListener('error', onError)
58+
ws.removeEventListener('open', onOpen)
59+
ws.removeEventListener('error', onError)
5760
}
5861
function onError (err) {
5962
reject(err)
60-
this.removeEventListener('open', onOpen)
61-
this.removeEventListener('error', onError)
63+
ws.removeEventListener('open', onOpen)
64+
ws.removeEventListener('error', onError)
6265
}
6366
ws.addEventListener('open', onOpen)
6467
ws.addEventListener('error', onError)
@@ -71,7 +74,50 @@ experiments['undici'] = {
7174
return ws
7275
},
7376

74-
binaries: [binary, binary.toString('utf-8')]
77+
binaries
78+
}
79+
80+
experiments['ws'] = {
81+
fn: (ws, binary) => {
82+
if (!(ws instanceof WsWebSocket)) {
83+
throw new Error("'ws' websocket are expected.")
84+
}
85+
86+
return (ev) => {
87+
ws.once('message', () => {
88+
ev.end()
89+
})
90+
ev.start()
91+
ws.send(binary)
92+
}
93+
},
94+
95+
connect: async (url) => {
96+
const ws = new WsWebSocket(url)
97+
98+
await /** @type {Promise<void>} */ (
99+
new Promise((resolve, reject) => {
100+
function onOpen () {
101+
resolve()
102+
ws.off('open', onOpen)
103+
ws.off('error', onError)
104+
}
105+
function onError (err) {
106+
reject(err)
107+
ws.off('open', onOpen)
108+
ws.off('error', onError)
109+
}
110+
ws.on('open', onOpen)
111+
ws.on('error', onError)
112+
})
113+
)
114+
115+
ws.binaryType = 'arraybuffer'
116+
117+
return ws
118+
},
119+
120+
binaries
75121
}
76122

77123
async function init () {
@@ -85,14 +131,14 @@ async function init () {
85131

86132
const { fn, connect, binaries } = experiments[name]
87133

88-
const ws = await connect()
134+
const ws = await connect('ws://localhost:5001')
89135

90136
for (let i = 0; i < binaries.length; ++i) {
91137
const binary = binaries[i]
92138
const bytes = Buffer.byteLength(binary)
93139

94140
const binaryType = typeof binary === 'string' ? 'string' : 'binary'
95-
const roundName = `${name} [${(bytes / (1024)).toFixed(2)}Kib (${binaryType})]`
141+
const roundName = `${name} [${Math.floor(bytes / (1024))}Kib (${binaryType})]`
96142
round[roundName] = fn(ws, binary)
97143
experimentsInfo[roundName] = { bytes, binaryType }
98144
}

benchmarks/websocket/server/simple.mjs

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)