1
0

parse-bench-readme.mjs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env node
  2. // Aggregate the README A/B (bench-readme.sh output): per repo, median of N runs
  3. // per arm → time, tool calls, tokens, cost, and % saved. Plus an average row.
  4. //
  5. // Tokens = SUM of per-turn assistant `usage` (input + output + cache read +
  6. // cache creation) — the cumulative "total tokens processed". NOTE: `result.usage`
  7. // is last-turn-only in current Claude Code, so it under-counts badly; don't use it.
  8. // `total_cost_usd` and `duration_ms` are already cumulative.
  9. //
  10. // Usage: node parse-bench-readme.mjs [/tmp/ab-readme]
  11. import { readFileSync, existsSync, readdirSync } from 'fs';
  12. import { join } from 'path';
  13. const ROOT = process.argv[2] || '/tmp/ab-readme';
  14. const REPOS = ['vscode', 'excalidraw', 'django', 'tokio', 'okhttp', 'gin', 'alamofire'];
  15. function parse(file) {
  16. if (!existsSync(file)) return null;
  17. const L = readFileSync(file, 'utf8').split('\n').filter(Boolean);
  18. let tools = 0, reads = 0, grep = 0, cg = 0, tokens = 0, r = null;
  19. for (const l of L) { let e; try { e = JSON.parse(l); } catch { continue; }
  20. if (e.type === 'assistant') {
  21. const u = e.message?.usage;
  22. if (u) tokens += (u.input_tokens || 0) + (u.output_tokens || 0) + (u.cache_read_input_tokens || 0) + (u.cache_creation_input_tokens || 0);
  23. for (const b of (e.message?.content || [])) if (b.type === 'tool_use') {
  24. const n = b.name;
  25. if (n === 'ToolSearch') continue;
  26. tools++;
  27. if (n === 'Read') reads++;
  28. else if (n === 'Grep' || n === 'Glob') grep++;
  29. else if (/codegraph/.test(n)) cg++;
  30. }
  31. }
  32. if (e.type === 'result') r = e;
  33. }
  34. if (!r || r.subtype !== 'success') return null;
  35. return { dur: r.duration_ms / 1000, tools, reads, grep, cg, tokens, cost: r.total_cost_usd || 0 };
  36. }
  37. const median = (arr) => { const v = [...arr].sort((a, b) => a - b); const n = v.length; return n === 0 ? 0 : n % 2 ? v[(n - 1) / 2] : (v[n / 2 - 1] + v[n / 2]) / 2; };
  38. const fmtTime = (s) => s >= 60 ? `${Math.floor(s / 60)}m ${Math.round(s % 60)}s` : `${Math.round(s)}s`;
  39. const fmtTok = (t) => t >= 1e6 ? `${(t / 1e6).toFixed(1)}M` : `${Math.round(t / 1000)}k`;
  40. const pct = (w, wo) => wo > 0 ? Math.round((1 - w / wo) * 100) : 0;
  41. console.log('repo n(w/wo) time WITH→WITHOUT tools W→WO tokens W→WO (saved) cost W→WO (saved)');
  42. const savings = { cost: [], tokens: [], time: [], tools: [] };
  43. for (const repo of REPOS) {
  44. const dir = join(ROOT, repo);
  45. const runDirs = existsSync(dir) ? readdirSync(dir).filter(d => /^run\d+$/.test(d)) : [];
  46. const W = [], WO = [];
  47. for (const rd of runDirs) {
  48. const w = parse(join(dir, rd, 'run-headless-with.jsonl')); if (w) W.push(w);
  49. const wo = parse(join(dir, rd, 'run-headless-without.jsonl')); if (wo) WO.push(wo);
  50. }
  51. if (!W.length || !WO.length) { console.log(`${repo.padEnd(11)} (incomplete: w=${W.length} wo=${WO.length})`); continue; }
  52. const m = (arr, k) => median(arr.map(x => x[k]));
  53. const wT = m(W, 'dur'), woT = m(WO, 'dur'), wTok = m(W, 'tokens'), woTok = m(WO, 'tokens');
  54. const wC = m(W, 'cost'), woC = m(WO, 'cost'), wTl = m(W, 'tools'), woTl = m(WO, 'tools');
  55. savings.time.push(pct(wT, woT)); savings.tokens.push(pct(wTok, woTok)); savings.cost.push(pct(wC, woC)); savings.tools.push(pct(wTl, woTl));
  56. console.log(
  57. `${repo.padEnd(11)} ${W.length}/${WO.length} ` +
  58. `${(fmtTime(wT) + '→' + fmtTime(woT)).padEnd(22)}` +
  59. `${(Math.round(wTl) + '→' + Math.round(woTl)).padEnd(12)}` +
  60. `${(fmtTok(wTok) + '→' + fmtTok(woTok) + ' (' + pct(wTok, woTok) + '%)').padEnd(24)}` +
  61. `$${wC.toFixed(2)}→$${woC.toFixed(2)} (${pct(wC, woC)}%)`
  62. );
  63. }
  64. const avg = (a) => a.length ? Math.round(a.reduce((s, x) => s + x, 0) / a.length) : 0;
  65. console.log(`\nAVERAGE saved: cost ${avg(savings.cost)}% · tokens ${avg(savings.tokens)}% · time ${avg(savings.time)}% · tool calls ${avg(savings.tools)}%`);