const { execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); const RESULTS_DIR = path.join(__dirname, '../../test-results/performance'); const REPORT_FILE = path.join(RESULTS_DIR, 'performance-report.json'); const SUMMARY_FILE = path.join(RESULTS_DIR, 'performance-summary.txt'); function ensureResultsDir() { if (!fs.existsSync(RESULTS_DIR)) { fs.mkdirSync(RESULTS_DIR, { recursive: true }); } } function runPerformanceTests() { console.log('开始运行性能测试...'); console.log('='.repeat(50)); ensureResultsDir(); try { const startTime = Date.now(); execSync('npx playwright test e2e/performance/page-load.spec.ts --reporter=json --reporter-file=test-results/performance/page-load.json', { stdio: 'inherit', cwd: path.join(__dirname, '../..'), }); execSync('npx playwright test e2e/performance/animation.spec.ts --reporter=json --reporter-file=test-results/performance/animation.json', { stdio: 'inherit', cwd: path.join(__dirname, '../..'), }); const endTime = Date.now(); const duration = ((endTime - startTime) / 1000).toFixed(2); console.log('='.repeat(50)); console.log(`性能测试完成,耗时: ${duration}秒`); console.log('='.repeat(50)); generateSummary(); return { success: true, duration }; } catch (error) { console.error('性能测试失败:', error.message); return { success: false, error: error.message }; } } function generateSummary() { console.log('生成性能测试摘要...'); const pageLoadResults = readTestResults('page-load.json'); const animationResults = readTestResults('animation.json'); const summary = generatePerformanceSummary(pageLoadResults, animationResults); fs.writeFileSync(SUMMARY_FILE, summary, 'utf-8'); console.log('性能测试摘要已生成:', SUMMARY_FILE); } function readTestResults(filename) { const filePath = path.join(RESULTS_DIR, filename); if (!fs.existsSync(filePath)) { return { suites: [], specs: [], tests: [] }; } const content = fs.readFileSync(filePath, 'utf-8'); return JSON.parse(content); } function generatePerformanceSummary(pageLoadResults, animationResults) { const pageLoadTests = pageLoadResults.tests || []; const animationTests = animationResults.tests || []; const totalTests = pageLoadTests.length + animationTests.length; const passedTests = [...pageLoadTests, ...animationTests].filter(test => test.status === 'passed').length; const failedTests = totalTests - passedTests; const summary = ` 性能测试报告摘要 ================ 测试时间: ${new Date().toISOString()} 测试数量: ${totalTests} 通过数量: ${passedTests} 失败数量: ${failedTests} 通过率: ${((passedTests / totalTests) * 100).toFixed(2)}% 页面加载性能测试 ---------------- 测试数量: ${pageLoadTests.length} 通过数量: ${pageLoadTests.filter(test => test.status === 'passed').length} 失败数量: ${pageLoadTests.filter(test => test.status === 'failed').length} 动画性能测试 ------------ 测试数量: ${animationTests.length} 通过数量: ${animationTests.filter(test => test.status === 'passed').length} 失败数量: ${animationTests.filter(test => test.status === 'failed').length} 失败测试详情 ------------ ${[...pageLoadTests, ...animationTests] .filter(test => test.status === 'failed') .map((test, index) => ` ${index + 1}. ${test.title} - 文件: ${test.location.file} - 行号: ${test.location.line} - 错误: ${test.error?.message || '未知错误'} `).join('\n')} 性能指标 -------- 页面加载性能阈值: - 页面加载时间: < 2000ms - 首次内容绘制: < 1000ms - 最大内容绘制: < 1500ms - 可交互时间: < 2000ms - 累积布局偏移: < 0.1 - 首次输入延迟: < 100ms 动画性能阈值: - 动画帧率: > 30fps - 帧时间: < 33.33ms - 动画持续时间: < 500ms 建议 ---- ${failedTests > 0 ? ` 1. 优先修复失败的测试用例 2. 检查性能指标是否超过阈值 3. 优化页面加载性能 4. 优化动画性能 ` : ` 1. 持续监控性能指标 2. 定期运行性能测试 3. 记录性能基线 4. 及时发现性能退化 `} `; return summary; } function main() { const args = process.argv.slice(2); const command = args[0] || 'run'; switch (command) { case 'run': const result = runPerformanceTests(); process.exit(result.success ? 0 : 1); break; case 'summary': generateSummary(); process.exit(0); break; default: console.log('用法: node run-tests.js [run|summary]'); console.log(' run - 运行性能测试'); console.log(' summary - 生成性能测试摘要'); process.exit(1); } } if (require.main === module) { main(); } module.exports = { runPerformanceTests, generateSummary, };