# Simple Performance Benchmark Script param( [int]$ConcurrentUsers = 10, [int]$DurationSeconds = 20 ) $ErrorActionPreference = "Stop" Write-Host "========================================" -ForegroundColor Cyan Write-Host "Performance Benchmark Test" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "Concurrent Users: $ConcurrentUsers" Write-Host "Test Duration: $DurationSeconds seconds" Write-Host "========================================" -ForegroundColor Cyan # Test endpoints $testEndpoints = @( @{ Name = "API Gateway"; Url = "http://localhost:8080/actuator/health" }, @{ Name = "User Service"; Url = "http://localhost:8081/actuator/health" }, @{ Name = "Auth Service"; Url = "http://localhost:8082/actuator/health" }, @{ Name = "Merchant Service"; Url = "http://localhost:8083/actuator/health" }, @{ Name = "Order Service"; Url = "http://localhost:8084/actuator/health" }, @{ Name = "Rider Service"; Url = "http://localhost:8085/actuator/health" } ) $allResults = @{} foreach ($endpoint in $testEndpoints) { Write-Host "`n[Testing] $($endpoint.Name)..." -ForegroundColor Yellow $results = @{ TotalRequests = 0 SuccessRequests = 0 FailedRequests = 0 ResponseTimes = @() StartTime = $null EndTime = $null } # Warmup try { $null = Invoke-WebRequest -Uri $endpoint.Url -UseBasicParsing -TimeoutSec 5 Write-Host " Warmup completed" -ForegroundColor Green } catch { Write-Host " Warmup failed: $_" -ForegroundColor Red } $results.StartTime = Get-Date $jobs = @() # Launch concurrent jobs for ($i = 0; $i -lt $ConcurrentUsers; $i++) { $jobs += Start-Job -ScriptBlock { param($Url, $DurationSeconds) $endTime = (Get-Date).AddSeconds($DurationSeconds) $localResults = @{ Requests = 0 Success = 0 Failed = 0 ResponseTimes = @() } while ((Get-Date) -lt $endTime) { try { $sw = [System.Diagnostics.Stopwatch]::StartNew() $null = Invoke-WebRequest -Uri $Url -UseBasicParsing -TimeoutSec 10 $sw.Stop() $localResults.Requests++ $localResults.Success++ $localResults.ResponseTimes += $sw.Elapsed.TotalMilliseconds } catch { $localResults.Requests++ $localResults.Failed++ } } return $localResults } -ArgumentList $endpoint.Url, $DurationSeconds } $jobs | Wait-Job | Out-Null $results.EndTime = Get-Date # Collect results foreach ($job in $jobs) { $jobResult = Receive-Job -Job $job $results.TotalRequests += $jobResult.Requests $results.SuccessRequests += $jobResult.Success $results.FailedRequests += $jobResult.Failed $results.ResponseTimes += $jobResult.ResponseTimes Remove-Job -Job $job } # Calculate statistics $totalTime = ($results.EndTime - $results.StartTime).TotalSeconds $avgResponseTime = if ($results.ResponseTimes.Count -gt 0) { ($results.ResponseTimes | Measure-Object -Average).Average } else { 0 } $p50ResponseTime = if ($results.ResponseTimes.Count -gt 0) { ($results.ResponseTimes | Sort-Object)[[Math]::Floor($results.ResponseTimes.Count * 0.5)] } else { 0 } $p95ResponseTime = if ($results.ResponseTimes.Count -gt 0) { ($results.ResponseTimes | Sort-Object)[[Math]::Floor($results.ResponseTimes.Count * 0.95)] } else { 0 } $p99ResponseTime = if ($results.ResponseTimes.Count -gt 0) { ($results.ResponseTimes | Sort-Object)[[Math]::Floor($results.ResponseTimes.Count * 0.99)] } else { 0 } $successRate = if ($results.TotalRequests -gt 0) { ($results.SuccessRequests / $results.TotalRequests) * 100 } else { 0 } $rps = if ($totalTime -gt 0) { $results.TotalRequests / $totalTime } else { 0 } $allResults[$endpoint.Name] = @{ AvgResponseTime = $avgResponseTime P50ResponseTime = $p50ResponseTime P95ResponseTime = $p95ResponseTime P99ResponseTime = $p99ResponseTime SuccessRate = $successRate RPS = $rps TotalRequests = $results.TotalRequests SuccessRequests = $results.SuccessRequests FailedRequests = $results.FailedRequests } Write-Host " Completed! Total $($results.TotalRequests) requests, success rate $([Math]::Round($successRate, 2))%" -ForegroundColor Green } # Generate report $timestamp = Get-Date -Format "yyyyMMdd_HHmmss" $report = "========================================`n" $report += "Performance Benchmark Report`n" $report += "========================================`n" $report += "Test Time: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")`n" $report += "Concurrent Users: $ConcurrentUsers`n" $report += "Test Duration: $DurationSeconds seconds`n`n" foreach ($svcName in $allResults.Keys) { $data = $allResults[$svcName] $report += "=== $svcName ===`n" $report += "Total Requests: $($data.TotalRequests)`n" $report += "Success Requests: $($data.SuccessRequests)`n" $report += "Failed Requests: $($data.FailedRequests)`n" $report += "Success Rate: $([Math]::Round($data.SuccessRate, 2))%`n" $report += "Throughput: $([Math]::Round($data.RPS, 2)) req/s`n" $report += "Response Time (ms):`n" $report += " Avg: $([Math]::Round($data.AvgResponseTime, 2))`n" $report += " P50: $([Math]::Round($data.P50ResponseTime, 2))`n" $report += " P95: $([Math]::Round($data.P95ResponseTime, 2))`n" $report += " P99: $([Math]::Round($data.P99ResponseTime, 2))`n`n" } $report += "========================================`n" Write-Host "`n`n$report" # Save report $reportPath = "d:\FzxBDSH\FzxFile\benchmark-report_$timestamp.md" $report | Out-File -FilePath $reportPath -Encoding UTF8 Write-Host "`nReport saved to: $reportPath" -ForegroundColor Cyan return $reportPath, $allResults