Files
mtusi2026lab7/student-tests/argon2_cost.py
T
2026-11-11 13:53:51 +03:00

37 lines
1.1 KiB
Python

"""Measure CPU/wall time and peak RSS inside the API cgroup without user data."""
from concurrent.futures import ThreadPoolExecutor
import json
import resource
import secrets
import statistics
import time
from argon2 import PasswordHasher
hasher = PasswordHasher(memory_cost=19456, time_cost=2, parallelism=1)
password = secrets.token_urlsafe(32)
hashed = hasher.hash(password)
def verify(_):
start = time.perf_counter()
assert hasher.verify(hashed, password)
return 1000 * (time.perf_counter() - start)
def sample(concurrency):
start, cpu = time.perf_counter(), time.process_time()
with ThreadPoolExecutor(max_workers=concurrency) as pool:
samples = sorted(pool.map(verify, range(50)))
print(json.dumps({
"checks": len(samples), "concurrency": concurrency, "p50_ms": round(statistics.median(samples), 3),
"p95_ms": round(samples[47], 3), "cpu_seconds": round(time.process_time() - cpu, 3),
"wall_seconds": round(time.perf_counter() - start, 3),
"peak_rss_mib": round(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, 3),
}))
sample(1)
sample(2)