repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/the-system/__bench__/render.bench.ts
Rawimport { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import type {
BoardItemState,
BoardProposal,
BoardState,
} from "../board-state.ts";
import { loadProjectIndex } from "../project-index.ts";
import {
type RenderInput,
renderSystemBoardSite,
renderSystemBookSite,
renderSystemViewSite,
} from "../system-renderer.ts";
const ROUNDS = Number(process.env.SYSTEM_BENCH_ROUNDS ?? 10);
const MINIMUM_ROUND_NS = Number(
process.env.SYSTEM_BENCH_MINIMUM_ROUND_NS ?? 200_000_000,
);
const SCALE = Number(process.env.SYSTEM_BENCH_SCALE ?? 1);
let sink = 0;
const PREFIX = "PX";
const token = (index: number) => `T${`${index}`.padStart(7, "0")}`;
const documentId = (type: string, index: number) =>
`${PREFIX}-${type.toUpperCase()}-${token(index)}`;
const bundlePath = (directory: string, id: string) =>
`.system/${directory}/${id}-${id.slice(-8).toLowerCase()}/index.md`;
const linkTo = (directory: string, id: string) =>
`../../${directory}/${id.slice(0, -8)}${id.slice(-8).toLowerCase()}/index.md`;
const paragraph = (seed: number) =>
`Renders the governed corpus through remark and rehype. The pipeline must survive repeated board mutations without unbounded growth. Round ${seed} covers heading anchors, slug stability, and reference rewrites across every bundle in the project tree with enough prose to exercise the parser. The board rebuilds the whole site whenever a stored signature changes.`;
const body = (seed: number, links: readonly string[]) => `## Overview
${paragraph(seed)}
### Data
| Layer | Cost | Notes |
| ----- | ---- | ----- |
| parse | ${seed} ms | remark tokenizers |
| render | ${seed * 2} ms | rehype plugins |
Inline math $E = mc^${(seed % 9) + 1}$ and display math:
$$\\int_0^1 x^${(seed % 7) + 1} \\, dx = \\frac{1}{${(seed % 7) + 2}}$$
> [!WARNING]
> Renderer output is regenerated on every board mutation.
\`\`\`ts
export const round = (seed: number): number => seed * ${(seed % 13) + 1};
\`\`\`
\`\`\`mermaid
graph LR
A[Parse] --> B[Render]
B --> C[Write]
\`\`\`
### References
${links.map((link) => `- See [${link.slice(0, -9)}](${link})`).join("\n")}
First term
: Governs the pipeline.
[External](https://example.invalid/report)
`;
const frontmatter = (id: string, fields: Record<string, string>) => {
const type = id.split("-")[1]?.toLowerCase();
return `---\n${Object.entries({ id, type, ...fields })
.map(([key, value]) => `${key}: ${value}`)
.join("\n")}\n---\n`;
};
const writeDocument = (
root: string,
directory: string,
id: string,
fields: Record<string, string>,
links: readonly string[],
seed: number,
) => {
const file = path.join(root, bundlePath(directory, id));
mkdirSync(path.dirname(file), { recursive: true });
writeFileSync(file, `${frontmatter(id, fields)}${body(seed, links)}`, "utf8");
};
const createProject = (root: string): void => {
mkdirSync(path.join(root, ".system"), { recursive: true });
writeFileSync(
path.join(root, ".system/config.json"),
JSON.stringify({
prefix: PREFIX,
schemaVersion: 0,
title: "Render Bench",
currentPhase: documentId("phase", 1),
}),
"utf8",
);
writeFileSync(
path.join(root, ".system/board.json"),
JSON.stringify({ schemaVersion: 0, items: {} }),
"utf8",
);
for (const name of ["SYSTEM", "MISSION", "RULES"])
writeFileSync(
path.join(root, `.system/${name}.md`),
`# ${name}\n\n${paragraph(name.length)}\n\n- ${name} law one\n- ${name} law two\n`,
"utf8",
);
for (let index = 1; index <= 2 * SCALE; index++) {
const id = documentId("phase", index);
writeDocument(
root,
"phases",
id,
{ title: `Phase ${index}`, label: `P${index}` },
[],
index,
);
}
for (let index = 1; index <= 8 * SCALE; index++) {
const id = documentId("research", index);
writeDocument(
root,
"research",
id,
{ title: `Research ${index}` },
[],
index,
);
}
for (let index = 1; index <= 12 * SCALE; index++) {
const id = documentId("spec", index);
writeDocument(
root,
"specs",
id,
{
title: `Spec ${index}`,
research: `[${documentId("research", ((index - 1) % (8 * SCALE)) + 1)}]`,
},
[
linkTo(
"research",
documentId("research", ((index - 1) % (8 * SCALE)) + 1),
),
],
index,
);
}
for (let index = 1; index <= 16 * SCALE; index++) {
const id = documentId("plan", index);
const dependency = index > 1 ? documentId("plan", index - 1) : undefined;
writeDocument(
root,
"plans",
id,
{
title: `Plan ${index}`,
spec: documentId("spec", ((index - 1) % (12 * SCALE)) + 1),
status: index % 2 === 0 ? "approved" : "draft",
...(dependency ? { depends_on: `[${dependency}]` } : {}),
},
[linkTo("specs", documentId("spec", ((index - 1) % (12 * SCALE)) + 1))],
index,
);
}
for (let index = 1; index <= 16 * SCALE; index++) {
const id = documentId("issue", index);
writeDocument(
root,
"issues",
id,
{
title: `Issue ${index}`,
specs: `[${documentId("spec", ((index - 1) % (12 * SCALE)) + 1)}]`,
},
[],
index,
);
}
};
const root = mkdtempSync(path.join(tmpdir(), "system-bench-"));
createProject(root);
const directories: string[] = [];
const index = loadProjectIndex(root);
const board: BoardState = {
schemaVersion: 0,
items: Object.fromEntries(
index.documents
.filter((document) => document.metadata.type === "issue")
.slice(0, 6)
.map((document) => [
document.metadata.id,
{
state: "open",
updatedAt: "2025-01-01T00:00:00.000Z",
source: "manual",
} satisfies {
state: BoardItemState;
updatedAt: string;
source: "manual";
},
]),
),
};
const proposals: readonly BoardProposal[] = index.documents
.filter((document) => document.metadata.type === "plan")
.slice(0, 4)
.map((document) => ({
id: document.metadata.id,
state: "done" as const,
rationale: "Benchmark proposal",
}));
const input: RenderInput = index;
const selected = index.documents[0] as RenderInput["documents"][number];
function benchmark(name: string, operation: () => void): void {
for (let index = 0; index < 2; 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`,
);
for (const directory of directories.splice(0))
rmSync(directory, { recursive: true, force: true });
}
}
if (!Number.isInteger(ROUNDS) || ROUNDS < 1)
throw new Error("SYSTEM_BENCH_ROUNDS must be a positive integer");
if (!Number.isFinite(MINIMUM_ROUND_NS) || MINIMUM_ROUND_NS < 1)
throw new Error("SYSTEM_BENCH_MINIMUM_ROUND_NS must be positive");
console.log(`goos: ${process.platform}`);
console.log(`goarch: ${process.arch}`);
console.log("pkg: pi-ext/the-system");
benchmark("LoadProjectIndex", () => {
sink ^= loadProjectIndex(root).documents.length;
});
benchmark("RenderViewSite", () => {
directories.push(renderSystemViewSite(input, selected).directory);
});
benchmark("RenderBookSite", () => {
directories.push(renderSystemBookSite(input).directory);
});
benchmark("RenderBoardSite", () => {
directories.push(
renderSystemBoardSite(input, board, proposals, "bench").directory,
);
});
let coldRevision = 0;
benchmark("RenderBoardSiteCold", () => {
coldRevision += 1;
const marker = `\n<!-- rev ${coldRevision} -->`;
directories.push(
renderSystemBoardSite(
{
...input,
documents: input.documents.map((document) => ({
...document,
markdown: document.markdown + marker,
})),
},
board,
proposals,
"bench",
).directory,
);
});
rmSync(root, { recursive: true, force: true });
if (sink === 42) console.error(sink);