-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathupdate-benchmark-report.js
More file actions
199 lines (167 loc) · 8.27 KB
/
Copy pathupdate-benchmark-report.js
File metadata and controls
199 lines (167 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env node
/**
* Update benchmark report — reads benchmark JSON and updates:
* 1. generated/BUILD-BENCHMARKS.md (historical table + raw JSON in HTML comment)
* 2. README.md (performance section with latest numbers)
*
* Usage:
* node scripts/update-benchmark-report.js benchmark-result.json
* node scripts/benchmark.js | node scripts/update-benchmark-report.js
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, '..');
// ── Read benchmark JSON from file arg or stdin ───────────────────────────
let jsonText;
const arg = process.argv[2];
if (arg) {
jsonText = fs.readFileSync(path.resolve(arg), 'utf8');
} else {
jsonText = fs.readFileSync('/dev/stdin', 'utf8');
}
const entry = JSON.parse(jsonText);
// ── Paths ────────────────────────────────────────────────────────────────
const benchmarkPath = path.join(root, 'generated', 'BUILD-BENCHMARKS.md');
const readmePath = path.join(root, 'README.md');
// ── Load existing history from BUILD-BENCHMARKS.md ─────────────────────────────
let history = [];
if (fs.existsSync(benchmarkPath)) {
const content = fs.readFileSync(benchmarkPath, 'utf8');
const match = content.match(/<!--\s*BENCHMARK_DATA\s*([\s\S]*?)\s*-->/);
if (match) {
try {
history = JSON.parse(match[1]);
} catch {
/* start fresh if corrupt */
}
}
}
// Add new entry (deduplicate by version — replace if same version exists)
const idx = history.findIndex((h) => h.version === entry.version);
if (idx >= 0) {
history[idx] = entry;
} else {
history.unshift(entry);
}
// ── Helpers ──────────────────────────────────────────────────────────────
function trend(current, previous, lowerIsBetter = true) {
if (previous == null) return '';
const pct = ((current - previous) / previous) * 100;
if (Math.abs(pct) < 2) return ' ~';
if (lowerIsBetter) {
return pct < 0 ? ` ↓${Math.abs(Math.round(pct))}%` : ` ↑${Math.round(pct)}%`;
}
return pct > 0 ? ` ↑${Math.round(pct)}%` : ` ↓${Math.abs(Math.round(pct))}%`;
}
function formatMs(ms) {
if (ms >= 1000) return `${(ms / 1000).toFixed(1)}s`;
return `${Math.round(ms)}ms`;
}
function formatBytes(bytes) {
if (bytes >= 1048576) return `${(bytes / 1048576).toFixed(1)} MB`;
if (bytes >= 1024) return `${(bytes / 1024).toFixed(0)} KB`;
return `${bytes} B`;
}
function engineRow(h, prev, engineKey) {
const e = h[engineKey];
const p = prev?.[engineKey] || null;
if (!e) return null;
const buildTrend = trend(e.perFile.buildTimeMs, p?.perFile?.buildTimeMs);
const queryTrend = trend(e.queryTimeMs, p?.queryTimeMs);
const nodeTrend = trend(e.perFile.nodes, p?.perFile?.nodes, false);
const edgeTrend = trend(e.perFile.edges, p?.perFile?.edges, false);
const dbTrend = trend(e.perFile.dbSizeBytes, p?.perFile?.dbSizeBytes);
return (
`| ${h.version} | ${engineKey} | ${h.date} | ${h.files} ` +
`| ${e.perFile.buildTimeMs}${buildTrend} ` +
`| ${e.queryTimeMs}${queryTrend} ` +
`| ${e.perFile.nodes}${nodeTrend} ` +
`| ${e.perFile.edges}${edgeTrend} ` +
`| ${e.perFile.dbSizeBytes}${dbTrend} |`
);
}
// ── Build BUILD-BENCHMARKS.md ──────────────────────────────────────────────────
let md = '# Codegraph Performance Benchmarks\n\n';
md += 'Self-measured on every release by running codegraph on its own codebase.\n';
md += 'Metrics are normalized per file for cross-version comparability.\n\n';
md +=
'| Version | Engine | Date | Files | Build (ms/file) | Query (ms) | Nodes/file | Edges/file | DB (bytes/file) |\n';
md +=
'|---------|--------|------|------:|----------------:|-----------:|-----------:|-----------:|----------------:|\n';
for (let i = 0; i < history.length; i++) {
const h = history[i];
const prev = history[i + 1] || null;
const nativeRow = engineRow(h, prev, 'native');
const wasmRow = engineRow(h, prev, 'wasm');
if (nativeRow) md += nativeRow + '\n';
if (wasmRow) md += wasmRow + '\n';
}
md += '\n### Raw totals (latest)\n\n';
const latest = history[0];
for (const engineKey of ['native', 'wasm']) {
const e = latest[engineKey];
if (!e) continue;
md += `#### ${engineKey === 'native' ? 'Native (Rust)' : 'WASM'}\n\n`;
md += `| Metric | Value |\n`;
md += `|--------|-------|\n`;
md += `| Build time | ${formatMs(e.buildTimeMs)} |\n`;
md += `| Query time | ${formatMs(e.queryTimeMs)} |\n`;
md += `| Nodes | ${e.nodes.toLocaleString()} |\n`;
md += `| Edges | ${e.edges.toLocaleString()} |\n`;
md += `| DB size | ${formatBytes(e.dbSizeBytes)} |\n`;
md += `| Files | ${latest.files} |\n\n`;
}
// ── Extrapolated estimate for large repos ────────────────────────────────
const ESTIMATE_FILES = 50_000;
md += `### Estimated performance at ${(ESTIMATE_FILES).toLocaleString()} files\n\n`;
md += 'Extrapolated linearly from per-file metrics above.\n\n';
md += '| Metric | Native (Rust) | WASM |\n';
md += '|--------|---:|---:|\n';
const estNative = latest.native?.perFile;
const estWasm = latest.wasm.perFile;
md += `| Build time | ${estNative ? formatMs(estNative.buildTimeMs * ESTIMATE_FILES) : 'n/a'} | ${formatMs(estWasm.buildTimeMs * ESTIMATE_FILES)} |\n`;
md += `| DB size | ${estNative ? formatBytes(estNative.dbSizeBytes * ESTIMATE_FILES) : 'n/a'} | ${formatBytes(estWasm.dbSizeBytes * ESTIMATE_FILES)} |\n`;
md += `| Nodes | ${estNative ? Math.round(estNative.nodes * ESTIMATE_FILES).toLocaleString() : 'n/a'} | ${Math.round(estWasm.nodes * ESTIMATE_FILES).toLocaleString()} |\n`;
md += `| Edges | ${estNative ? Math.round(estNative.edges * ESTIMATE_FILES).toLocaleString() : 'n/a'} | ${Math.round(estWasm.edges * ESTIMATE_FILES).toLocaleString()} |\n\n`;
md += `<!-- BENCHMARK_DATA\n${JSON.stringify(history, null, 2)}\n-->\n`;
fs.mkdirSync(path.dirname(benchmarkPath), { recursive: true });
fs.writeFileSync(benchmarkPath, md);
console.error(`Updated ${path.relative(root, benchmarkPath)}`);
// ── Patch README.md ──────────────────────────────────────────────────────
if (fs.existsSync(readmePath)) {
let readme = fs.readFileSync(readmePath, 'utf8');
// Build the table rows — show both engines when native is available
let rows = '';
if (latest.native) {
rows += `| Build speed (native) | **${latest.native.perFile.buildTimeMs} ms/file** |\n`;
rows += `| Build speed (WASM) | **${latest.wasm.perFile.buildTimeMs} ms/file** |\n`;
rows += `| Query time | **${formatMs(latest.native.queryTimeMs)}** |\n`;
} else {
rows += `| Build speed | **${latest.wasm.perFile.buildTimeMs} ms/file** |\n`;
rows += `| Query time | **${formatMs(latest.wasm.queryTimeMs)}** |\n`;
}
// 50k-file estimate
const estBuild = latest.native
? formatMs(latest.native.perFile.buildTimeMs * ESTIMATE_FILES)
: formatMs(latest.wasm.perFile.buildTimeMs * ESTIMATE_FILES);
rows += `| ~${(ESTIMATE_FILES).toLocaleString()} files (est.) | **~${estBuild} build** |\n`;
const perfSection = `## 📊 Performance
Self-measured on every release via CI ([build benchmarks](generated/BUILD-BENCHMARKS.md) | [embedding benchmarks](generated/EMBEDDING-BENCHMARKS.md)):
| Metric | Latest |
|---|---|
${rows}
Metrics are normalized per file for cross-version comparability. Times above are for a full initial build — incremental rebuilds only re-parse changed files.
`;
// Match the performance section from header to next ## header or end
// Use \r?\n to handle both Unix and Windows line endings
const perfRegex = /## 📊 Performance\r?\n[\s\S]*?(?=\r?\n## |$)/;
if (perfRegex.test(readme)) {
readme = readme.replace(perfRegex, perfSection);
} else {
console.error('Warning: could not find performance section in README.md');
}
fs.writeFileSync(readmePath, readme);
console.error(`Updated ${path.relative(root, readmePath)}`);
}