$ErrorActionPreference = "Stop" $reportDir = Join-Path $PSScriptRoot "reports" $htmlReport = Join-Path $reportDir "frontend-performance-report.html" $csvPrefix = Join-Path $reportDir "frontend-performance" $pendingFile = Join-Path $reportDir "frontend_performance_pending.txt" New-Item -ItemType Directory -Path $reportDir -Force | Out-Null $locust = Get-Command locust -ErrorAction SilentlyContinue if ($null -eq $locust) { @( "ServicePilot 前端性能测试未执行", "原因:当前环境未安装 Locust 命令。", "安装命令:pip install -r requirements.txt", "执行命令:powershell -ExecutionPolicy Bypass -File .\run_frontend_performance.ps1", "预期输出:reports/frontend-performance-report.html 与 reports/frontend-performance_*.csv" ) | Set-Content -Path $pendingFile -Encoding UTF8 Write-Host "Locust 未安装,已生成待执行说明:$pendingFile" exit 1 } & $locust.Source -f "$PSScriptRoot\locustfile_frontend.py" ` --host "http://127.0.0.1:4301" ` --headless ` -u 30 ` -r 5 ` -t 3m ` --html "$htmlReport" ` --csv "$csvPrefix" $exitCode = $LASTEXITCODE if ($exitCode -ne 0) { exit $exitCode } if (Test-Path $pendingFile) { Remove-Item -LiteralPath $pendingFile -Force } $statsFile = "${csvPrefix}_stats.csv" $summaryFile = Join-Path $reportDir "frontend_performance_summary.txt" $aggregate = Import-Csv -LiteralPath $statsFile | Where-Object { $_.Name -eq "Aggregated" } | Select-Object -First 1 if ($null -ne $aggregate) { $requestCount = [int]$aggregate.'Request Count' $failureCount = [int]$aggregate.'Failure Count' $failureRate = 0 if ($requestCount -gt 0) { $failureRate = [math]::Round(($failureCount / $requestCount) * 100, 2) } @( "ServicePilot 前端性能测试执行记录", "执行时间:$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')", "压测工具:Locust", "测试对象:http://127.0.0.1:4301", "压测配置:30 用户,启动速率 5 用户/秒,持续 3 分钟", "请求总数:$requestCount", "失败请求数:$failureCount", "失败率:$failureRate%", "平均响应时间:$([math]::Round([double]$aggregate.'Average Response Time', 2)) ms", "P95 响应时间:$($aggregate.'95%') ms", "最大响应时间:$([math]::Round([double]$aggregate.'Max Response Time', 2)) ms", "吞吐量:$([math]::Round([double]$aggregate.'Requests/s', 2)) req/s", "HTML 报告:$htmlReport", "CSV 明细:$statsFile", "执行结果:PASS" ) | Set-Content -Path $summaryFile -Encoding UTF8 Write-Host "前端性能测试摘要已生成:$summaryFile" }