109 lines
4.6 KiB
Python
109 lines
4.6 KiB
Python
"""Persistent HTTP closed-loop benchmark, executed INSIDE the Compose network.
|
|
|
|
No retries: measures HTTP confirmations, with separate error/timeout counts.
|
|
Warmup is excluded. Individual latencies are retained, including failed requests.
|
|
"""
|
|
import argparse
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import http.client
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import threading
|
|
import time
|
|
import uuid
|
|
from urllib.parse import urlsplit
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
from client import Client
|
|
|
|
|
|
def benchmark(base, concurrency, duration, warmup):
|
|
c = Client(base)
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
endpoint = '/api/v1/chats/' + chat['id'] + '/messages'
|
|
parsed = urlsplit(base)
|
|
barrier = threading.Barrier(concurrency + 1)
|
|
run_id = uuid.uuid4().hex
|
|
timing = {}
|
|
|
|
def worker(index):
|
|
conn = http.client.HTTPConnection(parsed.hostname, parsed.port or 80, timeout=5)
|
|
samples = []
|
|
observed = set()
|
|
counter = 0
|
|
barrier.wait()
|
|
while time.monotonic() < timing['end']:
|
|
start = time.monotonic()
|
|
measured = start >= timing['measure']
|
|
key = f'{run_id}-{index}-{counter}'
|
|
text = 'bench-' + key
|
|
status = 0
|
|
error = None
|
|
try:
|
|
conn.request('POST', endpoint, json.dumps({'text': text}), {
|
|
'Authorization': 'Bearer ' + alice['token'],
|
|
'Content-Type': 'application/json', 'Idempotency-Key': key})
|
|
response = conn.getresponse()
|
|
status = response.status
|
|
payload = response.read()
|
|
if status == 201:
|
|
data = json.loads(payload)
|
|
if data['text'] != text or data['chat_id'] != chat['id']:
|
|
error = 'invalid_payload'
|
|
if measured:
|
|
observed.add(response.getheader('X-Instance-Id'))
|
|
else:
|
|
error = 'http_' + str(status)
|
|
except (OSError, http.client.HTTPException, ValueError, KeyError) as exc:
|
|
error = type(exc).__name__
|
|
conn.close()
|
|
conn = http.client.HTTPConnection(parsed.hostname, parsed.port or 80, timeout=5)
|
|
elapsed = 1000 * (time.monotonic() - start)
|
|
if measured:
|
|
samples.append([round(elapsed, 3), status, error])
|
|
counter += 1
|
|
conn.close()
|
|
return samples, observed
|
|
|
|
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
|
futures = [pool.submit(worker, i) for i in range(concurrency)]
|
|
begin = time.monotonic()
|
|
timing.update(measure=begin + warmup, end=begin + warmup + duration)
|
|
barrier.wait()
|
|
results = [f.result() for f in futures]
|
|
end = time.monotonic()
|
|
samples = [s for values, _ in results for s in values]
|
|
successful = sorted(s[0] for s in samples if s[1] == 201 and s[2] is None)
|
|
errors = {}
|
|
for _, _, error in samples:
|
|
if error:
|
|
errors[error] = errors.get(error, 0) + 1
|
|
def percentile(p):
|
|
return successful[max(0, math.ceil(p * len(successful))-1)] if successful else None
|
|
return {'workload': 'closed-loop persistent POST via LB, unique keys, no retries',
|
|
'concurrency': concurrency, 'warmup_seconds': warmup,
|
|
'requested_duration_seconds': duration,
|
|
'measure_started_unix': time.time() - (time.monotonic() - timing['measure']),
|
|
'finished_unix': time.time(),
|
|
'elapsed_seconds': round(end - timing['measure'], 3),
|
|
'successful_requests': len(successful), 'errors': errors,
|
|
'successful_rps': round(len(successful) / (end-timing['measure']), 3),
|
|
'successful_latency_ms': {f'p{p}': percentile(p/100) for p in (50,95,99)},
|
|
'observed_instances': sorted({i for _, ids in results for i in ids if i}),
|
|
'samples_ms_status_error': samples}
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--base-url', default=os.getenv('BASE_URL', 'http://localhost:8083'))
|
|
parser.add_argument('--concurrency', type=int, default=16)
|
|
parser.add_argument('--duration', type=float, default=12)
|
|
parser.add_argument('--warmup', type=float, default=3)
|
|
args = parser.parse_args()
|
|
if not 1 <= args.concurrency <= 128 or not 1 <= args.duration <= 3600 or not 0 <= args.warmup <= 60:
|
|
parser.error('invalid load parameters')
|
|
print(json.dumps(benchmark(args.base_url, args.concurrency, args.duration, args.warmup)))
|