-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
236 additions
and
36 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 |
---|---|---|
|
@@ -76,4 +76,4 @@ 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
//@ts-check | ||
|
||
"use strict"; | ||
Check failure on line 3 in benchmarks/_util/runner.js GitHub Actions / Lint
|
||
|
||
class Info { | ||
/**@type {string} */ | ||
#name; | ||
/**@type {bigint} */ | ||
#current; | ||
/**@type {bigint} */ | ||
#finish; | ||
/**@type {(...args: any[]) => any} */ | ||
#callback; | ||
/**@type {boolean} */ | ||
#finalized = false; | ||
|
||
/** | ||
* @param {string} name | ||
* @param {(...args: any[]) => any} callback | ||
*/ | ||
constructor(name, callback) { | ||
this.#name = name; | ||
this.#callback = callback; | ||
} | ||
|
||
get name() { | ||
return this.#name; | ||
} | ||
|
||
start() { | ||
if (this.#finalized) { | ||
throw new TypeError("called after finished."); | ||
} | ||
this.#current = process.hrtime.bigint(); | ||
} | ||
|
||
end() { | ||
if (this.#finalized) { | ||
throw new TypeError("called after finished."); | ||
} | ||
this.#finish = process.hrtime.bigint(); | ||
this.#finalized = true; | ||
this.#callback(); | ||
} | ||
|
||
diff() { | ||
return Number(this.#finish - this.#current); | ||
} | ||
} | ||
|
||
/** | ||
* @typedef BenchMarkHandler | ||
* @type {(ev: { name: string; start(): void; end(): void; }) => any} | ||
*/ | ||
|
||
/** | ||
* @param {Record<string, BenchMarkHandler>} experiments | ||
* @param {{}} [options] | ||
* @returns {Promise<{ name: string; average: number; samples: number; fn: BenchMarkHandler; min: number; max: number }[]>} | ||
*/ | ||
async function bench(experiments, options = {}) { | ||
const names = Object.keys(experiments); | ||
|
||
/**@type {{ name: string; average: number; samples: number; fn: BenchMarkHandler; min: number; max: number }[]} */ | ||
const results = []; | ||
|
||
async function waitMaybePromiseLike(p) { | ||
if ( | ||
(typeof p === "object" || typeof p === "function") && | ||
p !== null && | ||
typeof p.then === "function" | ||
) { | ||
await p; | ||
} | ||
} | ||
|
||
for (let i = 0; i < names.length; ++i) { | ||
const name = names[i]; | ||
const fn = experiments[name]; | ||
const samples = []; | ||
|
||
let timing = 0; | ||
|
||
for (let j = 0; j < 128 || timing < 800_000_000; ++j) { | ||
let resolve = (value) => {}, | ||
reject = (reason) => {}, | ||
promise = new Promise( | ||
(done, fail) => ((resolve = done), (reject = fail)) | ||
); | ||
|
||
const info = new Info(name, resolve); | ||
|
||
try { | ||
const p = fn(info); | ||
|
||
await waitMaybePromiseLike(p); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
|
||
await promise; | ||
|
||
samples.push({ time: 1e6 * info.diff() }); | ||
|
||
timing += info.diff(); | ||
} | ||
|
||
const average = | ||
samples.map((v) => v.time).reduce((a, b) => a + b, 0) / samples.length; | ||
|
||
results.push({ | ||
name: names[i], | ||
average: average, | ||
samples: samples.length, | ||
fn: fn, | ||
min: samples.reduce((a, acc) => Math.min(a, acc.time), samples[0].time), | ||
max: samples.reduce((a, acc) => Math.max(a, acc.time), samples[0].time), | ||
}); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
function print() { | ||
|
||
} | ||
|
||
module.exports = { bench }; |
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 @@ | ||
import './websocket/server/simple.mjs' |
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 |
---|---|---|
@@ -1,3 +1,94 @@ | ||
import { cronometro } from "cronometro" | ||
//@ts-check | ||
|
||
// TODO | ||
import { bench } from "./_util/runner.js"; | ||
import { WebSocket } from "../index.js"; | ||
import { randomBytes } from "node:crypto"; | ||
const __BINARY_SIZE__ = 1024 * 256; | ||
|
||
const binary = randomBytes(__BINARY_SIZE__); | ||
|
||
/** | ||
* @param {{ | ||
* send(buffer: Uint8Array | string): void; | ||
* addEventListener(name: string, listener: (...args: any) => void, options?: { once: boolean }): void | ||
* }} socket | ||
* @param {Uint8Array | string} data | ||
* @param {(...args: any) => any} callback | ||
*/ | ||
function waitWrite(socket, data, callback) { | ||
return /** @type {Promise<void>} */(new Promise((resolve, reject) => { | ||
socket.send(data); | ||
|
||
socket.addEventListener( | ||
"message", | ||
(ev) => { | ||
resolve(); | ||
callback(); | ||
}, | ||
{ once: true } | ||
); | ||
})) | ||
} | ||
|
||
/**@type {Record<string, import('./_util/runner.js').BenchMarkHandler} */ | ||
const experiments = {}; | ||
|
||
/**@type {WebSocket | null} */ | ||
let ws; | ||
|
||
experiments["sending 256Kib (undici)"] = async function (ev) { | ||
if (ws === null) { | ||
throw new Error("called before initialized"); | ||
} | ||
ev.start(); | ||
await waitWrite(ws, binary, () => { | ||
ev.end(); | ||
}); | ||
}; | ||
|
||
async function connect() { | ||
ws = new WebSocket("ws://localhost:5001"); | ||
|
||
await /**@type {Promise<void>} */ ( | ||
new Promise((resolve, reject) => { | ||
if (ws === null) { | ||
return void reject(new Error("called before initialized")); | ||
} | ||
function onOpen() { | ||
resolve(); | ||
this.removeEventListener("open", onOpen); | ||
this.removeEventListener("error", onError); | ||
} | ||
function onError(err) { | ||
reject(err); | ||
this.removeEventListener("open", onOpen); | ||
this.removeEventListener("error", onError); | ||
} | ||
ws.addEventListener("open", onOpen); | ||
ws.addEventListener("error", onError); | ||
}) | ||
); | ||
} | ||
|
||
connect() | ||
.then(() => bench(experiments, {})) | ||
.then((results) => { | ||
if (ws === null) { | ||
throw new Error("called before initialized"); | ||
} | ||
|
||
ws.close(); | ||
|
||
console.log(results); | ||
}) | ||
.catch((err) => { | ||
process.nextTick((err) => { | ||
throw err; | ||
}, err); | ||
}); | ||
|
||
function print(results) { | ||
|
||
} | ||
|
||
export {}; |
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