import path from "node:path"; import { createPiSession } from "../../../test/fixture/create-pi-session.ts"; import { findNu } from "../runtime.ts"; const rounds = Number(process.env["NUSHELL_BENCH_ROUNDS"] ?? 25); const warmups = Number(process.env["NUSHELL_BENCH_WARMUPS"] ?? 3); if ( !Number.isSafeInteger(rounds) || rounds < 1 || !Number.isSafeInteger(warmups) || warmups < 0 ) throw new Error( "Benchmark rounds must be positive integers; warmups may be zero", ); if (!findNu()) throw new Error("nu executable missing on PATH"); const session = await createPiSession({ cwd: process.cwd(), extensionPaths: [path.resolve(import.meta.dirname, "../index.ts")], }); try { const runner = session.session.extensionRunner; const fresh = session.extensions[0]?.tools.get("nu")?.definition; if (!fresh) throw new Error("Nushell fresh tool not registered"); const exec = async () => { const result = await fresh.execute( "bench", { command: "1 + 2" }, undefined, undefined, runner.createToolContext("bench", undefined), ); if ( !result.content.some( (part: { type: string; text?: string }) => part.type === "text" && (part.text?.includes("3") ?? false), ) ) throw new Error("Incorrect benchmark output"); }; const measure = async (record: boolean) => { const start = process.hrtime.bigint(); await exec(); if (record) console.log( `BenchmarkNuFresh\t1\t${process.hrtime.bigint() - start} ns/op`, ); }; console.log(`goos: ${process.platform}`); console.log(`goarch: ${process.arch}`); console.log("pkg: pi-ext/nushell"); console.log(`node: ${process.version}`); for (let index = -warmups; index < rounds; index++) await measure(index >= 0); } finally { await session.dispose(); }