Files
mtusi2026lab7/scripts/security_benchmark.py
T

43 lines
1.2 KiB
Python

import concurrent.futures
import json
import resource
import secrets
import statistics
import time
from argon2 import PasswordHasher
hasher = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
password = secrets.token_urlsafe(24)
encoded = hasher.hash(password)
def measure(_):
start = time.perf_counter()
hasher.verify(encoded, password)
return (time.perf_counter() - start) * 1000
results = []
for concurrency in [1, 2, 4]:
start = time.perf_counter()
cpu = time.process_time()
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as pool:
samples = sorted(pool.map(measure, range(100)))
results.append(
{
"concurrency": concurrency,
"samples": len(samples),
"median_ms": round(statistics.median(samples), 3),
"p95_ms": round(samples[94], 3),
"cpu_ms_per_verify": round((time.process_time() - cpu) * 10, 3),
"wall_seconds": round(time.perf_counter() - start, 3),
"peak_rss_kib": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
}
)
print(
json.dumps(
{"memory_kib": 19456, "iterations": 2, "parallelism": 1, "results": results}, indent=2
)
)