Files
data-engine/test-hpa-strong.ps1
2026-04-17 17:46:13 +08:00

133 lines
4.6 KiB
PowerShell

Write-Host "=== HPA Stress Test ===" -ForegroundColor Cyan
Write-Host ""
# 配置参数
$url = "http://localhost:30301"
$jobCount = 50
$requestPerJob = 5000
Write-Host "Configuration:" -ForegroundColor Yellow
Write-Host " Target URL: $url" -ForegroundColor White
Write-Host " Concurrent Jobs: $jobCount" -ForegroundColor White
Write-Host " Requests per Job: $requestPerJob" -ForegroundColor White
Write-Host " Total Requests: $($jobCount * $requestPerJob)" -ForegroundColor White
Write-Host ""
# 清理旧任务
Write-Host "[1/4] Cleaning up old jobs..." -ForegroundColor Cyan
Get-Job | Where-Object { $_.Name -like "HPA-*" } | Stop-Job | Remove-Job
Start-Sleep -Seconds 2
Write-Host "[OK] Cleaned" -ForegroundColor Green
Write-Host ""
# 启动并发任务
Write-Host "[2/4] Starting $jobCount concurrent jobs..." -ForegroundColor Cyan
$jobs = @()
for ($i = 1; $i -le $jobCount; $i++) {
try {
$job = Start-Job -Name "HPA-$i" -ScriptBlock {
param($targetUrl, $totalRequests)
$success = 0
$failed = 0
for ($j = 1; $j -le $totalRequests; $j++) {
try {
$response = Invoke-WebRequest -Uri $targetUrl -Method GET -TimeoutSec 3 -UseBasicParsing
if ($response.StatusCode -eq 200) {
$success++
}
} catch {
$failed++
}
# 每 500 个请求短暂休息,避免完全耗尽资源
if ($j % 500 -eq 0) {
Start-Sleep -Milliseconds 50
}
}
return @{
Success = $success
Failed = $failed
}
} -ArgumentList $url, $requestPerJob
$jobs += $job
if ($i % 10 -eq 0) {
Write-Host " Started $i/$jobCount jobs..." -ForegroundColor Gray
}
} catch {
Write-Host " Failed to start job $i : $_" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "[OK] All $jobCount jobs started" -ForegroundColor Green
Write-Host ""
# 显示监控命令
Write-Host "[3/4] Monitoring Commands (run in separate windows):" -ForegroundColor Cyan
Write-Host " Window 1 - HPA Status:" -ForegroundColor Yellow
Write-Host " kubectl get hpa data-engine-hpa -w" -ForegroundColor White
Write-Host ""
Write-Host " Window 2 - Pod Status:" -ForegroundColor Yellow
Write-Host " kubectl get pods -l app=data-engine -w" -ForegroundColor White
Write-Host ""
Write-Host " Window 3 - Resource Usage:" -ForegroundColor Yellow
Write-Host " while(`$true) { Clear-Host; kubectl top pods -l app=data-engine; kubectl get hpa data-engine-hpa; Start-Sleep 3 }" -ForegroundColor White
Write-Host ""
# 等待用户确认
Write-Host "[4/4] Load test is running..." -ForegroundColor Green
Write-Host "Press any key to stop all jobs and see results..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
# 停止所有任务
Write-Host "Stopping all jobs..." -ForegroundColor Cyan
Get-Job -Name "HPA-*" | Stop-Job
# 收集结果
Write-Host "Collecting results..." -ForegroundColor Cyan
$results = Get-Job -Name "HPA-*" | Receive-Job -Wait
Get-Job -Name "HPA-*" | Remove-Job
# 统计
$totalSuccess = 0
$totalFailed = 0
foreach ($result in $results) {
$totalSuccess += $result.Success
$totalFailed += $result.Failed
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " TEST RESULTS" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Total Requests: $($totalSuccess + $totalFailed)" -ForegroundColor White
Write-Host "Successful: $totalSuccess" -ForegroundColor Green
Write-Host "Failed: $totalFailed" -ForegroundColor $(if ($totalFailed -gt 0) { "Red" } else { "Green" })
Write-Host ""
# 显示最终状态
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " FINAL STATUS" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "[HPA Status]" -ForegroundColor Yellow
kubectl get hpa data-engine-hpa
Write-Host ""
Write-Host "[Pod List]" -ForegroundColor Yellow
kubectl get pods -l app=data-engine
Write-Host ""
Write-Host "[Resource Usage]" -ForegroundColor Yellow
kubectl top pods -l app=data-engine 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Host " Metrics not available" -ForegroundColor Gray
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Test completed!" -ForegroundColor Green