Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions bench/golden.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import {
RunState,
buildIntervention,
Expand Down Expand Up @@ -195,6 +196,24 @@ export async function replayFixture(fixture: GoldenFixture): Promise<GoldenRepla
};
}

/**
* Golden corpus directory (`fixtures/golden`), resolved relative to this
* module — never the process cwd — so `lexloop bench golden` works from any
* working directory. The module URL lands in different spots depending on
* layout (repo `bench/` under tsx/vitest, `packages/cli/dist/` once bundled),
* so walk upward until a `fixtures/golden` directory exists.
*/
export function defaultGoldenDir(): string {
let dir = dirname(fileURLToPath(import.meta.url));
for (;;) {
const candidate = join(dir, "fixtures", "golden");
if (existsSync(candidate)) return candidate;
const parent = dirname(dir);
if (parent === dir) return resolve("fixtures", "golden");
dir = parent;
}
}

function listJsonFiles(dir: string): string[] {
try {
return readdirSync(dir)
Expand All @@ -214,12 +233,22 @@ function loadFile(path: string): GoldenFixture {
return loadGoldenFixture(raw, name);
}

export async function runGoldenBench(dir = "fixtures/golden"): Promise<{
export async function runGoldenBench(dir?: string): Promise<{
result: GoldenBenchResult;
replays: GoldenReplay[];
}> {
const loopFiles = listJsonFiles(join(dir, "loops"));
const productiveFiles = listJsonFiles(join(dir, "productive"));
const root = dir ?? defaultGoldenDir();
const loopFiles = listJsonFiles(join(root, "loops"));
const productiveFiles = listJsonFiles(join(root, "productive"));
if (loopFiles.length === 0 && productiveFiles.length === 0) {
throw Object.assign(
new Error(
`LL4005 no golden fixtures found under ${root} ` +
`(expected .json files in ${join(root, "loops")} and ${join(root, "productive")})`,
),
{ code: "LL4005" },
);
}
const detectMs: number[] = [];
const replays: GoldenReplay[] = [];

Expand Down
14 changes: 9 additions & 5 deletions packages/cli/src/commands/bench.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import type { Command } from "commander";
import { goldenPassed, runGoldenBench } from "../../../../bench/golden.ts";
import { CI_FAIL_MS, detectLoadPassed, runDetectLoad } from "../../../../bench/detect_load.ts";
import { exitWith, formatUnknown } from "../exit.ts";

export function addBenchCommand(program: Command): void {
const bench = program.command("bench").description("Golden corpus and detect load bench");

bench
.command("golden")
.description("Replay fixtures/golden")
.option("--dir <dir>", "fixtures/golden", "fixtures/golden")
.option("--dir <dir>", "golden fixture dir (default: <package>/fixtures/golden)")
.action(async (opts: { dir?: string }) => {
const dir = opts.dir ?? "fixtures/golden";
const { result } = await runGoldenBench(dir);
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
if (!goldenPassed(result)) process.exit(3);
try {
const { result } = await runGoldenBench(opts.dir);
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
if (!goldenPassed(result)) process.exit(3);
} catch (err) {
exitWith(2, formatUnknown(err));
}
});

bench
Expand Down
Loading