interface PerformanceMetrics {
startupTime: number;
firstResponseTime: number;
toolExecutionTimes: Map<string, number[]>;
tokenCounts: number[];
cacheHitRate: number;
}
function collectMetrics(): PerformanceMetrics {
return {
startupTime: performance.now() - startTime,
firstResponseTime: firstResponseTime - startTime,
toolExecutionTimes: toolMetrics,
tokenCounts: tokenHistory,
cacheHitRate: cacheHits / (cacheHits + cacheMisses),
};
}// 启用性能分析
process.env.CLAUDE_CODE_PROFILE = '1';
// 输出性能报告
function printPerformanceReport() {
const metrics = collectMetrics();
console.log('Performance Report:');
console.log(`Startup: ${metrics.startupTime}ms`);
console.log(`First Response: ${metrics.firstResponseTime}ms`);
console.log(`Cache Hit Rate: ${(metrics.cacheHitRate * 100).toFixed(1)}%`);
// 工具执行时间
for (const [tool, times] of metrics.toolExecutionTimes) {
const avg = times.reduce((a, b) => a + b) / times.length;
console.log(`${tool}: ${avg.toFixed(0)}ms (avg)`);
}
}