Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Sep 18, 2024
1 parent e2daa55 commit 6f1e7ef
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
18 changes: 17 additions & 1 deletion benchmarks/_util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,20 @@ function printResults (results) {
return console.table(rows)
}

module.exports = { makeParallelRequests, printResults }
/**
* @param {number} num
* @returns {string}
*/
function formatBytes (num) {
if (!Number.isFinite(num)) {
throw new Error('invalid number')
}

const prefixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']

const idx = Math.min(Math.floor(Math.log(num) / Math.log(1024)), prefixes.length - 1)

return `${(num / Math.pow(1024, idx)).toFixed(2)}${prefixes[idx]}`
}

module.exports = { makeParallelRequests, printResults, formatBytes }
70 changes: 34 additions & 36 deletions benchmarks/websocket-benchmark.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// @ts-check

import { bench } from './_util/runner.js'
import { formatBytes } from './_util/index.js'
import * as undici from '../index.js'
import { WebSocket as WsWebSocket } from 'ws'

const {
WebSocket,
// @ts-ignore https://github.com/nodejs/undici/pull/3560
WebSocketStream
} = undici
const { WebSocket } = undici

/**
* @type {Record<string, { fn: (ws: any, binary: string | Uint8Array) => import('./_util/runner.js').BenchMarkHandler; connect: (url: string) => Promise<any>; binaries: (string | Uint8Array)[] }>}
Expand Down Expand Up @@ -76,35 +73,36 @@ experiments['undici'] = {
binaries
}

if (typeof WebSocketStream === 'function') {
experiments['undici - stream'] = {
fn: (ws, binary) => {
/** @type {ReadableStreamDefaultReader<string | Uint8Array>} */
const reader = ws.reader
/** @type {WritableStreamDefaultWriter<string | BufferSource>} */
const writer = ws.writer

return async (ev) => {
ev.start()
await writer.write(binary)
await reader.read()
ev.end()
}
},

connect: async (url) => {
const ws = new WebSocketStream(url)

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

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

binaries
}
}
// https://github.com/nodejs/undici/pull/3560
// if (typeof WebSocketStream === 'function') {
// experiments['undici - stream'] = {
// fn: (ws, binary) => {
// /** @type {ReadableStreamDefaultReader<string | Uint8Array>} */
// const reader = ws.reader
// /** @type {WritableStreamDefaultWriter<string | BufferSource>} */
// const writer = ws.writer

// return async (ev) => {
// ev.start()
// await writer.write(binary)
// await reader.read()
// ev.end()
// }
// },

// connect: async (url) => {
// const ws = new WebSocketStream(url)

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

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

// binaries
// }
// }

experiments['ws'] = {
fn: (ws, binary) => {
Expand Down Expand Up @@ -169,7 +167,7 @@ async function init () {

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

round[roundName] = fn(ws, binary)
Expand Down Expand Up @@ -204,7 +202,7 @@ function print (results) {
const { bytes } = experimentsInfo[name]

console.log(
`${name} -> ${(((bytes / average) * 1e9) / (1024 * 1024)).toFixed(2)}MiB/s`
`${name} -> ${formatBytes((bytes / average) * 1e9)}/s`
)
}
}
Expand Down

0 comments on commit 6f1e7ef

Please sign in to comment.