Files
mtusi2026lab7/scripts/sample_docker_resources.ps1
T
ferraronp ca7515601a
Skeleton contracts / contracts (pull_request) Blocked by required conditions
feat: реализовать ЛР7 — TLS, mTLS, OpenBao и hardening
2026-11-26 19:50:38 +03:00

63 lines
2.8 KiB
PowerShell

# Sample observed Docker container memory/CPU during a Lab5 workload.
# Usage (another terminal, while attachments_concurrent.py runs):
# powershell -ExecutionPolicy Bypass -File scripts/sample_docker_resources.ps1 -DurationSeconds 45
param(
[ValidateRange(5, 3600)][int]$DurationSeconds = 45,
[ValidateRange(250, 10000)][int]$IntervalMs = 1000,
[string]$Output = 'evidence/worker-resources.csv'
)
$ErrorActionPreference = 'Stop'
$composeArgs = @('compose', '--env-file', '.env')
$ids = @( & docker @composeArgs ps -q worker api1 api2 | Where-Object { $_ -and $_.Trim() } )
if ($LASTEXITCODE -ne 0 -or $ids.Count -ne 3) {
throw 'Expected exactly 3 running containers: worker, api1, api2'
}
function Convert-ToMiB([string]$usage) {
$first = ($usage -split ' / ')[0].Trim()
if ($first -notmatch '^([0-9]+(?:\.[0-9]+)?)\s*(B|KiB|MiB|GiB|TiB|kB|MB|GB|TB)$') {
throw "Unrecognized docker stats memory format: $first"
}
$amount = [double]::Parse($Matches[1], [Globalization.CultureInfo]::InvariantCulture)
$factor = switch -CaseSensitive ($Matches[2]) {
'B' { 1 / 1048576.0 }
'KiB' { 1 / 1024.0 }
'MiB' { 1.0 }
'GiB' { 1024.0 }
'TiB' { 1048576.0 }
'kB' { 1000.0 / 1048576.0 }
'MB' { 1000000.0 / 1048576.0 }
'GB' { 1000000000.0 / 1048576.0 }
'TB' { 1000000000000.0 / 1048576.0 }
}
return [math]::Round($amount * $factor, 3)
}
$rows = [System.Collections.Generic.List[object]]::new()
$until = (Get-Date).AddSeconds($DurationSeconds)
Write-Host "Sampling worker/api1/api2 until $($until.ToString('HH:mm:ss'))" -ForegroundColor Cyan
while ((Get-Date) -lt $until) {
$raw = @( & docker stats --no-stream --format '{{json .}}' @ids )
if ($LASTEXITCODE -ne 0) { throw 'docker stats failed' }
foreach ($line in $raw) {
if ([string]::IsNullOrWhiteSpace($line)) { continue }
$item = $line | ConvertFrom-Json
$rows.Add([PSCustomObject]@{
time = (Get-Date).ToUniversalTime().ToString('o')
container = $item.Name
memory_mib = Convert-ToMiB $item.MemUsage
cpu_percent = [double]::Parse($item.CPUPerc.TrimEnd('%'), [Globalization.CultureInfo]::InvariantCulture)
})
}
Start-Sleep -Milliseconds $IntervalMs
}
if ($rows.Count -eq 0) { throw 'No Docker stats samples collected' }
$folder = Split-Path -Parent $Output
if ($folder -and !(Test-Path $folder)) { New-Item -ItemType Directory -Path $folder -Force | Out-Null }
$rows | Export-Csv -Path $Output -NoTypeInformation -Encoding UTF8
$rows | Group-Object container | ForEach-Object {
$peak = ($_.Group | Measure-Object -Property memory_mib -Maximum).Maximum
"{0}: observed max container memory {1:N2} MiB, {2} samples" -f $_.Name, $peak, $_.Count
}
Write-Host "Saved $Output. Observed sampled memory, NOT true peak RSS." -ForegroundColor Green