Files
mtusi2026lab7/app/tools/benchmark_argon2.py
T

31 lines
1009 B
Python

"""Compare Argon2id verification latency in the API container (synthetic input)."""
import json
from statistics import median, quantiles
from time import perf_counter
from argon2 import PasswordHasher, Type
def main() -> None:
password = 'laboratory-benchmark-synthetic-password'
for memory in (19456, 32768, 65536):
hasher = PasswordHasher(time_cost=2, memory_cost=memory, parallelism=1, type=Type.ID)
stored = hasher.hash(password)
for _ in range(3):
hasher.verify(stored, password)
samples = []
for _ in range(40):
started = perf_counter()
hasher.verify(stored, password)
samples.append((perf_counter() - started) * 1000)
print(json.dumps({
'memory_kib': memory, 'iterations': 2, 'parallelism': 1,
'samples': len(samples), 'p50_ms': round(median(samples), 2),
'p95_ms': round(quantiles(samples, n=20)[18], 2),
}))
if __name__ == '__main__':
main()