Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/uv/__bench__/spawn-hook.bench.ts

Raw
import { __test } from "../index.ts";

const ROUNDS = Number(process.env["UV_BENCH_ROUNDS"] ?? 12);
const MINIMUM_ROUND_NS = Number(
	process.env["UV_BENCH_MINIMUM_ROUND_NS"] ?? 200_000_000,
);
let sink = 0;

function benchmark(name: string, operation: () => void): void {
	for (let index = 0; index < 1_000; index++) operation();
	let iterations = 1;
	while (true) {
		const started = process.hrtime.bigint();
		for (let index = 0; index < iterations; index++) operation();
		if (Number(process.hrtime.bigint() - started) >= MINIMUM_ROUND_NS) break;
		iterations *= 2;
	}
	for (let round = 0; round < ROUNDS; round++) {
		const started = process.hrtime.bigint();
		for (let index = 0; index < iterations; index++) operation();
		const elapsed = Number(process.hrtime.bigint() - started);
		console.log(
			`Benchmark${name}\t${iterations}\t${(elapsed / iterations).toFixed(0)} ns/op`,
		);
	}
}

if (!Number.isInteger(ROUNDS) || ROUNDS < 1)
	throw new Error("UV_BENCH_ROUNDS must be a positive integer");
if (!Number.isFinite(MINIMUM_ROUND_NS) || MINIMUM_ROUND_NS < 1)
	throw new Error("UV_BENCH_MINIMUM_ROUND_NS must be positive");

console.log(`goos: ${process.platform}`);
console.log(`goarch: ${process.arch}`);
console.log("pkg: pi-ext/uv");
benchmark("AllowedCommand", () => {
	sink ^= __test.getBlockedCommandMessage("git status")?.length ?? 0;
});
benchmark("BlockedPython", () => {
	sink ^=
		__test.getBlockedCommandMessage("python -m pip install rich")?.length ?? 0;
});
if (sink === 42) console.error(sink);