|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import { bench } from './_util/runner.js' |
| 4 | +import { formatBytes } from './_util/index.js' |
| 5 | +import { WebSocket, WebSocketStream } from '../index.js' |
| 6 | +import { WebSocket as WsWebSocket } from 'ws' |
| 7 | + |
| 8 | +/** |
| 9 | + * @type {Record<string, { fn: (ws: any, binary: string | Uint8Array) => import('./_util/runner.js').BenchMarkHandler; connect: (url: string) => Promise<any>; binaries: (string | Uint8Array)[] }>} |
| 10 | + */ |
| 11 | +const experiments = {} |
| 12 | +/** |
| 13 | + * @type {Record<string, { bytes: number; binaryType: 'string' | 'binary' }>} |
| 14 | + */ |
| 15 | +const experimentsInfo = {} |
| 16 | + |
| 17 | +/** |
| 18 | + * @type {any[]} |
| 19 | + */ |
| 20 | +const connections = [] |
| 21 | + |
| 22 | +const binary = Buffer.alloc(256 * 1024, '_') |
| 23 | +const binaries = [binary, binary.subarray(0, 256 * 1024).toString('utf-8')] |
| 24 | + |
| 25 | +experiments['undici'] = { |
| 26 | + fn: (ws, binary) => { |
| 27 | + if (!(ws instanceof WebSocket)) { |
| 28 | + throw new Error("'undici' websocket are expected.") |
| 29 | + } |
| 30 | + |
| 31 | + return (ev) => { |
| 32 | + ws.addEventListener( |
| 33 | + 'message', |
| 34 | + () => { |
| 35 | + ev.end() |
| 36 | + }, |
| 37 | + { once: true } |
| 38 | + ) |
| 39 | + |
| 40 | + ev.start() |
| 41 | + ws.send(binary) |
| 42 | + } |
| 43 | + }, |
| 44 | + |
| 45 | + connect: async (url) => { |
| 46 | + const ws = new WebSocket(url) |
| 47 | + |
| 48 | + await /** @type {Promise<void>} */ ( |
| 49 | + new Promise((resolve, reject) => { |
| 50 | + function onOpen () { |
| 51 | + resolve() |
| 52 | + ws.removeEventListener('open', onOpen) |
| 53 | + ws.removeEventListener('error', onError) |
| 54 | + } |
| 55 | + function onError (err) { |
| 56 | + reject(err) |
| 57 | + ws.removeEventListener('open', onOpen) |
| 58 | + ws.removeEventListener('error', onError) |
| 59 | + } |
| 60 | + ws.addEventListener('open', onOpen) |
| 61 | + ws.addEventListener('error', onError) |
| 62 | + }) |
| 63 | + ) |
| 64 | + |
| 65 | + // avoid create blob |
| 66 | + ws.binaryType = 'arraybuffer' |
| 67 | + |
| 68 | + return ws |
| 69 | + }, |
| 70 | + |
| 71 | + binaries |
| 72 | +} |
| 73 | + |
| 74 | +experiments['undici - stream'] = { |
| 75 | + fn: (ws, binary) => { |
| 76 | + /** @type {ReadableStreamDefaultReader<string | Uint8Array>} */ |
| 77 | + const reader = ws.reader |
| 78 | + /** @type {WritableStreamDefaultWriter<string | BufferSource>} */ |
| 79 | + const writer = ws.writer |
| 80 | + |
| 81 | + return async (ev) => { |
| 82 | + ev.start() |
| 83 | + await writer.write(binary) |
| 84 | + await reader.read() |
| 85 | + ev.end() |
| 86 | + } |
| 87 | + }, |
| 88 | + |
| 89 | + connect: async (url) => { |
| 90 | + const ws = new WebSocketStream(url) |
| 91 | + |
| 92 | + const { readable, writable } = await ws.opened |
| 93 | + const reader = readable.getReader() |
| 94 | + const writer = writable.getWriter() |
| 95 | + |
| 96 | + // @ts-ignore |
| 97 | + return { reader, writer, close: () => ws.close() } |
| 98 | + }, |
| 99 | + |
| 100 | + binaries |
| 101 | +} |
| 102 | + |
| 103 | +experiments['ws'] = { |
| 104 | + fn: (ws, binary) => { |
| 105 | + if (!(ws instanceof WsWebSocket)) { |
| 106 | + throw new Error("'ws' websocket are expected.") |
| 107 | + } |
| 108 | + |
| 109 | + return (ev) => { |
| 110 | + ws.once('message', () => { |
| 111 | + ev.end() |
| 112 | + }) |
| 113 | + ev.start() |
| 114 | + ws.send(binary) |
| 115 | + } |
| 116 | + }, |
| 117 | + |
| 118 | + connect: async (url) => { |
| 119 | + const ws = new WsWebSocket(url, { maxPayload: 1024 * 1024 * 1024 }) |
| 120 | + |
| 121 | + await /** @type {Promise<void>} */ ( |
| 122 | + new Promise((resolve, reject) => { |
| 123 | + function onOpen () { |
| 124 | + resolve() |
| 125 | + ws.off('open', onOpen) |
| 126 | + ws.off('error', onError) |
| 127 | + } |
| 128 | + function onError (err) { |
| 129 | + reject(err) |
| 130 | + ws.off('open', onOpen) |
| 131 | + ws.off('error', onError) |
| 132 | + } |
| 133 | + ws.on('open', onOpen) |
| 134 | + ws.on('error', onError) |
| 135 | + }) |
| 136 | + ) |
| 137 | + |
| 138 | + ws.binaryType = 'arraybuffer' |
| 139 | + |
| 140 | + return ws |
| 141 | + }, |
| 142 | + |
| 143 | + binaries |
| 144 | +} |
| 145 | + |
| 146 | +async function init () { |
| 147 | + /** @type {Record<string, import('./_util/runner.js').BenchMarkHandler>} */ |
| 148 | + const round = {} |
| 149 | + |
| 150 | + const keys = Object.keys(experiments) |
| 151 | + |
| 152 | + for (let i = 0; i < keys.length; ++i) { |
| 153 | + const name = keys[i] |
| 154 | + |
| 155 | + const { fn, connect, binaries } = experiments[name] |
| 156 | + |
| 157 | + const ws = await connect('ws://localhost:8080') |
| 158 | + |
| 159 | + const needShowBytes = binaries.length !== 2 || typeof binaries[0] === typeof binaries[1] |
| 160 | + for (let i = 0; i < binaries.length; ++i) { |
| 161 | + const binary = binaries[i] |
| 162 | + const bytes = Buffer.byteLength(binary) |
| 163 | + |
| 164 | + const binaryType = typeof binary === 'string' ? 'string' : 'binary' |
| 165 | + const roundName = needShowBytes |
| 166 | + ? `${name} [${formatBytes(bytes)} (${binaryType})]` |
| 167 | + : `${name} [${binaryType}]` |
| 168 | + |
| 169 | + round[roundName] = fn(ws, binary) |
| 170 | + experimentsInfo[roundName] = { bytes, binaryType } |
| 171 | + } |
| 172 | + |
| 173 | + connections.push(ws) |
| 174 | + } |
| 175 | + |
| 176 | + return round |
| 177 | +} |
| 178 | + |
| 179 | +init() |
| 180 | + .then((round) => bench(round, { |
| 181 | + minSamples: 512 |
| 182 | + })) |
| 183 | + .then((results) => { |
| 184 | + print(results) |
| 185 | + |
| 186 | + for (const ws of connections) { |
| 187 | + ws.close() |
| 188 | + } |
| 189 | + }, (err) => { |
| 190 | + process.nextTick((err) => { |
| 191 | + throw err |
| 192 | + }, err) |
| 193 | + }) |
| 194 | + |
| 195 | +/** |
| 196 | + * @param {{ name: string; average: number; iterationPerSecond: number; }[]} results |
| 197 | + */ |
| 198 | +function print (results) { |
| 199 | + for (const { name, average, iterationPerSecond } of results) { |
| 200 | + const { bytes } = experimentsInfo[name] |
| 201 | + |
| 202 | + console.log( |
| 203 | + `${name}: transferred ${formatBytes((bytes / average) * 1e9)} Bytes/s (${iterationPerSecond.toFixed(4)} per/sec)` |
| 204 | + ) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +export {} |
0 commit comments