Files
mtusi2026lab3/scripts/test-lb-exclusion.ps1
2026-10-17 08:59:12 +03:00

52 lines
3.1 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
$ErrorActionPreference = 'Stop'
$logDir = "evidence\lab3"
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
# 1. Создаем тестового юзера (на случай чистого volume)
curl.exe -s -o NUL -X POST http://127.0.0.1:8080/api/v1/users -H "Content-Type: application/json" -d '{\"username\":\"lbtest\",\"display_name\":\"L\",\"password\":\"validpassword123\"}'
# 2. Получаем токен и создаем чат
$TOKEN = (curl.exe -s -X POST http://127.0.0.1:8080/api/v1/auth/sessions -u "lbtest:validpassword123" | ConvertFrom-Json).token
$USER_ID = (curl.exe -s http://127.0.0.1:8080/api/v1/users/me -H "Authorization: Bearer $TOKEN" | ConvertFrom-Json).id
$CHAT_ID = (curl.exe -s -X POST http://127.0.0.1:8080/api/v1/chats -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d ('{\"title\":\"lb-test\",\"member_ids\":[\"' + $USER_ID + '\"]}') | ConvertFrom-Json).id
$out = "$logDir\lb-exclusion-pause-test.log"
"=== LB exclusion test (docker pause) at $(Get-Date -Format o) ===" | Out-File $out
"CHAT_ID=$CHAT_ID" | Add-Content $out
# 3. Замеры ДО паузы
"`n--- ДО ПАУЗЫ (трафик идет на обе реплики) ---" | Add-Content $out
for ($i = 1; $i -le 4; $i++) {
$r = curl.exe -s -o NUL -w "HTTP=%{http_code} time=%{time_total}s" -X POST "http://127.0.0.1:8080/api/v1/chats/$CHAT_ID/messages" -H "Authorization: Bearer $TOKEN" -H "Idempotency-Key: pre-$i-$(New-Guid)" -H "Content-Type: application/json" -d '{\"text\":\"pre\"}'
Write-Host "Pre: $r"
"${i}: ${r}" | Add-Content $out
}
# 4. Замораживаем api-1
"`n---> docker pause lab3-api-1 (имитация зависшего пула)" | Add-Content $out
docker pause lab3-api-1
Write-Host "api-1 PAUSED"
# 5. Замеры ВО ВРЕМЯ паузы (проверяем non_idempotent)
"`n--- ВО ВРЕМЯ ПАУЗЫ (Nginx должен перекинуть на api-2) ---" | Add-Content $out
for ($i = 1; $i -le 6; $i++) {
# Ставим таймаут curl 15s, чтобы он не оборвался раньше Nginx (у nginx read_timeout 10s)
$r = curl.exe -s -o NUL -w "HTTP=%{http_code} time=%{time_total}s" -m 15 -X POST "http://127.0.0.1:8080/api/v1/chats/$CHAT_ID/messages" -H "Authorization: Bearer $TOKEN" -H "Idempotency-Key: during-$i-$(New-Guid)" -H "Content-Type: application/json" -d '{\"text\":\"during\"}'
Write-Host "During: $r"
"${i}: ${r}" | Add-Content $out
}
# 6. Размораживаем api-1
"`n---> docker unpause lab3-api-1" | Add-Content $out
docker unpause lab3-api-1
Write-Host "api-1 UNPAUSED"
# 7. Замеры ПОСЛЕ паузы
"`n--- ПОСЛЕ ПАУЗЫ ---" | Add-Content $out
for ($i = 1; $i -le 4; $i++) {
$r = curl.exe -s -o NUL -w "HTTP=%{http_code} time=%{time_total}s" -X POST "http://127.0.0.1:8080/api/v1/chats/$CHAT_ID/messages" -H "Authorization: Bearer $TOKEN" -H "Idempotency-Key: post-$i-$(New-Guid)" -H "Content-Type: application/json" -d '{\"text\":\"post\"}'
Write-Host "Post: $r"
"${i}: ${r}" | Add-Content $out
}
Write-Host "`n✅ Готово! Лог сохранен в $out"