Files
mtusi2026lab6/scripts/lab6_load.py
T
2026-11-05 20:33:41 +03:00

170 lines
6.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""Open-loop lab6 load: fixed arrival rate, messages and attachment uploads.
Unlike tests/load.py this schedules arrivals instead of running a closed loop,
so a degraded server cannot reduce the offered rate. It is intentionally small
and standard-library only; use k6/wrk2 for capacity claims.
"""
import argparse
import base64
import os
import json
import math
import sys
import threading
import time
import urllib.request
import uuid
from pathlib import Path
# The lab tools always target loopback; ignore ambient corporate proxies.
for _proxy_key in ('HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy'):
os.environ.pop(_proxy_key, None)
os.environ['NO_PROXY'] = 'localhost,127.0.0.1,::1'
os.environ['no_proxy'] = 'localhost,127.0.0.1,::1'
urllib.request.getproxies = lambda: {}
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
def percentile(values, p):
if not values:
return None
values = sorted(values)
rank = max(0, min(len(values) - 1, int(math.ceil(p / 100.0 * len(values))) - 1))
return values[rank]
def main():
parser = argparse.ArgumentParser(description='Open-loop lab6 load')
parser.add_argument('--base-url', default='http://localhost:8080')
parser.add_argument('--rate', type=float, default=1.0, help='arrivals per second')
parser.add_argument('--duration', type=float, default=60.0, help='seconds')
parser.add_argument('--upload-ratio', type=float, default=0.25)
parser.add_argument('--deadline', type=float, default=10.0)
parser.add_argument('--workers', type=int, default=32)
parser.add_argument('--output', default=None)
parser.add_argument('--token-refresh-seconds', type=float, default=3000.0)
args = parser.parse_args()
from client import Client, Failure
bootstrap = Client(args.base_url, timeout=args.deadline)
alice, _, _, chat = bootstrap.bootstrap()
lock = threading.Lock()
token_lock = threading.Lock()
last_refresh = [time.monotonic()]
def refresh_session():
# Sessions live one hour; refresh well before expiry so a 70m warmup
# does not turn into 401s halfway through.
if time.monotonic() - last_refresh[0] < args.token_refresh_seconds:
return
with token_lock:
if time.monotonic() - last_refresh[0] < args.token_refresh_seconds:
return
registration = alice['registration']
credential = base64.b64encode(
(registration['username'] + ':' + registration['password']).encode()).decode()
refresher = Client(args.base_url, timeout=args.deadline)
session = refresher.call(
'POST', '/api/v1/auth/sessions', 201,
headers={'Authorization': 'Basic ' + credential}, schema='Session')['json']
alice['token'] = session['token']
last_refresh[0] = time.monotonic()
latencies, ttr, failures = [], [], []
counters = {'accepted': 0, 'ready': 0, 'failed': 0, 'messages': 0, 'uploads': 0, 'dropped': 0}
sem = threading.Semaphore(args.workers)
def record(kind, elapsed, ok, detail=''):
with lock:
if kind == 'message':
counters['messages'] += 1
if ok:
latencies.append(elapsed)
else:
counters['failed'] += 1
failures.append(detail)
else:
if ok:
counters['ready'] += 1
ttr.append(elapsed)
else:
counters['failed'] += 1
failures.append(detail)
def run_one(kind, index):
client = Client(args.base_url, timeout=args.deadline)
started = time.monotonic()
try:
refresh_session()
if kind == 'message':
client.message(alice, chat, 'lab6 load %d %s' % (index, uuid.uuid4().hex[:8]))
record(kind, time.monotonic() - started, True)
else:
att, _ = client.attachment(alice, chat)
with lock:
counters['uploads'] += 1
counters['accepted'] += 1
try:
client.wait_ready(alice, att, wait_seconds=args.deadline)
record(kind, time.monotonic() - started, True)
except Exception as exc:
with lock:
counters['failed'] += 1
failures.append('ready: ' + type(exc).__name__ + ': ' + str(exc)[:120])
except Exception as exc:
record(kind, time.monotonic() - started, False, type(exc).__name__ + ': ' + str(exc)[:120])
finally:
sem.release()
total = max(0, int(args.rate * args.duration))
started = time.monotonic()
threads = []
for index in range(total):
target = started + index / max(args.rate, 0.001)
delay = target - time.monotonic()
if delay > 0:
time.sleep(delay)
if not sem.acquire(blocking=False):
with lock:
counters['dropped'] += 1
continue
kind = 'upload' if ((index * 37) % 100) < int(args.upload_ratio * 100) else 'message'
thread = threading.Thread(target=run_one, args=(kind, index), daemon=True)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
summary = {
'base_url': args.base_url,
'rate': args.rate,
'duration_s': round(time.monotonic() - started, 2),
'scheduled': total,
'counters': counters,
'failures': len(failures),
'failure_samples': failures[:10],
'latency_ms': {
'p50': round(percentile(latencies, 50) * 1000, 1) if latencies else None,
'p95': round(percentile(latencies, 95) * 1000, 1) if latencies else None,
'p99': round(percentile(latencies, 99) * 1000, 1) if latencies else None,
},
'ttr_ms': {
'p50': round(percentile(ttr, 50) * 1000, 1) if ttr else None,
'p95': round(percentile(ttr, 95) * 1000, 1) if ttr else None,
'p99': round(percentile(ttr, 99) * 1000, 1) if ttr else None,
},
}
if args.output:
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
Path(args.output).write_text(json.dumps(summary, indent=2) + '\n')
print(json.dumps(summary, indent=2))
return 0 if not failures else 1
if __name__ == '__main__':
raise SystemExit(main())