repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/good-job/__benches__/storage.bench.ts
Rawimport { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { feedbackIdFromSeed } from "../core.ts";
import { ensureLayout, layout, loadRecords } from "../storage.ts";
const RECORD_COUNT = Number(process.env.GOOD_JOB_BENCH_RECORDS ?? 1_000);
const MINIMUM_MILLISECONDS = Number(
process.env.GOOD_JOB_BENCH_MILLISECONDS ?? 750,
);
function record(index: number) {
const id = feedbackIdFromSeed("praise", `bench:${index}`);
return {
version: 2,
id,
createdAt: "2026-01-01T00:00:00.000Z",
completedAt: `2026-01-01T00:${String(index % 60).padStart(2, "0")}:00.000Z`,
kind: "praise",
feedback: "gj",
source: {
type: "session",
cwd: "/project",
sessionPath: "/sessions/example.jsonl",
sessionId: "session-1",
sessionName: null,
leafId: "leaf-1",
agentModel: { provider: "provider", id: "worker" },
},
analysisModel: { provider: "provider", id: "analyzer" },
learning: {
slug: `achievement-${index}`,
summary: `Achievement ${id}`,
evidence: ["Focused tests passed"],
behaviors: ["Inspected callers before editing"],
candidateRule: "Fix shared routes before leaf callers.",
},
usage: {
input: 100,
output: 50,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 150,
cost: 0,
},
};
}
async function main(): Promise<void> {
if (!Number.isInteger(RECORD_COUNT) || RECORD_COUNT < 1)
throw new Error("GOOD_JOB_BENCH_RECORDS must be a positive integer");
const root = await mkdtemp(join(tmpdir(), "good-job-bench-"));
const paths = layout(root);
try {
await ensureLayout(paths);
await Promise.all(
Array.from({ length: RECORD_COUNT }, async (_, index) => {
await writeFile(
join(paths.records, `${index.toString().padStart(8, "0")}.json`),
JSON.stringify(record(index)),
);
}),
);
const control = await loadRecords(paths);
if (control.records.length !== RECORD_COUNT || control.errors.length !== 0)
throw new Error("benchmark fixture failed control load");
let iterations = 1;
let elapsed = 0;
const minimumNanoseconds = MINIMUM_MILLISECONDS * 1_000_000;
do {
const start = process.hrtime.bigint();
for (let index = 0; index < iterations; index++) {
const loaded = await loadRecords(paths);
if (
loaded.records.length !== RECORD_COUNT ||
loaded.errors.length !== 0
)
throw new Error("loadRecords returned an invalid result");
}
elapsed = Number(process.hrtime.bigint() - start);
if (elapsed < minimumNanoseconds) iterations *= 2;
} while (elapsed < minimumNanoseconds);
console.log(
`BenchmarkLoadRecords${RECORD_COUNT}-${process.arch}\t${iterations}\t${(elapsed / iterations).toFixed(0)} ns/op`,
);
} finally {
await rm(root, { recursive: true, force: true });
}
}
await main();