Files
mtusi2026lab6/scripts/lab6-traffic.py
T

262 lines
9.7 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import base64
from concurrent.futures import ThreadPoolExecutor
import json
import math
from pathlib import Path
import secrets
import struct
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
import zlib
MAX_RESPONSE_BYTES = 1 << 20
def png_fixture(width=96, height=64):
rows = []
for y in range(height):
pixels = bytearray()
for x in range(width):
pixels.extend(((x * 3) % 256, (y * 5) % 256, (x + y) % 256))
rows.append(b"\x00" + bytes(pixels))
raw = b"".join(rows)
def chunk(name, payload):
return struct.pack(">I", len(payload)) + name + payload + struct.pack(">I", zlib.crc32(name + payload) & 0xFFFFFFFF)
header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(raw, 6)) + chunk(b"IEND", b"")
def request(method, url, body=None, headers=None, timeout=10):
req = urllib.request.Request(url, data=body, method=method, headers=headers or {})
try:
with urllib.request.urlopen(req, timeout=timeout) as response:
payload = response.read(MAX_RESPONSE_BYTES)
return response.status, payload
except urllib.error.HTTPError as error:
return error.code, error.read(MAX_RESPONSE_BYTES)
except urllib.error.URLError:
return 0, b""
def decode_json(payload):
return json.loads(payload.decode("utf-8"))
def json_request(method, url, value=None, token=None, headers=None):
request_headers = {"Accept": "application/json"}
body = None
if value is not None:
body = json.dumps(value, separators=(",", ":")).encode("utf-8")
request_headers["Content-Type"] = "application/json"
if token:
request_headers["Authorization"] = "Bearer " + token
if headers:
request_headers.update(headers)
status, payload = request(method, url, body, request_headers)
return status, decode_json(payload) if payload else {}
def bootstrap(base_url):
suffix = secrets.token_hex(6)
password = "Lab6-" + secrets.token_urlsafe(18)
users = []
for prefix in ("telemetry_a_", "telemetry_b_"):
username = prefix + suffix
status, user = json_request("POST", base_url + "/api/v1/users", {
"username": username,
"display_name": "Synthetic telemetry load",
"password": password,
})
if status != 201:
raise RuntimeError("register user returned HTTP %d" % status)
users.append((username, user["id"]))
credentials = base64.b64encode((users[0][0] + ":" + password).encode("utf-8")).decode("ascii")
status, payload = request("POST", base_url + "/api/v1/auth/sessions", headers={
"Authorization": "Basic " + credentials,
"Accept": "application/json",
})
if status != 201:
raise RuntimeError("create session returned HTTP %d" % status)
token = decode_json(payload)["token"]
status, chat = json_request("POST", base_url + "/api/v1/chats", {
"title": "Lab 6 synthetic telemetry",
"member_ids": [users[0][1], users[1][1]],
}, token)
if status != 201:
raise RuntimeError("create chat returned HTTP %d" % status)
return token, chat["id"]
class Results:
def __init__(self):
self.lock = threading.Lock()
self.latencies = []
self.statuses = {}
self.attachment_ids = []
def record(self, status, elapsed, attachment_id=None):
with self.lock:
self.latencies.append(elapsed)
key = str(status)
self.statuses[key] = self.statuses.get(key, 0) + 1
if attachment_id:
self.attachment_ids.append(attachment_id)
def percentile(values, quantile):
if not values:
return None
ordered = sorted(values)
index = max(0, math.ceil(quantile * len(ordered)) - 1)
return round(ordered[index] * 1000, 3)
def run_request(base_url, token, chat_id, mode, sequence, fixture):
if mode == "mixed":
mode = "attachments" if sequence % 3 == 0 else "messages"
started = time.monotonic()
if mode == "messages":
status, _ = json_request("POST", base_url + "/api/v1/chats/" + chat_id + "/messages", {
"text": "synthetic telemetry event",
"attachment_ids": [],
}, token, {"Idempotency-Key": secrets.token_hex(16)})
return status, time.monotonic() - started, None
upload_url = base_url + "/api/v1/chats/" + chat_id + "/attachments?" + urllib.parse.urlencode({
"filename": "synthetic.png",
})
status, payload = request("POST", upload_url, fixture, {
"Authorization": "Bearer " + token,
"Content-Type": "image/png",
"Accept": "application/json",
})
attachment_id = None
if status == 202:
attachment_id = decode_json(payload).get("id")
return status, time.monotonic() - started, attachment_id
def wait_for_attachments(base_url, token, attachment_ids, timeout):
remaining = set(attachment_ids)
states = {"ready": 0, "failed": 0, "timeout": 0}
deadline = time.monotonic() + timeout
while remaining and time.monotonic() < deadline:
for attachment_id in list(remaining):
status, metadata = json_request("GET", base_url + "/api/v1/attachments/" + attachment_id, token=token)
if status != 200:
continue
state = metadata.get("status")
if state in ("ready", "failed"):
states[state] += 1
remaining.remove(attachment_id)
if remaining:
time.sleep(0.25)
states["timeout"] = len(remaining)
return states
def main():
parser = argparse.ArgumentParser(description="Bounded synthetic load for Lab 6 telemetry and incidents")
parser.add_argument("--base-url", default="http://127.0.0.1:8080")
parser.add_argument("--mode", choices=("messages", "attachments", "mixed"), default="mixed")
parser.add_argument("--duration", type=float, default=30)
parser.add_argument("--requests", type=int, default=0, help="Maximum requests; zero means duration only")
parser.add_argument("--concurrency", type=int, default=4)
parser.add_argument("--rate", type=float, default=0, help="Approximate aggregate request rate; zero is unthrottled")
parser.add_argument("--wait-ready", action="store_true")
parser.add_argument("--ready-timeout", type=float, default=120)
parser.add_argument("--output")
parser.add_argument("--started-file", help="Touch after synthetic session and chat are ready")
args = parser.parse_args()
if args.duration <= 0 or args.duration > 1800:
parser.error("duration must be in (0, 1800]")
if args.requests < 0:
parser.error("requests must be non-negative")
if args.concurrency < 1 or args.concurrency > 64:
parser.error("concurrency must be in [1, 64]")
if args.rate < 0 or args.rate > 1000:
parser.error("rate must be in [0, 1000]")
base_url = args.base_url.rstrip("/")
token, chat_id = bootstrap(base_url)
if args.started_file:
started_file = Path(args.started_file)
started_file.parent.mkdir(parents=True, exist_ok=True)
started_file.touch()
fixture = png_fixture()
results = Results()
next_sequence = 0
sequence_lock = threading.Lock()
deadline = time.monotonic() + args.duration
def worker():
nonlocal next_sequence
while time.monotonic() < deadline:
with sequence_lock:
if args.requests and next_sequence >= args.requests:
return
sequence = next_sequence
next_sequence += 1
status, elapsed, attachment_id = run_request(base_url, token, chat_id, args.mode, sequence, fixture)
results.record(status, elapsed, attachment_id)
if args.rate:
delay = args.concurrency / args.rate - elapsed
if delay > 0:
time.sleep(delay)
started = time.monotonic()
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
futures = [pool.submit(worker) for _ in range(args.concurrency)]
for future in futures:
future.result()
elapsed = time.monotonic() - started
attachment_states = None
if args.wait_ready and results.attachment_ids:
attachment_states = wait_for_attachments(base_url, token, results.attachment_ids, args.ready_timeout)
total = len(results.latencies)
successes = sum(count for status, count in results.statuses.items() if status.startswith("2"))
report = {
"schema_version": 1,
"workload": args.mode,
"started_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(time.time() - elapsed)),
"duration_seconds": round(elapsed, 3),
"concurrency": args.concurrency,
"requested_rate": args.rate or None,
"requests": total,
"successful_requests": successes,
"request_rate": round(total / elapsed, 3) if elapsed else None,
"status_counts": dict(sorted(results.statuses.items())),
"latency_ms": {
"p50": percentile(results.latencies, 0.50),
"p95": percentile(results.latencies, 0.95),
"p99": percentile(results.latencies, 0.99),
},
"attachments_accepted": len(results.attachment_ids),
"attachment_terminal_states": attachment_states,
}
encoded = json.dumps(report, ensure_ascii=False, indent=2) + "\n"
if args.output:
destination = Path(args.output)
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(encoded, encoding="utf-8")
print(encoded, end="")
if not total:
raise SystemExit("no requests were sent")
if __name__ == "__main__":
main()