Files
mtusi2026lab4/scripts/benchmark_upload_tracks.py

376 lines
9.7 KiB
Python

import csv
import json
import subprocess
import sys
import threading
import time
import urllib.request
import hashlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check
BASE_URL = "http://localhost:8080"
FIXTURES = [
ROOT / "tests" / "fixtures" / "sample.png",
ROOT / "tests" / "fixtures" / "sample_large.png",
]
API_CONTAINERS = [
"mtusi-lab4-api-1",
"mtusi-lab4-api-2",
]
REPEATS = 3
OUTPUT = ROOT / "evidence" / "track1-results.csv"
def docker_memory():
"""Return current memory usage in MiB for both API containers."""
result = subprocess.run(
[
"docker",
"stats",
"--no-stream",
"--format",
"{{.Name}}|{{.MemUsage}}",
],
capture_output=True,
text=True,
check=True,
)
values = {}
for line in result.stdout.splitlines():
if "|" not in line:
continue
name, usage = line.split("|", 1)
if name not in API_CONTAINERS:
continue
memory_value = usage.split("/", 1)[0].strip()
values[name] = parse_memory_mib(memory_value)
missing = set(API_CONTAINERS) - set(values)
if missing:
raise RuntimeError(
"Could not read memory for containers: "
+ ", ".join(sorted(missing))
)
return values
def parse_memory_mib(value):
"""Convert Docker memory values to MiB."""
value = value.strip()
units = {
"B": 1 / (1024 * 1024),
"KiB": 1 / 1024,
"MiB": 1,
"GiB": 1024,
"kB": 1000 / (1024 * 1024),
"MB": 1000 * 1000 / (1024 * 1024),
"GB": 1000 * 1000 * 1000 / (1024 * 1024),
"Ki": 1 / 1024,
"Mi": 1,
"Gi": 1024,
}
for unit in sorted(units, key=len, reverse=True):
if value.endswith(unit):
number = value[:-len(unit)].strip()
return float(number) * units[unit]
raise ValueError(f"Unknown Docker memory unit: {value}")
class MemoryMonitor:
"""Sample API container memory while one upload is running."""
def __init__(self):
self.baseline = {}
self.peak = {}
self.running = False
self.thread = None
def _sample(self):
while self.running:
try:
current = docker_memory()
for name, value in current.items():
self.peak[name] = max(
self.peak.get(name, value),
value,
)
except Exception as exc:
print(
"WARNING: memory sampling failed:",
type(exc).__name__,
)
time.sleep(0.1)
def __enter__(self):
self.baseline = docker_memory()
self.peak = dict(self.baseline)
self.running = True
self.thread = threading.Thread(
target=self._sample,
daemon=True,
)
self.thread.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.running = False
if self.thread:
self.thread.join(timeout=3)
try:
current = docker_memory()
for name, value in current.items():
self.peak[name] = max(
self.peak.get(name, value),
value,
)
except Exception:
pass
@property
def peak_rss_mib(self):
return max(self.peak.values())
@property
def peak_delta_mib(self):
return max(
self.peak[name] - self.baseline[name]
for name in API_CONTAINERS
)
def response_json(response):
return json.loads(response["bytes"].decode("utf-8"))
def proxy_upload(client, actor, chat, content):
"""Upload a file through the API proxy endpoint."""
started = time.monotonic()
response = client.call(
"POST",
f"/api/v1/chats/{chat['id']}/attachments?filename=benchmark.png",
201,
actor=actor,
raw=content,
headers={"Content-Type": "image/png"},
)
elapsed = time.monotonic() - started
attachment = response_json(response)
check(attachment["status"] == "ready", "proxy attachment is not ready")
check(
attachment["size_bytes"] == len(content),
"proxy attachment size mismatch",
)
check(
attachment["sha256"] == hashlib.sha256(content).hexdigest(),
"proxy attachment checksum mismatch",
)
return {
"elapsed_total_s": elapsed,
"elapsed_api_s": response["elapsed"],
"api_request_body_bytes": len(content),
"s3_upload_bytes": 0,
}
def presigned_upload(client, actor, chat, content):
"""Upload a file directly to S3 using a presigned URL."""
digest = hashlib.sha256(content).hexdigest()
create_body = {
"filename": "benchmark.png",
"content_type": "image/png",
"size_bytes": len(content),
"sha256": digest,
}
create_body_bytes = len(
json.dumps(create_body, ensure_ascii=False).encode("utf-8")
)
started = time.monotonic()
create_response = client.call(
"POST",
f"/api/v1/chats/{chat['id']}/attachments/presigned",
201,
actor=actor,
body=create_body,
)
created = response_json(create_response)
check(created["method"] == "PUT", "unexpected upload method")
s3_request = urllib.request.Request(
created["upload_url"],
data=content,
headers=created["headers"],
method="PUT",
)
with urllib.request.urlopen(s3_request, timeout=60) as response:
check(
response.status in (200, 201, 204),
"direct S3 upload failed",
)
complete_response = client.call(
"POST",
f"/api/v1/attachments/uploads/{created['upload_id']}/complete",
200,
actor=actor,
)
attachment = response_json(complete_response)
elapsed = time.monotonic() - started
check(attachment["status"] == "ready", "presigned attachment is not ready")
check(
attachment["size_bytes"] == len(content),
"presigned attachment size mismatch",
)
check(
attachment["sha256"] == digest,
"presigned attachment checksum mismatch",
)
return {
"elapsed_total_s": elapsed,
"elapsed_api_s": (
create_response["elapsed"] + complete_response["elapsed"]
),
"api_request_body_bytes": create_body_bytes,
"s3_upload_bytes": len(content),
}
def main():
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
for path in FIXTURES:
if not path.exists():
raise FileNotFoundError(path)
if path.stat().st_size > 10 * 1024 * 1024:
raise RuntimeError(f"Fixture exceeds 10 MiB: {path.name}")
print("Checking API readiness...")
with urllib.request.urlopen(
BASE_URL + "/health/ready",
timeout=10,
) as response:
check(response.status == 200, "API is not ready")
client = Client(BASE_URL)
alice, bob, eve, chat = client.bootstrap()
rows = []
for fixture in FIXTURES:
content = fixture.read_bytes()
size_bytes = len(content)
for method in ("proxy", "presigned"):
for repeat in range(1, REPEATS + 1):
print(
f"Running {method}: {fixture.name}, "
f"repeat {repeat}/{REPEATS}"
)
with MemoryMonitor() as memory:
if method == "proxy":
metrics = proxy_upload(
client,
alice,
chat,
content,
)
else:
metrics = presigned_upload(
client,
alice,
chat,
content,
)
row = {
"method": method,
"file": fixture.name,
"file_size_bytes": size_bytes,
"repeat": repeat,
"elapsed_total_s": round(
metrics["elapsed_total_s"], 4
),
"elapsed_api_s": round(
metrics["elapsed_api_s"], 4
),
"client_to_api_body_bytes": (
metrics["api_request_body_bytes"]
),
"client_to_s3_upload_bytes": (
metrics["s3_upload_bytes"]
),
"api_peak_rss_mib": round(
memory.peak_rss_mib, 2
),
"api_peak_delta_mib": round(
memory.peak_delta_mib, 2
),
}
rows.append(row)
print(
f" total={row['elapsed_total_s']}s, "
f"API={row['elapsed_api_s']}s, "
f"API body={row['client_to_api_body_bytes']} bytes, "
f"peak RAM={row['api_peak_rss_mib']} MiB, "
f"RAM delta={row['api_peak_delta_mib']} MiB"
)
fieldnames = list(rows[0].keys())
with OUTPUT.open("w", newline="", encoding="utf-8-sig") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
print()
print("PASS: Track 1 benchmark completed")
print(f"Results: {OUTPUT}")
print(f"Measurements: {len(rows)}")
if __name__ == "__main__":
main()