forked from mtusi2026/mtusi2026lab3
100 lines
4.7 KiB
Python
100 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Independent persistent HTTP connection per worker; no shared HTTP client pool."""
|
|
import argparse
|
|
from collections import Counter
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import http.client
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import time
|
|
from urllib.parse import urlsplit
|
|
import uuid
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
from client import Client
|
|
|
|
|
|
def percentile(values, p):
|
|
values = sorted(values)
|
|
return round(values[max(0, math.ceil(len(values) * p) - 1)], 3) if values else None
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--base-url', default=os.getenv('BASE_URL', 'http://localhost:8080'))
|
|
parser.add_argument('--duration', type=float, default=20)
|
|
parser.add_argument('--warmup', type=float, default=5)
|
|
parser.add_argument('--concurrency', type=int, default=32)
|
|
parser.add_argument('--output', required=True)
|
|
args = parser.parse_args()
|
|
url = urlsplit(args.base_url)
|
|
if url.scheme != 'http' or not url.hostname:
|
|
parser.error('This local lab3 generator requires an HTTP base URL')
|
|
if not 1 <= args.concurrency <= 256 or not 1 <= args.duration <= 3600 or args.warmup < 0:
|
|
parser.error('invalid load parameters')
|
|
alice, _, _, chat = Client(args.base_url).bootstrap()
|
|
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
|
measured_start = time.monotonic() + args.warmup
|
|
deadline = measured_start + args.duration
|
|
|
|
def worker(worker_id):
|
|
conn = http.client.HTTPConnection(url.hostname, url.port or 80, timeout=5)
|
|
samples = []
|
|
index = 0
|
|
try:
|
|
while time.monotonic() < deadline:
|
|
data = json.dumps({'text': 'load-%04d-%08d' % (worker_id, index)}, separators=(',', ':')).encode()
|
|
headers = {'Authorization': 'Bearer ' + alice['token'],
|
|
'Content-Type': 'application/json', 'Idempotency-Key': uuid.uuid4().hex}
|
|
before = time.monotonic()
|
|
try:
|
|
conn.request('POST', path, body=data, headers=headers)
|
|
response = conn.getresponse()
|
|
response.read()
|
|
status = response.status
|
|
instance = response.getheader('X-Instance-Id', '')
|
|
except (OSError, http.client.HTTPException):
|
|
status, instance = 0, ''
|
|
conn.close()
|
|
after = time.monotonic()
|
|
if before >= measured_start:
|
|
samples.append({'started_s': round(before - measured_start, 6),
|
|
'latency_ms': round(1000 * (after - before), 3), 'status': status, 'instance': instance})
|
|
index += 1
|
|
finally:
|
|
conn.close()
|
|
return samples
|
|
|
|
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
|
batches = list(pool.map(worker, range(args.concurrency)))
|
|
elapsed = time.monotonic() - measured_start
|
|
samples = sorted((s for batch in batches for s in batch), key=lambda s: s['started_s'])
|
|
success = [s['latency_ms'] for s in samples if s['status'] == 201]
|
|
report = {'generator': 'stdlib http.client; independent persistent connection per thread',
|
|
'workload': 'closed-loop POST messages; unique keys, persistent connections, no retries',
|
|
'duration_seconds': args.duration, 'warmup_seconds': args.warmup,
|
|
'elapsed_seconds': round(elapsed, 3), 'concurrency': args.concurrency,
|
|
'successful_requests': len(success), 'failed_requests': len(samples) - len(success),
|
|
'successful_rps': round(len(success) / elapsed, 3),
|
|
'error_ratio': round((len(samples) - len(success)) / len(samples), 6) if samples else None,
|
|
'successful_latency_ms': {label: percentile(success, p) for label, p in [('p50', .5), ('p95', .95), ('p99', .99)]},
|
|
'status_counts': dict(Counter(s['status'] for s in samples)),
|
|
'instance_counts': dict(Counter(s['instance'] for s in samples)),
|
|
'dataset': {'initial_messages_in_new_chat': 0, 'measured_attempts': len(samples), 'text_template_codepoints': 18},
|
|
'limits': 'Success-only latency; closed-loop coordinated omission. Client/host can bottleneck. '
|
|
'Warmup inserts remain in history, excluded from measured samples. HTTP acknowledgements, not COMMIT count.'}
|
|
dest = Path(args.output)
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
dest.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
|
|
dest.with_suffix('.jsonl').write_text(''.join(json.dumps(s) + '\n' for s in samples), encoding='utf-8')
|
|
print(json.dumps(report, indent=2))
|
|
if not success:
|
|
raise SystemExit('No successful writes')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|