run_frontend_performance.ps1 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. $ErrorActionPreference = "Stop"
  2. $reportDir = Join-Path $PSScriptRoot "reports"
  3. $htmlReport = Join-Path $reportDir "frontend-performance-report.html"
  4. $csvPrefix = Join-Path $reportDir "frontend-performance"
  5. $pendingFile = Join-Path $reportDir "frontend_performance_pending.txt"
  6. New-Item -ItemType Directory -Path $reportDir -Force | Out-Null
  7. $locust = Get-Command locust -ErrorAction SilentlyContinue
  8. if ($null -eq $locust) {
  9. @(
  10. "ServicePilot 前端性能测试未执行",
  11. "原因:当前环境未安装 Locust 命令。",
  12. "安装命令:pip install -r requirements.txt",
  13. "执行命令:powershell -ExecutionPolicy Bypass -File .\run_frontend_performance.ps1",
  14. "预期输出:reports/frontend-performance-report.html 与 reports/frontend-performance_*.csv"
  15. ) | Set-Content -Path $pendingFile -Encoding UTF8
  16. Write-Host "Locust 未安装,已生成待执行说明:$pendingFile"
  17. exit 1
  18. }
  19. & $locust.Source -f "$PSScriptRoot\locustfile_frontend.py" `
  20. --host "http://127.0.0.1:4301" `
  21. --headless `
  22. -u 30 `
  23. -r 5 `
  24. -t 3m `
  25. --html "$htmlReport" `
  26. --csv "$csvPrefix"
  27. $exitCode = $LASTEXITCODE
  28. if ($exitCode -ne 0) {
  29. exit $exitCode
  30. }
  31. if (Test-Path $pendingFile) {
  32. Remove-Item -LiteralPath $pendingFile -Force
  33. }
  34. $statsFile = "${csvPrefix}_stats.csv"
  35. $summaryFile = Join-Path $reportDir "frontend_performance_summary.txt"
  36. $aggregate = Import-Csv -LiteralPath $statsFile | Where-Object { $_.Name -eq "Aggregated" } | Select-Object -First 1
  37. if ($null -ne $aggregate) {
  38. $requestCount = [int]$aggregate.'Request Count'
  39. $failureCount = [int]$aggregate.'Failure Count'
  40. $failureRate = 0
  41. if ($requestCount -gt 0) {
  42. $failureRate = [math]::Round(($failureCount / $requestCount) * 100, 2)
  43. }
  44. @(
  45. "ServicePilot 前端性能测试执行记录",
  46. "执行时间:$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
  47. "压测工具:Locust",
  48. "测试对象:http://127.0.0.1:4301",
  49. "压测配置:30 用户,启动速率 5 用户/秒,持续 3 分钟",
  50. "请求总数:$requestCount",
  51. "失败请求数:$failureCount",
  52. "失败率:$failureRate%",
  53. "平均响应时间:$([math]::Round([double]$aggregate.'Average Response Time', 2)) ms",
  54. "P95 响应时间:$($aggregate.'95%') ms",
  55. "最大响应时间:$([math]::Round([double]$aggregate.'Max Response Time', 2)) ms",
  56. "吞吐量:$([math]::Round([double]$aggregate.'Requests/s', 2)) req/s",
  57. "HTML 报告:$htmlReport",
  58. "CSV 明细:$statsFile",
  59. "执行结果:PASS"
  60. ) | Set-Content -Path $summaryFile -Encoding UTF8
  61. Write-Host "前端性能测试摘要已生成:$summaryFile"
  62. }