repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/llmiterate/__bench__/bench.ts
RawFile outline
import { performance } from "node:perf_hooks";
import { defaultConfig } from "../config";
import { parseMarkerPrompts } from "../parser";
import { compilePathFilter, shouldIncludeCompiledPath } from "../path-filter";
const ITERATIONS = Number(process.env.ITERATIONS ?? 2000);
const MIN_TIME_MS = Number(process.env.MIN_TIME_MS ?? 200);
const sourceNoMarkers = makeSource(600, 0);
const sourceSomeMarkers = makeSource(600, 20);
const markdown = makeMarkdown(900, 30);
const paths = makePaths(5000);
const filter = compilePathFilter({
...defaultConfig(),
include: ["**/*.{ts,tsx,rs,java,md}"],
});
bench("ParseNoMarkers", () =>
parseMarkerPrompts("src/main.ts", sourceNoMarkers),
);
bench("ParseSomeMarkers", () =>
parseMarkerPrompts("src/main.ts", sourceSomeMarkers),
);
bench("ParseMarkdownFences", () =>
parseMarkerPrompts("docs/notes.md", markdown),
);
bench("PathFilterCompiled", () => {
let matched = 0;
for (const p of paths) if (shouldIncludeCompiledPath(p, filter)) matched++;
if (matched === 0) throw new Error("impossible");
return matched;
});
function bench(name: string, fn: () => unknown): void {
for (let i = 0; i < 50; i++) fn();
let iterations = ITERATIONS;
let elapsed = 0;
let result: unknown;
do {
const start = performance.now();
for (let i = 0; i < iterations; i++) result = fn();
elapsed = performance.now() - start;
if (elapsed < MIN_TIME_MS) iterations *= 2;
} while (elapsed < MIN_TIME_MS);
if (result === undefined) throw new Error("bench result vanished");
const nsPerOp = (elapsed * 1_000_000) / iterations;
console.log(`Benchmark${name} ${iterations} ${nsPerOp.toFixed(0)} ns/op`);
}
function makeSource(lines: number, markerEvery: number): string {
const out: string[] = [];
for (let i = 0; i < lines; i++) {
if (markerEvery > 0 && i % markerEvery === 0)
out.push(` // llmiterate: implement generated case ${i}`);
else out.push(` const value${i} = callThing(${i}, "${i}");`);
}
return out.join("\n");
}
function makeMarkdown(lines: number, markerEvery: number): string {
const out: string[] = ["# Notes"];
for (let i = 0; i < lines; i++) {
if (i % 80 === 0) out.push("```ts");
if (i % 80 === 40) out.push("```");
if (markerEvery > 0 && i % markerEvery === 0)
out.push(`<!-- llmiterate: summarize section ${i} -->`);
else out.push(`Regular prose line ${i} with enough text to be realistic.`);
}
return out.join("\n");
}
function makePaths(count: number): string[] {
const exts = ["ts", "tsx", "rs", "java", "md", "png", "lock"];
const dirs = [
"src",
"test",
"node_modules/pkg",
"target/debug",
"research/pages",
"docs",
];
return Array.from(
{ length: count },
(_, i) => `${dirs[i % dirs.length]}/file-${i}.${exts[i % exts.length]}`,
);
}