This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import resource
|
||||
import statistics
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from src import security
|
||||
|
||||
|
||||
def percentile(values, fraction):
|
||||
ordered = sorted(values)
|
||||
return ordered[max(0, math.ceil(len(ordered) * fraction) - 1)]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Measure Argon2id latency and bounded concurrency.")
|
||||
parser.add_argument("--samples", type=int, default=20)
|
||||
parser.add_argument("--concurrency", type=int, default=12)
|
||||
args = parser.parse_args()
|
||||
if args.samples < 1 or args.concurrency < 1:
|
||||
parser.error("samples and concurrency must be positive")
|
||||
|
||||
encoded = security.hash_password("argon2-budget-synthetic-password")
|
||||
durations = []
|
||||
for _ in range(args.samples):
|
||||
started = time.perf_counter()
|
||||
if not security.verify_password("argon2-budget-synthetic-password", encoded):
|
||||
raise RuntimeError("Argon2id self-verification failed")
|
||||
durations.append(time.perf_counter() - started)
|
||||
|
||||
active = 0
|
||||
peak_active = 0
|
||||
lock = threading.Lock()
|
||||
original_slots = security._PASSWORD_HASH_SLOTS
|
||||
|
||||
class TrackingSlots:
|
||||
def acquire(self, timeout=None):
|
||||
nonlocal active, peak_active
|
||||
acquired = original_slots.acquire(timeout=timeout)
|
||||
if acquired:
|
||||
with lock:
|
||||
active += 1
|
||||
peak_active = max(peak_active, active)
|
||||
return acquired
|
||||
|
||||
def release(self):
|
||||
nonlocal active
|
||||
with lock:
|
||||
active -= 1
|
||||
original_slots.release()
|
||||
|
||||
security._PASSWORD_HASH_SLOTS = TrackingSlots()
|
||||
|
||||
def verify_once(_):
|
||||
try:
|
||||
return security.verify_password("argon2-budget-synthetic-password", encoded)
|
||||
except security.PasswordHashCapacityError:
|
||||
return False
|
||||
|
||||
with ThreadPoolExecutor(max_workers=args.concurrency) as executor:
|
||||
burst_results = list(executor.map(verify_once, range(args.concurrency)))
|
||||
|
||||
print(json.dumps({
|
||||
"argon2_memory_kib": security._ARGON2_MEMORY_KIB,
|
||||
"argon2_iterations": security._ARGON2_ITERATIONS,
|
||||
"argon2_parallelism": security._ARGON2_PARALLELISM,
|
||||
"hash_slots_per_process": security._PASSWORD_HASH_CONCURRENCY,
|
||||
"hash_wait_seconds": security._PASSWORD_HASH_WAIT_SECONDS,
|
||||
"sequential_samples": args.samples,
|
||||
"verify_latency_ms": {
|
||||
"p50": round(statistics.median(durations) * 1000, 3),
|
||||
"p95": round(percentile(durations, 0.95) * 1000, 3),
|
||||
"max": round(max(durations) * 1000, 3),
|
||||
},
|
||||
"burst_requests": args.concurrency,
|
||||
"burst_successes": sum(burst_results),
|
||||
"burst_capacity_rejections": args.concurrency - sum(burst_results),
|
||||
"peak_active_hashes": peak_active,
|
||||
"max_rss_platform_units": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
|
||||
"platform": sys.platform,
|
||||
}, sort_keys=True))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user