const fs = require('fs'); const path = require('path'); console.log('🚀 测试性能优化工具...'); const mockTestResults = [ { testId: 'test-1', file: 'smoke/navigation.smoke.spec.ts', title: '应该成功加载首页', duration: 15000, tier: 'fast', history: [12000, 14000, 15000, 16000, 18000], }, { testId: 'test-2', file: 'admin/news-management.spec.ts', title: '应该能够创建新闻', duration: 45000, tier: 'standard', history: [40000, 42000, 45000, 48000, 50000], }, { testId: 'test-3', file: 'api/admin.api.spec.ts', title: '应该能够获取内容列表', duration: 80000, tier: 'fast', history: [60000, 70000, 80000, 90000, 100000], }, { testId: 'test-4', file: 'visual/homepage-visual.spec.ts', title: '首页视觉回归测试', duration: 150000, tier: 'deep', history: [120000, 130000, 150000, 170000, 180000], }, { testId: 'test-5', file: 'responsive/mobile-navigation.spec.ts', title: '移动端导航功能测试', duration: 95000, tier: 'standard', history: [80000, 85000, 95000, 105000, 110000], }, ]; const optimizer = { rules: [ { name: 'slow-test', condition: (p) => p.duration > 60000, suggestions: [ '考虑将测试拆分为多个小测试', '检查是否有不必要的等待时间', '优化选择器以提高定位速度', ], }, { name: 'very-slow-test', condition: (p) => p.duration > 120000, suggestions: [ '测试执行时间过长,强烈建议拆分', '检查是否有性能瓶颈(如大量DOM操作)', ], }, { name: 'tier-fast-slow', condition: (p) => p.tier === 'fast' && p.duration > 30000, suggestions: [ '快速层测试不应超过30秒', '重新评估测试是否属于快速层', ], }, ], analyzePerformance(testId, file, title, duration, tier, history) { 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 = []; for (const rule of this.rules) { if (rule.condition({ testId, file, title, duration, tier, avgDuration, percentile, isSlow })) { suggestions.push(...rule.suggestions); } } return { testId, file, title, duration, tier, avgDuration, percentile, isSlow, optimizationSuggestions: suggestions, }; }, generateOptimizationReport(performances) { 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; let report = '🚀 测试性能优化报告\n'; report += '='.repeat(50) + '\n\n'; report += `总测试数: ${performances.length}\n`; report += `慢速测试: ${slowTests.length}\n`; report += `潜在节省时间: ${(potentialSavings / 1000).toFixed(2)}s\n\n`; const sortedSlowTests = slowTests.sort((a, b) => b.duration - a.duration); if (sortedSlowTests.length > 0) { report += '🐌 慢速测试详情:\n'; sortedSlowTests.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; }, }; console.log('📊 分析测试性能...'); const performances = mockTestResults.map(result => optimizer.analyzePerformance( result.testId, result.file, result.title, result.duration, result.tier, result.history ) ); const report = optimizer.generateOptimizationReport(performances); console.log(report); const slowTestsCount = performances.filter(p => p.isSlow).length; if (slowTestsCount > 0) { console.log(`\n✅ 优化器工作正常,发现 ${slowTestsCount} 个慢速测试`); process.exit(0); } else { console.log('\n⚠️ 未发现慢速测试'); process.exit(1); }