Complete Lab 7 security and PKI track

This commit is contained in:
2026-12-05 23:43:41 +03:00
parent 20afc2c790
commit d8ecd2aad9
93 changed files with 6714 additions and 133 deletions
+33 -11
View File
@@ -1,9 +1,11 @@
#!/usr/bin/env python3
"""Small closed-loop HTTP write benchmark; not an open-loop capacity proof."""
from concurrent.futures import ThreadPoolExecutor
from collections import Counter
import json
import math
from pathlib import Path
import re
import threading
import time
from client import Client, Failure, LAB, arguments, check, run
@@ -19,32 +21,52 @@ def main():
check(1<=args.concurrency<=128,'concurrency must be 1..128')
c = Client(args.base_url,args.ca_file)
alice,bob,eve,chat = c.bootstrap()
barrier = threading.Barrier(args.concurrency)
benchmark_started = [None]
barrier = threading.Barrier(
args.concurrency,
action=lambda: benchmark_started.__setitem__(0, time.monotonic()),
)
def worker(worker_id):
local = Client(args.base_url,args.ca_file,timeout=5)
barrier.wait()
deadline = time.monotonic()+args.duration
latencies,errors = [],0
deadline = benchmark_started[0]+args.duration
latencies, failure_offsets, failure_categories = [], [], []
index = 0
while time.monotonic()<deadline:
start = time.monotonic()
try:
local.message(alice,chat,'load-%s-%s' % (worker_id,index))
latencies.append(time.monotonic()-start)
except Failure:
errors += 1
except Failure as exc:
failure_offsets.append(time.monotonic()-benchmark_started[0])
message = str(exc)
status = re.search(r'received (\d{3})$', message)
if status:
failure_categories.append('http_' + status.group(1))
elif 'connection/TLS failure' in message:
failure_categories.append('connection_tls')
else:
failure_categories.append('other')
index += 1
return latencies,errors
started = time.monotonic()
return latencies, failure_offsets, failure_categories
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
results = list(pool.map(worker,range(args.concurrency)))
elapsed = time.monotonic()-started
latencies = sorted(x for values,_ in results for x in values)
errors = sum(n for _,n in results)
elapsed = time.monotonic()-benchmark_started[0]
latencies = sorted(x for values,_,_ in results for x in values)
failure_offsets = sorted(x for _,offsets,_ in results for x in offsets)
failure_categories = Counter(category for _,_,categories in results for category in categories)
errors = len(failure_offsets)
total = len(latencies)+errors
def percentile(p):
return round(1000*latencies[max(0,math.ceil(p*len(latencies))-1)],3) if latencies else None
report = {'lab':LAB,'workload':'closed-loop POST messages; no automatic retries','requested_duration_seconds':args.duration,'elapsed_seconds':round(elapsed,3),'concurrency':args.concurrency,'successful_requests':len(latencies),'failed_requests':errors,'error_ratio':round(errors/total,6) if total else None,'successful_rps':round(len(latencies)/elapsed,3),'successful_latency_ms':{'p50':percentile(.50),'p95':percentile(.95),'p99':percentile(.99)},'note':'Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.'}
failure_window = None
if failure_offsets:
failure_window = {
'first_failure_seconds': round(failure_offsets[0], 3),
'last_failure_seconds': round(failure_offsets[-1], 3),
'span_seconds': round(failure_offsets[-1] - failure_offsets[0], 3),
}
report = {'lab':LAB,'workload':'closed-loop POST messages; no automatic retries','requested_duration_seconds':args.duration,'elapsed_seconds':round(elapsed,3),'concurrency':args.concurrency,'successful_requests':len(latencies),'failed_requests':errors,'error_ratio':round(errors/total,6) if total else None,'failure_categories':dict(sorted(failure_categories.items())),'failure_window_seconds':failure_window,'successful_rps':round(len(latencies)/elapsed,3),'successful_latency_ms':{'p50':percentile(.50),'p95':percentile(.95),'p99':percentile(.99)},'note':'Latency percentiles cover successes only. Failure offsets/categories help locate interruption windows but are not a maximum-capacity estimate. Closed-loop has coordinated omission and client overhead.'}
text = json.dumps(report,ensure_ascii=False,indent=2)
if args.output:
dest = Path(args.output)