feat: add test performance optimization tool

This commit is contained in:
张翔
2026-03-13 11:58:15 +08:00
parent b09673b036
commit 6c6e9f002f
2 changed files with 392 additions and 0 deletions
+222
View File
@@ -0,0 +1,222 @@
interface TestPerformance {
testId: string;
file: string;
title: string;
duration: number;
tier: string;
avgDuration: number;
percentile: number;
isSlow: boolean;
optimizationSuggestions: string[];
}
interface OptimizationRule {
name: string;
condition: (perf: TestPerformance) => boolean;
suggestions: string[];
}
export class TestOptimizer {
private rules: OptimizationRule[] = [];
constructor() {
this.initializeRules();
}
private initializeRules(): void {
this.rules = [
{
name: 'slow-test',
condition: (p) => p.duration > 60000,
suggestions: [
'考虑将测试拆分为多个小测试',
'检查是否有不必要的等待时间',
'优化选择器以提高定位速度',
'考虑使用mock数据替代真实API调用',
],
},
{
name: 'very-slow-test',
condition: (p) => p.duration > 120000,
suggestions: [
'测试执行时间过长,强烈建议拆分',
'检查是否有性能瓶颈(如大量DOM操作)',
'考虑使用并行测试策略',
'评估是否需要完整加载所有资源',
],
},
{
name: 'flaky-test',
condition: (p) => p.percentile > 90,
suggestions: [
'测试执行时间波动较大,可能存在稳定性问题',
'增加重试次数或使用更稳定的等待策略',
'检查网络请求的稳定性',
'考虑添加更明确的断言条件',
],
},
{
name: 'tier-fast-slow',
condition: (p) => p.tier === 'fast' && p.duration > 30000,
suggestions: [
'快速层测试不应超过30秒',
'重新评估测试是否属于快速层',
'优化测试逻辑或移至标准层',
],
},
{
name: 'tier-standard-slow',
condition: (p) => p.tier === 'standard' && p.duration > 90000,
suggestions: [
'标准层测试不应超过90秒',
'考虑拆分测试或优化执行流程',
'评估是否需要移至深度层',
],
},
];
}
analyzePerformance(
testId: string,
file: string,
title: string,
duration: number,
tier: string,
history: number[]
): TestPerformance {
const avgDuration = history.length > 0
? history.reduce((sum, d) => sum + d, 0) / history.length
: duration;
const sortedHistory = [...history].sort((a, b) => a - b);
const percentile = sortedHistory.length > 0
? sortedHistory[Math.floor(sortedHistory.length * 0.9)]
: duration;
const isSlow = duration > 60000;
const suggestions: string[] = [];
for (const rule of this.rules) {
if (rule.condition({
testId,
file,
title,
duration,
tier,
avgDuration,
percentile,
isSlow,
optimizationSuggestions: [],
})) {
suggestions.push(...rule.suggestions);
}
}
return {
testId,
file,
title,
duration,
tier,
avgDuration,
percentile,
isSlow,
optimizationSuggestions: suggestions,
};
}
optimizeTestSuite(performances: TestPerformance[]): {
totalTests: number;
slowTests: number;
potentialSavings: number;
recommendations: string[];
} {
const slowTests = performances.filter(p => p.isSlow);
const totalDuration = performances.reduce((sum, p) => sum + p.duration, 0);
const slowDuration = slowTests.reduce((sum, p) => sum + p.duration, 0);
const potentialSavings = slowDuration * 0.3; // 假设优化可节省30%时间
const recommendations: string[] = [];
if (slowTests.length > 0) {
recommendations.push(`发现 ${slowTests.length} 个慢速测试,建议优先优化`);
}
const tierSlowTests = {
fast: slowTests.filter(p => p.tier === 'fast').length,
standard: slowTests.filter(p => p.tier === 'standard').length,
deep: slowTests.filter(p => p.tier === 'deep').length,
};
if (tierSlowTests.fast > 0) {
recommendations.push(`${tierSlowTests.fast} 个快速层测试执行过慢,建议重新评估分层`);
}
if (tierSlowTests.standard > 5) {
recommendations.push(`${tierSlowTests.standard} 个标准层测试执行过慢,建议优化或拆分`);
}
const avgDuration = totalDuration / performances.length;
if (avgDuration > 60000) {
recommendations.push('平均测试执行时间超过60秒,建议整体优化测试策略');
}
return {
totalTests: performances.length,
slowTests: slowTests.length,
potentialSavings,
recommendations,
};
}
generateOptimizationReport(performances: TestPerformance[]): string {
const analysis = this.optimizeTestSuite(performances);
let report = '🚀 测试性能优化报告\n';
report += '='.repeat(50) + '\n\n';
report += `总测试数: ${analysis.totalTests}\n`;
report += `慢速测试: ${analysis.slowTests}\n`;
report += `潜在节省时间: ${(analysis.potentialSavings / 1000).toFixed(2)}s\n\n`;
if (analysis.recommendations.length > 0) {
report += '📋 优化建议:\n';
analysis.recommendations.forEach((rec, index) => {
report += ` ${index + 1}. ${rec}\n`;
});
report += '\n';
}
const slowTests = performances.filter(p => p.isSlow)
.sort((a, b) => b.duration - a.duration);
if (slowTests.length > 0) {
report += '🐌 慢速测试详情:\n';
slowTests.forEach((test, index) => {
report += `\n${index + 1}. ${test.title}\n`;
report += ` 文件: ${test.file}\n`;
report += ` 耗时: ${(test.duration / 1000).toFixed(2)}s\n`;
report += ` 层级: ${test.tier}\n`;
if (test.optimizationSuggestions.length > 0) {
report += ` 优化建议:\n`;
test.optimizationSuggestions.forEach(suggestion => {
report += ` - ${suggestion}\n`;
});
}
});
} else {
report += '✅ 未发现慢速测试\n';
}
return report;
}
addRule(rule: OptimizationRule): void {
this.rules.push(rule);
}
removeRule(ruleName: string): void {
this.rules = this.rules.filter(r => r.name !== ruleName);
}
}