repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/prompt-autopsy/__bench__/bench.ts
Rawimport { cpus } from "node:os";
import { groupSystemPrompt } from "../index.ts";
const append = `# Response style
- Abbreviate: DB/auth/config/req/res/fn/impl
- Strip conjunctions, articles
- Arrows: X → Y → Z
- Fragments OK
- No filler, hedging, pleasantries`;
const prompt = `You are a coding agent. Use tools.
<tools>
- read: Read files
- bash: Execute shell commands
- edit: Precise replacement
- write: Write files
</tools>
<rules>
${"- Use concise output.\n".repeat(120).trimEnd()}
</rules>
<addendum>
${append}
</addendum>
<project_context>
Project-specific instructions and guidelines:
<project_instructions path="/repo/AGENTS.md">
${"Use exact edits. Prefer rg/fd.\n".repeat(160).trimEnd()}
</project_instructions>
</project_context>
<skills>
The following skills provide specialized instructions for specific tasks.
Use the read tool to load a skill's file when the task matches its description.
When a skill file references a relative path, resolve it against the skill directory (parent of SKILL.md / dirname of the path) and use that absolute path in tool commands.
<available_skills>
${Array.from(
{ length: 24 },
(_, i) => ` <skill>
<name>skill-${i}</name>
<description>${"desc ".repeat(24)}</description>
<location>/skills/skill-${i}/SKILL.md</location>
</skill>`,
).join("\n")}
</available_skills>
</skills>
<cwd>
/repo
</cwd>`;
function oldGroupSystemPrompt(raw: string): string {
const sections = [
{
label: "[CORE]",
test: (l: string) =>
l.includes("You are a coding agent") || l.includes("use these tools"),
},
{
label: "[TOOLS]",
test: (l: string) =>
l.includes("## Available tools") ||
l.includes("- read:") ||
l.includes("- bash:"),
},
{
label: "[GUIDE]",
test: (l: string) =>
l.includes("## Guidelines") ||
l.includes("- Use the") ||
l.includes("- When"),
},
{
label: "[SKILLS]",
test: (l: string) =>
l.includes("## Available skills") ||
l.includes("Use skill documentation"),
},
{
label: "[CTX]",
test: (l: string) =>
l.includes("## Project Context") || l.includes("# Project Context"),
},
{
label: "[APPEND]",
test: (l: string) =>
l.includes("APPEND_SYSTEM") ||
l.includes("Response Protocol") ||
l.includes("Abbreviate") ||
l.includes("Strip conjunctions"),
},
{
label: "[DATE]",
test: (l: string) =>
l.includes("Current date:") || l.includes("Current working directory:"),
},
];
const lines = raw.split("\n");
let current = "[UNKNOWN]";
const grouped: Record<string, string[]> = {};
for (const line of lines) {
const match = sections.find((s) => s.test(line));
if (match) current = match.label;
else if (line.trim() === "") current = "[BLANK]";
if (!grouped[current]) grouped[current] = [];
grouped[current].push(line);
}
return Object.entries(grouped)
.map(([k, v]) => `${k}\n${v.join("\n")}`)
.join("\n\n");
}
const mode = process.argv[2] === "old" ? "old" : "new";
const threads = cpus().length;
let sink = 0;
function consume(s: string): void {
sink ^=
s.length ^
s.charCodeAt(0) ^
s.charCodeAt(s.length >> 1) ^
s.charCodeAt(s.length - 1);
}
function bench(name: string, fn: () => string, n = 20_000, rounds = 12): void {
for (let i = 0; i < 3_000; i++) consume(fn());
for (let r = 0; r < rounds; r++) {
const start = performance.now();
for (let i = 0; i < n; i++) consume(fn());
const ns = ((performance.now() - start) * 1_000_000) / n;
console.log(`Benchmark${name}-${threads}\t${n}\t${ns.toFixed(0)} ns/op`);
}
}
bench(
"PromptAutopsy",
mode === "old"
? () => oldGroupSystemPrompt(prompt)
: () => groupSystemPrompt(prompt),
);
if (sink === 42) console.log("sink", sink);