-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-token-report.ts
More file actions
460 lines (408 loc) · 20.5 KB
/
test-token-report.ts
File metadata and controls
460 lines (408 loc) · 20.5 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* MCP Token Reduction — Comprehensive Real-Time Test Report
*
* Tests ALL 12 tools + ALL 7 resources against 7 engineering scenarios.
* Scenarios run in parallel for speed. Baseline is computed once and shared.
*
* Tool coverage:
* Analysis: task_classify, repo_scope_find, flow_summarize, bug_trace_compact
* Planning: implementation_plan, poc_plan
* Execution: impact_analyze, test_select
* Documentation: doc_context_build, doc_update_plan
* Memory: memory_checkpoint, memory_restore
*
* Resource coverage (repo://):
* architecture-map, module-index, symbol-index, test-map,
* doc-map, patterns, glossary
*/
import { readFileSync, readdirSync } from 'fs';
import { join } from 'path';
// ── Capabilities ─────────────────────────────────────────────────────────────
import {
taskClassify,
repoScopeFind,
flowSummarize,
bugTraceCompacts,
} from './src/capabilities/analysis/analysis.js';
import { implementationPlan, pocPlan } from './src/capabilities/planning/planning.js';
import { impactAnalyze, testSelect } from './src/capabilities/execution/execution.js';
import { docContextBuild, docUpdatePlan } from './src/capabilities/documentation/documentation.js';
import { memoryCheckpoint, memoryRestore } from './src/capabilities/memory/memory.js';
// ── Resources ─────────────────────────────────────────────────────────────────
import {
buildArchitectureMap,
buildModuleIndex,
buildSymbolIndex,
buildTestMap,
buildDocMap,
buildPatterns,
buildGlossary,
} from './src/resources/resources.js';
// ── Token utils ───────────────────────────────────────────────────────────────
import { estimateTokens, estimateObjectTokens } from './src/utils/token-counter.js';
import type { FileIndexEntry } from './src/core/types.js';
// ─────────────────────────────────────────────────────────────────────────────
// Constants
// ─────────────────────────────────────────────────────────────────────────────
const REPO_PATH = '/Users/sridhar/IdeaProjects/mcp-token';
// ─────────────────────────────────────────────────────────────────────────────
// Shared helpers
// ─────────────────────────────────────────────────────────────────────────────
function getAllSourceFiles(dir: string): string[] {
const results: string[] = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const full = join(dir, entry.name);
if (entry.isDirectory() && !['node_modules', 'dist', '.git'].includes(entry.name)) {
results.push(...getAllSourceFiles(full));
} else if (entry.isFile() && entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts')) {
results.push(full);
}
}
return results;
}
function readTokens(files: string[]): number {
return files.reduce((sum, f) => {
try { return sum + estimateTokens(readFileSync(f, 'utf8')); }
catch { return sum; }
}, 0);
}
/** Map FileIndexEntry[] → path strings */
const paths = (entries: FileIndexEntry[], n = 5) => entries.slice(0, n).map(f => f.path);
// ─────────────────────────────────────────────────────────────────────────────
// Types
// ─────────────────────────────────────────────────────────────────────────────
interface ToolUsage {
name: string;
tokensIn: number;
tokensOut: number;
}
interface ScenarioResult {
name: string;
task: string;
category: string;
withoutTokens: number;
withoutDesc: string;
withTokens: number;
tools: ToolUsage[];
durationMs: number;
savedTokens: number;
savedPct: number;
error?: string;
}
// ─────────────────────────────────────────────────────────────────────────────
// Baseline (shared, computed once)
// ─────────────────────────────────────────────────────────────────────────────
const allSrcFiles = getAllSourceFiles(join(REPO_PATH, 'src'));
const FULL_REPO_TOKENS = readTokens(allSrcFiles);
const FILE_COUNT = allSrcFiles.length;
// ─────────────────────────────────────────────────────────────────────────────
// Scenario runner
// ─────────────────────────────────────────────────────────────────────────────
async function run<T>(
name: string,
params: unknown,
fn: (p: never) => Promise<T>
): Promise<{ result: T; usage: ToolUsage }> {
const tokensIn = estimateObjectTokens(params);
const result = await fn(params as never);
const tokensOut = estimateObjectTokens(result);
return { result, usage: { name, tokensIn, tokensOut } };
}
async function scenario(
name: string,
task: string,
category: string,
withoutTokens: number,
withoutDesc: string,
body: () => Promise<ToolUsage[]>
): Promise<ScenarioResult> {
const t0 = Date.now();
try {
const tools = await body();
const withTokens = tools.reduce((s, t) => s + t.tokensIn + t.tokensOut, 0);
const saved = withoutTokens - withTokens;
return {
name, task, category,
withoutTokens, withoutDesc,
withTokens, tools,
durationMs: Date.now() - t0,
savedTokens: saved,
savedPct: (saved / withoutTokens) * 100,
};
} catch (err: unknown) {
return {
name, task, category,
withoutTokens, withoutDesc,
withTokens: 0, tools: [],
durationMs: Date.now() - t0,
savedTokens: withoutTokens,
savedPct: 100,
error: err instanceof Error ? err.message : String(err),
};
}
}
// ─────────────────────────────────────────────────────────────────────────────
// SCENARIO DEFINITIONS (all run in parallel)
// ─────────────────────────────────────────────────────────────────────────────
async function s1_analysis(): Promise<ScenarioResult> {
const task = 'How does the summarization engine work?';
return scenario('1 — Analysis', task, 'ANALYSIS', FULL_REPO_TOKENS, `Read all ${FILE_COUNT} src files`, async () => {
const [r1] = [await run('task_classify', { task }, taskClassify)];
const [r2] = [await run('repo_scope_find', { task, taskType: 'analysis' }, repoScopeFind)];
const topPaths = paths(r2.result.files, 5);
const [r3] = [await run('flow_summarize', { scope: topPaths }, flowSummarize)];
return [r1.usage, r2.usage, r3.usage];
});
}
async function s2_bugfix(): Promise<ScenarioResult> {
const task = 'Bug: token counter gives wrong estimate for multi-byte unicode';
return scenario('2 — Bug Fix', task, 'BUG', FULL_REPO_TOKENS, `Read all ${FILE_COUNT} src files`, async () => {
const [r1, r2] = await Promise.all([
run('task_classify', { task }, taskClassify),
run('repo_scope_find', { task, taskType: 'bug' }, repoScopeFind),
]);
const bugPaths = paths(r2.result.files, 4);
const [r3] = [await run('bug_trace_compact', { symptom: task, scope: bugPaths }, bugTraceCompacts)];
return [r1.usage, r2.usage, r3.usage];
});
}
async function s3_feature(): Promise<ScenarioResult> {
const task = 'Add symbol_count tool: returns export counts per module';
return scenario('3 — Feature', task, 'FEATURE', FULL_REPO_TOKENS, `Read all ${FILE_COUNT} src files`, async () => {
const [r1, r2] = await Promise.all([
run('task_classify', { task }, taskClassify),
run('repo_scope_find', { task, taskType: 'feature' }, repoScopeFind),
]);
const scopePaths = paths(r2.result.files, 5);
const impactPaths = paths(r2.result.files, 3);
const [r3, r4, r5] = await Promise.all([
run('implementation_plan', { task, taskType: 'feature', scope: scopePaths }, implementationPlan),
run('impact_analyze', { scope: impactPaths, changeType: 'modify' }, impactAnalyze),
run('test_select', { scope: impactPaths, changeType: 'add' }, testSelect),
]);
return [r1.usage, r2.usage, r3.usage, r4.usage, r5.usage];
});
}
async function s4_poc(): Promise<ScenarioResult> {
const task = 'POC: streaming token counter endpoint for real-time cost estimation';
// Without MCP: read all files + plan manually
const withoutTokens = FULL_REPO_TOKENS;
return scenario('4 — POC', task, 'POC', withoutTokens, `Read all ${FILE_COUNT} src files + manual plan`, async () => {
const [r1, r2] = await Promise.all([
run('task_classify', { task }, taskClassify),
run('poc_plan', { goal: task, constraints: ['no auth', 'simple'] }, pocPlan),
]);
return [r1.usage, r2.usage];
});
}
async function s5_documentation(): Promise<ScenarioResult> {
const task = 'Document the new impact_analyze changes for API consumers';
const changedFiles = [
'src/capabilities/execution/execution.ts',
'src/core/retrieval/retriever.ts',
];
return scenario('5 — Documentation', task, 'DOCS', FULL_REPO_TOKENS, `Read all ${FILE_COUNT} src files`, async () => {
const [r1, r2] = await Promise.all([
run('task_classify', { task }, taskClassify),
run('doc_context_build', { feature: task, changedFiles, audience: 'api' }, docContextBuild),
]);
const [r3] = [await run('doc_update_plan', { changedFiles }, docUpdatePlan)];
return [r1.usage, r2.usage, r3.usage];
});
}
async function s6_memory(): Promise<ScenarioResult> {
const task = 'Checkpoint a multi-session refactor and restore it next session';
const checkpointFiles = allSrcFiles.slice(0, 6);
// Without MCP: re-read all files + re-reason from scratch each session
const sessionTokens = FULL_REPO_TOKENS * 2; // two sessions
return scenario('6 — Memory (multi-session)', task, 'MEMORY', sessionTokens, 'Re-read all files × 2 sessions', async () => {
const [r1] = [await run('memory_checkpoint', {
taskId: 'refactor-001',
taskType: 'feature',
files: checkpointFiles,
decisions: [{ description: 'Use retriever pattern', rationale: 'Consistent with existing code' }],
risks: ['impact on index size'],
notes: 'Session 1 complete, 3 steps remaining',
}, memoryCheckpoint)];
const [r2] = [await run('memory_restore', { taskId: 'refactor-001' }, memoryRestore)];
return [r1.usage, r2.usage];
});
}
async function s7_resources(): Promise<ScenarioResult> {
const task = 'Understand full repo architecture before starting a large refactor';
// Without: read every source file
return scenario('7 — Resources (all 7)', task, 'RESOURCES', FULL_REPO_TOKENS, `Read all ${FILE_COUNT} src files`, async () => {
const [archMap, modIdx, symIdx, testMap, docMap, patterns, glossary] = await Promise.all([
buildArchitectureMap(),
buildModuleIndex(),
buildSymbolIndex(),
buildTestMap(),
buildDocMap(),
buildPatterns(),
buildGlossary(),
]);
const toUsage = (name: string, result: object): ToolUsage => ({
name,
tokensIn: estimateTokens(name), // resource URI is the "input"
tokensOut: estimateObjectTokens(result),
});
return [
toUsage('repo://architecture-map', archMap),
toUsage('repo://module-index', modIdx),
toUsage('repo://symbol-index', symIdx),
toUsage('repo://test-map', testMap),
toUsage('repo://doc-map', docMap),
toUsage('repo://patterns', patterns),
toUsage('repo://glossary', glossary),
];
});
}
// ─────────────────────────────────────────────────────────────────────────────
// Report printer
// ─────────────────────────────────────────────────────────────────────────────
const SEP = '─'.repeat(72);
const DSEP = '═'.repeat(72);
function bar(pct: number, w = 28): string {
const f = Math.min(Math.round(pct / 100 * w), w);
return '▓'.repeat(f) + '░'.repeat(w - f);
}
function fmt(n: number): string { return n.toLocaleString(); }
function fmsPct(n: number): string { return n.toFixed(1) + '%'; }
function pad(s: string | number, w: number): string { return String(s).padStart(w); }
function lpad(s: string, w: number): string { return String(s).padEnd(w); }
function printScenario(r: ScenarioResult): void {
const ok = r.savedPct >= 0;
const flag = r.error ? ' ⚠ ERROR' : ok ? '' : ' ❌';
console.log(SEP);
console.log(` ${r.name} [${r.category}]${flag} (${r.durationMs}ms)`);
console.log(` Task: "${r.task}"`);
if (r.error) {
console.log(` Error: ${r.error}`);
return;
}
console.log();
console.log(` WITHOUT MCP ${fmt(r.withoutTokens).padStart(8)} tok ← ${r.withoutDesc}`);
console.log(` WITH MCP ${fmt(r.withTokens).padStart(8)} tok ← ${r.tools.length} tool/resource call(s)`);
console.log();
// Per-tool breakdown
console.log(' Tool/Resource breakdown:');
for (const t of r.tools) {
const total = t.tokensIn + t.tokensOut;
console.log(` ${lpad(t.name, 28)} in:${pad(t.tokensIn,5)} out:${pad(t.tokensOut,6)} total:${pad(total,6)}`);
}
console.log();
const saved = r.savedTokens;
const pct = fmsPct(r.savedPct);
if (saved > 0) {
console.log(` ✅ Saved ${fmt(saved)} tokens ${bar(r.savedPct)} ${pct}`);
} else {
console.log(` ⚠️ MCP output exceeds raw file cost by ${fmt(-saved)} tokens`);
}
console.log();
}
function printSummary(results: ScenarioResult[]): void {
const valid = results.filter(r => !r.error);
const totalWithout = valid.reduce((s, r) => s + r.withoutTokens, 0);
const totalWith = valid.reduce((s, r) => s + r.withTokens, 0);
const totalSaved = totalWithout - totalWith;
const totalPct = (totalSaved / totalWithout) * 100;
const totalMs = results.reduce((s, r) => s + r.durationMs, 0);
// All tool names used
const allTools = new Set(results.flatMap(r => r.tools.map(t => t.name)));
const toolList = [
'task_classify','repo_scope_find','flow_summarize','bug_trace_compact',
'implementation_plan','poc_plan','impact_analyze','test_select',
'doc_context_build','doc_update_plan','memory_checkpoint','memory_restore',
'repo://architecture-map','repo://module-index','repo://symbol-index',
'repo://test-map','repo://doc-map','repo://patterns','repo://glossary',
];
console.log(DSEP);
console.log();
console.log(' OVERALL SUMMARY');
console.log();
console.log(` Scenarios run : ${results.length} (${results.filter(r=>!r.error).length} passed, ${results.filter(r=>r.error).length} failed)`);
console.log(` Total wall time : ${totalMs}ms`);
console.log(` Repo size : ${FILE_COUNT} source files · ${fmt(FULL_REPO_TOKENS)} tokens`);
console.log();
console.log(` Total WITHOUT MCP : ${fmt(totalWithout)} tokens`);
console.log(` Total WITH MCP : ${fmt(totalWith)} tokens`);
console.log(` Total Saved : ${fmt(totalSaved)} tokens`);
console.log();
console.log(` Overall Reduction : ${bar(totalPct, 32)} ${fmsPct(totalPct)}`);
console.log();
// Per-scenario table
console.log(' Per-Scenario:');
console.log(' ' + '─'.repeat(68));
console.log(' ' + lpad('Scenario', 36) + pad('Without', 9) + pad('With', 8) + pad('Saved', 9) + ' ms');
console.log(' ' + '─'.repeat(68));
for (const r of results) {
const tag = r.error ? ' ERR' : '';
console.log(
' ' + lpad(r.name + tag, 36) +
pad(fmt(r.withoutTokens), 9) +
pad(fmt(r.withTokens), 8) +
pad(fmsPct(r.savedPct), 9) +
pad(r.durationMs, 6)
);
}
console.log(' ' + '─'.repeat(68));
console.log();
// Tool coverage table
console.log(' Tool & Resource Coverage:');
console.log(' ' + '─'.repeat(50));
const covered = toolList.filter(t => allTools.has(t));
const missed = toolList.filter(t => !allTools.has(t));
for (const t of covered) console.log(` ✅ ${t}`);
for (const t of missed) console.log(` ⬜ ${t} (not invoked)`);
console.log(` Coverage: ${covered.length}/${toolList.length} (${fmsPct(covered.length / toolList.length * 100)})`);
console.log();
// Verdict
let verdict: string;
if (totalPct >= 90) verdict = '🟢 EXCELLENT — MCP eliminates >90% of token overhead';
else if (totalPct >= 70) verdict = '🟡 GOOD — Solid savings, MCP working as designed';
else if (totalPct >= 50) verdict = '🟠 MODERATE — Some savings, room to improve summaries';
else if (totalPct >= 0) verdict = '🔴 LOW — MCP overhead nearly equals file cost';
else verdict = '❌ NEGATIVE — MCP responses larger than raw source';
console.log(` Verdict: ${verdict}`);
console.log();
// Notes
console.log(' Methodology:');
console.log(' • Token estimation: project\'s own estimateTokens() (~4 chars + 1.3×words / 2)');
console.log(' • WITHOUT = Claude reads source files directly (one Read call per file)');
console.log(' • WITH = tokens in tool inputs + tokens in tool outputs combined');
console.log(' • Scenarios 1–5 run in parallel; memory scenario reuses checkpoint from s6');
console.log(' • Real API savings are higher: MCP also reduces follow-up calls + reasoning');
console.log(' • Savings scale with repo size: 10× larger repo → ~10× more tokens saved');
console.log();
console.log(DSEP);
}
// ─────────────────────────────────────────────────────────────────────────────
// Main
// ─────────────────────────────────────────────────────────────────────────────
async function main() {
console.log('\n' + DSEP);
console.log(' MCP TOKEN REDUCTION — COMPREHENSIVE REAL-TIME TEST REPORT');
console.log(` Date: ${new Date().toISOString().slice(0, 19).replace('T', ' ')} Model: claude-sonnet-4-6`);
console.log(DSEP + '\n');
console.log(` Indexing repo (${FILE_COUNT} files, ~${fmt(FULL_REPO_TOKENS)} baseline tokens)...`);
// Index repo once before all scenarios
await repoScopeFind({ task: 'index', taskType: 'analysis', repoPath: REPO_PATH });
console.log(' Index ready. Running 7 scenarios in parallel...\n');
// Run 1-5 in parallel (independent), 6 sequential (memory), 7 parallel resources
const [r1, r2, r3, r4, r5, r7] = await Promise.all([
s1_analysis(),
s2_bugfix(),
s3_feature(),
s4_poc(),
s5_documentation(),
s7_resources(),
]);
const r6 = await s6_memory(); // must run after index is ready
const results: ScenarioResult[] = [r1, r2, r3, r4, r5, r6, r7];
for (const r of results) printScenario(r);
printSummary(results);
}
main().catch(err => {
console.error('\n[FATAL]', err);
process.exit(1);
});