#!/usr/bin/env ts-node
import { SemanticCompactorBenchmark } from '../src/core/compactor/benchmark';
import path from 'path';
async function main() {
const projectPath = process.argv[2] || process.cwd();
const benchmark = new SemanticCompactorBenchmark();
console.log('š Semantic Compactor Benchmark');
console.log(`š Project: ${projectPath}`);
try {
// Basic benchmark
console.log('\n1ļøā£ Running basic benchmark...');
const basicResult = await benchmark.benchmarkProject(projectPath);
// Performance-optimized benchmark
console.log('\n2ļøā£ Running performance-optimized benchmark...');
const optimizedResult = await benchmark.benchmarkProject(projectPath, {
maxConcurrentFiles: 20,
astOptions: {
maxFunctionBodyLines: 3,
includePrivateMethods: false,
includeComments: false,
},
deduplicationOptions: {
enableCrossFileDeduplication: true,
similarityThreshold: 0.9,
},
});
// Quality-focused benchmark
console.log('\n3ļøā£ Running quality-focused benchmark...');
const qualityResult = await benchmark.benchmarkProject(projectPath, {
astOptions: {
includeComments: true,
includePrivateMethods: true,
maxFunctionBodyLines: 10,
},
includeDocstrings: true,
minSymbolImportance: 5,
});
// Comparison
console.log('\nš COMPARISON RESULTS:');
console.log('='.repeat(60));
const results = [
{ name: 'Basic', result: basicResult },
{ name: 'Performance', result: optimizedResult },
{ name: 'Quality', result: qualityResult },
];
// Print comparison table
console.log('Configuration | Time(ms) | Compression | Quality | Memory(MB)');
console.log('-'.repeat(65));
results.forEach(({ name, result }) => {
const time = result.metrics.totalProcessingTime.toFixed(0);
const compression = (result.metrics.compressionRatio * 100).toFixed(1) + '%';
const quality = result.metrics.averageSymbolImportance.toFixed(1);
const memory = (result.memoryUsage.heapUsed / 1024 / 1024).toFixed(1);
console.log(
`${name.padEnd(15)} | ${time.padStart(8)} | ${compression.padStart(11)} | ${quality.padStart(7)} | ${memory.padStart(9)}`
);
});
// Recommendations
console.log('\nš” RECOMMENDATIONS:');
console.log('='.repeat(60));
const fastest = results.reduce((min, curr) =>
curr.result.metrics.totalProcessingTime < min.result.metrics.totalProcessingTime ? curr : min
);
const bestCompression = results.reduce((min, curr) =>
curr.result.metrics.compressionRatio < min.result.metrics.compressionRatio ? curr : min
);
const bestQuality = results.reduce((max, curr) =>
curr.result.metrics.averageSymbolImportance > max.result.metrics.averageSymbolImportance
? curr
: max
);
console.log(
`š Fastest: ${fastest.name} (${fastest.result.metrics.totalProcessingTime.toFixed(0)}ms)`
);
console.log(
`šļø Best Compression: ${bestCompression.name} (${(bestCompression.result.metrics.compressionRatio * 100).toFixed(1)}%)`
);
console.log(
`ā Best Quality: ${bestQuality.name} (${bestQuality.result.metrics.averageSymbolImportance.toFixed(1)} avg importance)`
);
// Token savings
console.log('\nš° TOKEN SAVINGS:');
console.log('='.repeat(60));
results.forEach(({ name, result }) => {
const savings = result.metrics.spaceSavedTokens;
const percentage =
((result.metrics.estimatedOriginalTokens - result.metrics.compactedTokens) /
result.metrics.estimatedOriginalTokens) *
100;
console.log(
`${name}: ${savings.toLocaleString()} tokens saved (${percentage.toFixed(1)}% reduction)`
);
});
// Performance insights
console.log('\nā” PERFORMANCE INSIGHTS:');
console.log('='.repeat(60));
const avgProcessingTime =
results.reduce((sum, r) => sum + r.result.metrics.totalProcessingTime, 0) / results.length;
const avgFilesPerSecond =
results.reduce(
(sum, r) =>
sum + r.result.metrics.processedFiles / (r.result.metrics.totalProcessingTime / 1000),
0
) / results.length;
console.log(`š Average processing time: ${avgProcessingTime.toFixed(0)}ms`);
console.log(`š Average files per second: ${avgFilesPerSecond.toFixed(1)}`);
console.log(
`š§ Average memory usage: ${(results.reduce((sum, r) => sum + r.result.memoryUsage.heapUsed, 0) / results.length / 1024 / 1024).toFixed(1)}MB`
);
// Save detailed results
const outputPath = path.join(
process.cwd(),
`benchmark-${new Date().toISOString().slice(0, 10)}.json`
);
await benchmark.generateReport(
results.map(r => r.result),
outputPath
);
console.log(`\nš Detailed results saved to: ${outputPath}`);
} catch (error) {
console.error('ā Benchmark failed:', error);
process.exit(1);
}
}
if (require.main === module) {
main();
}