-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathupdate-embedding-report.js
More file actions
184 lines (157 loc) · 7.31 KB
/
Copy pathupdate-embedding-report.js
File metadata and controls
184 lines (157 loc) · 7.31 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
#!/usr/bin/env node
/**
* Update embedding benchmark report — reads benchmark JSON and updates:
* generated/benchmarks/EMBEDDING-BENCHMARKS.md (historical table + raw JSON in HTML comment)
*
* Usage:
* node scripts/update-embedding-report.js embedding-benchmark-result.json
* node scripts/embedding-benchmark.js | node scripts/update-embedding-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);
// Guard: reject empty benchmark results (all workers crashed or no symbols indexed)
if (!entry.symbols || !entry.models || Object.keys(entry.models).length === 0) {
console.error(
`Embedding benchmark produced empty results (symbols=${entry.symbols}, models=${Object.keys(entry.models || {}).length}). ` +
'Skipping report update to avoid overwriting valid data. Check benchmark worker logs.',
);
process.exit(1);
}
// ── Paths ────────────────────────────────────────────────────────────────
const reportPath = path.join(root, 'generated', 'benchmarks', 'EMBEDDING-BENCHMARKS.md');
// ── Load existing history ────────────────────────────────────────────────
let history = [];
if (fs.existsSync(reportPath)) {
const content = fs.readFileSync(reportPath, 'utf8');
const match = content.match(/<!--\s*EMBEDDING_BENCHMARK_DATA\s*([\s\S]*?)\s*-->/);
if (match) {
try {
history = JSON.parse(match[1]);
} catch {
/* start fresh if corrupt */
}
}
}
// Add new entry — dev entries are rolling, releases replace dev
const isDev = entry.version === 'dev';
const idx = history.findIndex((h) => h.version === entry.version);
if (idx >= 0) history.splice(idx, 1);
if (!isDev) {
const devIdx = history.findIndex((h) => h.version === 'dev');
if (devIdx >= 0) history.splice(devIdx, 1);
}
history.unshift(entry);
function findPrevRelease(hist, fromIdx) {
for (let i = fromIdx + 1; i < hist.length; i++) {
if (hist[i].version !== 'dev') return hist[i];
}
return null;
}
// ── Helpers ──────────────────────────────────────────────────────────────
function pct(n, total) {
return `${((n / total) * 100).toFixed(1)}%`;
}
function trend(current, previous) {
if (previous == null) return '';
const diff = current - previous;
if (Math.abs(diff) < 0.5) return ' ~';
return diff > 0 ? ` ↑${diff.toFixed(1)}pp` : ` ↓${Math.abs(diff).toFixed(1)}pp`;
}
function pctVal(n, total) {
return (n / total) * 100;
}
function formatMs(ms) {
if (ms >= 1000) return `${(ms / 1000).toFixed(1)}s`;
return `${Math.round(ms)}ms`;
}
// ── Build EMBEDDING-BENCHMARKS.md ────────────────────────────────────────
let md = '# Codegraph Embedding Benchmarks\n\n';
md += 'Self-measured on every release using auto-generated queries from symbol names.\n';
md += 'Each symbol\'s name is split into words (e.g. `buildGraph` → `"build graph"`) and used as the search query.\n';
md += 'Hit@N = expected symbol found in top N results.\n\n';
md +=
'| Version | Model | Symbols | Hit@1 | Hit@3 | Hit@5 | Misses | Embed Time |\n';
md +=
'|---------|-------|--------:|------:|------:|------:|-------:|-----------:|\n';
for (let i = 0; i < history.length; i++) {
const h = history[i];
const prev = findPrevRelease(history, i);
for (const [modelKey, m] of Object.entries(h.models)) {
const pm = prev?.models?.[modelKey] || null;
const h1 = pctVal(m.hits1, m.total);
const h3 = pctVal(m.hits3, m.total);
const h5 = pctVal(m.hits5, m.total);
const ph1 = pm ? pctVal(pm.hits1, pm.total) : null;
const ph3 = pm ? pctVal(pm.hits3, pm.total) : null;
const ph5 = pm ? pctVal(pm.hits5, pm.total) : null;
md += `| ${h.version} | ${modelKey} | ${m.total} `;
md += `| ${pct(m.hits1, m.total)}${trend(h1, ph1)} `;
md += `| ${pct(m.hits3, m.total)}${trend(h3, ph3)} `;
md += `| ${pct(m.hits5, m.total)}${trend(h5, ph5)} `;
md += `| ${m.misses} `;
md += `| ${formatMs(m.embedTimeMs)} |\n`;
}
}
// ── Latest summary ───────────────────────────────────────────────────────
const latest = history[0];
md += '\n### Latest results\n\n';
md += `**Version:** ${latest.version} | **Strategy:** ${latest.strategy} | **Symbols:** ${latest.symbols} | **Date:** ${latest.date}\n\n`;
md += '| Model | Dim | Context | Hit@1 | Hit@3 | Hit@5 | Hit@10 | Misses | Embed | Search |\n';
md += '|-------|----:|--------:|------:|------:|------:|-------:|-------:|------:|-------:|\n';
for (const [modelKey, m] of Object.entries(latest.models)) {
md += `| ${modelKey} `;
md += `| ${m.dim} `;
md += `| ${m.contextWindow} `;
md += `| ${pct(m.hits1, m.total)} `;
md += `| ${pct(m.hits3, m.total)} `;
md += `| ${pct(m.hits5, m.total)} `;
md += `| ${pct(m.hits10, m.total)} `;
md += `| ${m.misses} `;
md += `| ${formatMs(m.embedTimeMs)} `;
md += `| ${formatMs(m.searchTimeMs)} |\n`;
}
md += `\n<!-- EMBEDDING_BENCHMARK_DATA\n${JSON.stringify(history, null, 2)}\n-->\n`;
fs.mkdirSync(path.dirname(reportPath), { recursive: true });
fs.writeFileSync(reportPath, md);
console.error(`Updated ${path.relative(root, reportPath)}`);
// ── Regression detection ─────────────────────────────────────────────────
const REGRESSION_THRESHOLD = 0.15; // 15% regression triggers a warning
const prev = findPrevRelease(history, 0);
function checkRegression(label, current, previous, lowerIsBetter = true) {
if (previous == null || previous === 0) return;
const pct = (current - previous) / previous;
const regressed = lowerIsBetter ? pct > REGRESSION_THRESHOLD : pct < -REGRESSION_THRESHOLD;
if (regressed) {
const delta = lowerIsBetter ? `+${Math.round(pct * 100)}%` : `${Math.round(pct * 100)}%`;
const msg = `${label}: ${previous} → ${current} (${delta}, threshold ${Math.round(REGRESSION_THRESHOLD * 100)}%)`;
if (process.env.GITHUB_ACTIONS) {
console.error(`::warning title=Benchmark Regression::${msg}`);
} else {
console.error(`⚠ REGRESSION: ${msg}`);
}
}
}
if (prev) {
for (const [modelKey, m] of Object.entries(latest.models)) {
const pm = prev.models?.[modelKey];
if (!pm) continue;
const tag = `[${modelKey}]`;
// Hit rates: higher is better (regression = drop)
checkRegression(`${tag} Hit@1`, m.hits1 / m.total, pm.hits1 / pm.total, false);
checkRegression(`${tag} Hit@5`, m.hits5 / m.total, pm.hits5 / pm.total, false);
// Embed time: lower is better (regression = increase)
checkRegression(`${tag} Embed time`, m.embedTimeMs, pm.embedTimeMs);
}
}