-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.js
More file actions
executable file
·348 lines (299 loc) · 11.1 KB
/
benchmark.js
File metadata and controls
executable file
·348 lines (299 loc) · 11.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env node
/**
* Performance Benchmark for Nex Code Optimizations
*
* Tests the key optimized areas:
* 1. System Prompt Build-Time (cached vs uncached)
* 2. Token Estimation (cached vs uncached)
* 3. Context Gathering (cached vs uncached)
* 4. Tool Validation (cached vs uncached)
* 5. Tool Filtering (cached vs uncached)
*/
const { performance } = require("perf_hooks");
// Color helpers
const C = {
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
gray: "\x1b[90m",
reset: "\x1b[0m",
bold: "\x1b[1m",
};
function log(msg) {
console.log(`${C.gray}${msg}${C.reset}`);
}
function benchmark(name, fn, iterations = 100) {
// Warmup
for (let i = 0; i < 10; i++) fn();
// Measure
const start = performance.now();
for (let i = 0; i < iterations; i++) fn();
const end = performance.now();
const avg = (end - start) / iterations;
const total = end - start;
return { avg, total, iterations };
}
function formatMs(ms) {
if (ms < 1) return `${(ms * 1000).toFixed(2)}µs`;
if (ms < 100) return `${ms.toFixed(2)}ms`;
return `${ms.toFixed(1)}ms`;
}
async function runBenchmarks() {
console.log(
`\n${C.bold}╔════════════════════════════════════════════════════╗${C.reset}`,
);
console.log(
`${C.bold}║ Nex Code Performance Benchmark ║${C.reset}`,
);
console.log(
`${C.bold}╚════════════════════════════════════════════════════╝${C.reset}\n`,
);
const results = [];
// ─────────────────────────────────────────────────────────
// 1. System Prompt Build-Time
// ─────────────────────────────────────────────────────────
console.log(`${C.blue}1. System Prompt Build-Time${C.reset}`);
log(" Testing: buildSystemPrompt() with caching\n");
const {
buildSystemPrompt,
invalidateSystemPromptCache,
} = require("./cli/agent");
// First call (cache miss)
const firstCall = benchmark(
"First Call (cold)",
async () => {
invalidateSystemPromptCache();
await buildSystemPrompt();
},
10,
);
// Subsequent calls (cache hit)
const cachedCall = benchmark(
"Cached (hot)",
async () => {
await buildSystemPrompt();
},
100,
);
console.log(
` ${C.yellow}Cold:${C.reset} ${formatMs(firstCall.avg)} (avg of ${firstCall.iterations})`,
);
console.log(
` ${C.green}Cached:${C.reset} ${formatMs(cachedCall.avg)} (avg of ${cachedCall.iterations})`,
);
const speedup1 = (firstCall.avg / cachedCall.avg).toFixed(1);
console.log(` ${C.bold}Speedup: ${speedup1}×${C.reset}\n`);
results.push({
name: "System Prompt",
cold: firstCall.avg,
cached: cachedCall.avg,
speedup: speedup1,
});
// ─────────────────────────────────────────────────────────
// 2. Token Estimation
// ─────────────────────────────────────────────────────────
console.log(`${C.blue}2. Token Estimation${C.reset}`);
log(" Testing: estimateTokens() with string caching\n");
const { estimateTokens } = require("./cli/context-engine");
const testString = "This is a test string for token estimation. ".repeat(100);
// First call (cache miss)
const tokenFirst = benchmark(
"First Call",
() => {
estimateTokens(testString);
},
1000,
);
// Cached call
const tokenCached = benchmark(
"Cached",
() => {
estimateTokens(testString);
},
10000,
);
console.log(
` ${C.yellow}First:${C.reset} ${formatMs(tokenFirst.avg)} (avg of ${tokenFirst.iterations})`,
);
console.log(
` ${C.green}Cached:${C.reset} ${formatMs(tokenCached.avg)} (avg of ${tokenCached.iterations})`,
);
const speedup2 = (tokenFirst.avg / tokenCached.avg).toFixed(1);
console.log(` ${C.bold}Speedup: ${speedup2}×${C.reset}\n`);
results.push({
name: "Token Estimation",
cold: tokenFirst.avg,
cached: tokenCached.avg,
speedup: speedup2,
});
// ─────────────────────────────────────────────────────────
// 3. Context Gathering
// ─────────────────────────────────────────────────────────
console.log(`${C.blue}3. Context Gathering${C.reset}`);
log(" Testing: gatherProjectContext() with file caching\n");
const { gatherProjectContext, _clearContextCache } = require("./cli/context");
// First call (cache miss)
const contextFirst = benchmark(
"First Call",
async () => {
_clearContextCache();
await gatherProjectContext(process.cwd());
},
10,
);
// Cached call
const contextCached = benchmark(
"Cached",
async () => {
await gatherProjectContext(process.cwd());
},
100,
);
console.log(
` ${C.yellow}Cold:${C.reset} ${formatMs(contextFirst.avg)} (avg of ${contextFirst.iterations})`,
);
console.log(
` ${C.green}Cached:${C.reset} ${formatMs(contextCached.avg)} (avg of ${contextCached.iterations})`,
);
const speedup3 = (contextFirst.avg / contextCached.avg).toFixed(1);
console.log(` ${C.bold}Speedup: ${speedup3}×${C.reset}\n`);
results.push({
name: "Context Gathering",
cold: contextFirst.avg,
cached: contextCached.avg,
speedup: speedup3,
});
// ─────────────────────────────────────────────────────────
// 4. Tool Validation
// ─────────────────────────────────────────────────────────
console.log(`${C.blue}4. Tool Validation${C.reset}`);
log(" Testing: validateToolArgs() with schema caching\n");
const {
validateToolArgs,
clearSchemaCache,
} = require("./cli/tool-validator");
const testArgs = { path: "test.txt", content: "Hello World" };
// First call (cache miss)
const validationFirst = benchmark(
"First Call",
() => {
clearSchemaCache();
validateToolArgs("write_file", testArgs);
},
100,
);
// Cached call
const validationCached = benchmark(
"Cached",
() => {
validateToolArgs("write_file", testArgs);
},
1000,
);
console.log(
` ${C.yellow}Cold:${C.reset} ${formatMs(validationFirst.avg)} (avg of ${validationFirst.iterations})`,
);
console.log(
` ${C.green}Cached:${C.reset} ${formatMs(validationCached.avg)} (avg of ${validationCached.iterations})`,
);
const speedup4 = (validationFirst.avg / validationCached.avg).toFixed(1);
console.log(` ${C.bold}Speedup: ${speedup4}×${C.reset}\n`);
results.push({
name: "Tool Validation",
cold: validationFirst.avg,
cached: validationCached.avg,
speedup: speedup4,
});
// ─────────────────────────────────────────────────────────
// 5. Tool Filtering
// ─────────────────────────────────────────────────────────
console.log(`${C.blue}5. Tool Filtering${C.reset}`);
log(" Testing: getCachedFilteredTools() with model caching\n");
const {
getCachedFilteredTools,
clearToolFilterCache,
} = require("./cli/agent");
const { TOOL_DEFINITIONS } = require("./cli/tools");
const { getSkillToolDefinitions } = require("./cli/skills");
const { getMCPToolDefinitions } = require("./cli/mcp");
const allTools = [
...TOOL_DEFINITIONS,
...getSkillToolDefinitions(),
...getMCPToolDefinitions(),
];
// First call (cache miss)
const filterFirst = benchmark(
"First Call",
() => {
clearToolFilterCache();
getCachedFilteredTools(allTools);
},
100,
);
// Cached call
const filterCached = benchmark(
"Cached",
() => {
getCachedFilteredTools(allTools);
},
1000,
);
console.log(
` ${C.yellow}Cold:${C.reset} ${formatMs(filterFirst.avg)} (avg of ${filterFirst.iterations})`,
);
console.log(
` ${C.green}Cached:${C.reset} ${formatMs(filterCached.avg)} (avg of ${filterCached.iterations})`,
);
const speedup5 = (filterFirst.avg / filterCached.avg).toFixed(1);
console.log(` ${C.bold}Speedup: ${speedup5}×${C.reset}\n`);
results.push({
name: "Tool Filtering",
cold: filterFirst.avg,
cached: filterCached.avg,
speedup: speedup5,
});
// ─────────────────────────────────────────────────────────
// Summary
// ─────────────────────────────────────────────────────────
console.log(
`${C.bold}╔════════════════════════════════════════════════════╗${C.reset}`,
);
console.log(
`${C.bold}║ Summary ║${C.reset}`,
);
console.log(
`${C.bold}╚════════════════════════════════════════════════════╝${C.reset}\n`,
);
console.log(
` ${C.bold}Optimization Cold Cached Speedup${C.reset}`,
);
console.log(
` ${C.gray}─────────────────────────────────────────────────${C.reset}`,
);
for (const r of results) {
const namePad = r.name.padEnd(20);
const coldPad = formatMs(r.cold).padStart(10);
const cachedPad = formatMs(r.cached).padStart(10);
const speedupPad = `${r.speedup}×`.padStart(7);
console.log(
` ${namePad} ${coldPad} ${cachedPad} ${C.green}${speedupPad}${C.reset}`,
);
}
const avgSpeedup = (
results.reduce((sum, r) => sum + parseFloat(r.speedup), 0) / results.length
).toFixed(1);
console.log(
`\n ${C.bold}Average Speedup: ${C.green}${avgSpeedup}×${C.reset}\n`,
);
console.log(
`${C.gray} Note: Actual performance gains depend on project size,${C.reset}`,
);
console.log(
`${C.gray} conversation length, and tool usage patterns.${C.reset}\n`,
);
}
// Run benchmarks
runBenchmarks().catch((err) => {
console.error(`${C.red}Benchmark failed:${C.reset}`, err);
process.exit(1);
});