Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Sep 9, 2024
1 parent 5e86207 commit 18e2482
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions benchmarks/websocket-benchmark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ experiments['undici'] = {
if (typeof WebSocketStream === 'function') {
experiments['undici - stream'] = {
fn: (ws, binary) => {
const { readable, writable } = ws
/** @type {ReadableStreamDefaultReader<string | Uint8Array>} */
const reader = readable.getReader()
const reader = ws.reader
/** @type {WritableStreamDefaultWriter<string | BufferSource>} */
const writer = writable.getWriter()
const writer = ws.writer

return async (ev) => {
ev.start()
await writer.write(binary)
Expand All @@ -95,7 +95,11 @@ if (typeof WebSocketStream === 'function') {
connect: async (url) => {
const ws = new WebSocketStream(url)

return await ws.opened
const { readable, writable } = await ws.opened
const reader = readable.getReader()
const writer = writable.getWriter()

return { reader, writer, close: () => ws.close() }
},

binaries
Expand Down Expand Up @@ -158,12 +162,16 @@ async function init () {

const ws = await connect('ws://localhost:5001')

const needShowBytes = binaries.length !== 2 || typeof binaries[0] === typeof binaries[1]
for (let i = 0; i < binaries.length; ++i) {
const binary = binaries[i]
const bytes = Buffer.byteLength(binary)

const binaryType = typeof binary === 'string' ? 'string' : 'binary'
const roundName = `${name} [${Math.floor(bytes / (1024))}Kib (${binaryType})]`
const roundName = needShowBytes
? `${name} [${Math.floor(bytes / 1024)}Kib (${binaryType})]`
: `${name} [${binaryType}]`

round[roundName] = fn(ws, binary)
experimentsInfo[roundName] = { bytes, binaryType }
}
Expand Down

0 comments on commit 18e2482

Please sign in to comment.