Files
mtusi2026lab2/scripts/track2-experiment.ps1

81 lines
3.1 KiB
PowerShell

$ErrorActionPreference = "Stop"
$repo = Split-Path -Parent $PSScriptRoot
Set-Location $repo
if (-not (Test-Path ".env")) {
throw "Copy .env.example to .env and set POSTGRES_PASSWORD first."
}
$settings = @{}
Get-Content ".env" | ForEach-Object {
if ($_ -match "^\s*([^#=]+)=(.*)$") {
$settings[$matches[1].Trim()] = $matches[2].Trim()
}
}
if (-not $settings["POSTGRES_PASSWORD"]) {
throw "POSTGRES_PASSWORD is missing from .env."
}
$evidence = Join-Path $repo "evidence"
New-Item -ItemType Directory -Force -Path $evidence | Out-Null
$started = Get-Date
docker compose up -d --build
if ($LASTEXITCODE -ne 0) { throw "Compose startup failed" }
docker compose exec -T api python /app/scripts/track2_experiment.py |
Tee-Object -FilePath (Join-Path $evidence "track2-experiment.json")
if ($LASTEXITCODE -ne 0) { throw "Index experiment failed" }
docker compose --profile track2-restore run --rm backup-volume-init
if ($LASTEXITCODE -ne 0) { throw "Could not prepare backup volume" }
$restoreStarted = Get-Date
docker compose exec -T postgres pg_dump -Fc -U messenger -d messenger `
-n track2_lab -f /backups/track2_lab.dump
if ($LASTEXITCODE -ne 0) { throw "PostgreSQL backup failed" }
docker compose --profile track2-restore up -d --force-recreate postgres-restore
if ($LASTEXITCODE -ne 0) { throw "Restore PostgreSQL startup failed" }
$healthy = $false
for ($i = 0; $i -lt 60; $i++) {
$health = docker inspect --format '{{.State.Health.Status}}' mtusi-lab2-postgres-restore 2>$null
if ($health -eq "healthy") {
$healthy = $true
break
}
Start-Sleep -Seconds 2
}
if (-not $healthy) { throw "Restore PostgreSQL did not become healthy" }
docker compose exec -T postgres-restore pg_restore --clean --if-exists `
-U messenger -d messenger /backups/track2_lab.dump
if ($LASTEXITCODE -ne 0) { throw "PostgreSQL restore failed" }
$database = $settings["POSTGRES_DB"]
if (-not $database) { $database = "messenger" }
$username = $settings["POSTGRES_USER"]
if (-not $username) { $username = "messenger" }
$restoreUrl = "postgresql://{0}:{1}@postgres-restore:5432/{2}" -f $username, $settings["POSTGRES_PASSWORD"], $database
docker compose exec -T -e "DATABASE_URL=$restoreUrl" api `
python /app/scripts/track2_verify.py |
Tee-Object -FilePath (Join-Path $evidence "track2-restore.json")
if ($LASTEXITCODE -ne 0) { throw "Restored data verification failed" }
$restoreSeconds = [math]::Round(((Get-Date) - $restoreStarted).TotalSeconds, 2)
$duration = [math]::Round(((Get-Date) - $started).TotalSeconds, 2)
@"
Track 2 local Docker experiment
Started: $($started.ToString("o"))
Duration seconds (including container startup): $duration
Backup + separate database startup + restore + verification seconds: $restoreSeconds
Benchmark data: 100,000 synthetic rows per index variant, skewed chat distribution
Backup: PostgreSQL custom-format dump to named volume; never committed to Git
Restore target: separate PostgreSQL container and separate named volume
Results: see track2-experiment.json and track2-restore.json
"@ | Set-Content -Encoding utf8 (Join-Path $evidence "track2-run.txt")
docker compose ps