forked from mtusi2026/mtusi2026lab4
144 lines
5.0 KiB
Python
144 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Compare API traffic and memory for proxy and presigned uploads."""
|
|
|
|
import argparse
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import struct
|
|
import subprocess
|
|
import threading
|
|
import time
|
|
import urllib.request
|
|
import uuid
|
|
import zlib
|
|
|
|
from client import Client, check
|
|
|
|
|
|
def chunk(kind, payload):
|
|
return struct.pack(">I", len(payload)) + kind + payload + struct.pack(">I", zlib.crc32(kind + payload))
|
|
|
|
|
|
def png(width, height):
|
|
compressor = zlib.compressobj(level=0)
|
|
encoded = bytearray()
|
|
row = b"\0" + bytes(range(256)) * ((width * 3 + 255) // 256)
|
|
for _ in range(height):
|
|
encoded.extend(compressor.compress(row[:width * 3 + 1]))
|
|
encoded.extend(compressor.flush())
|
|
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)) + chunk(b"IDAT", bytes(encoded)) + chunk(b"IEND", b"")
|
|
|
|
|
|
def memory_mib():
|
|
ids = subprocess.run(
|
|
["docker", "compose", "--env-file", ".env", "ps", "-q", "api"],
|
|
check=True, capture_output=True, text=True,
|
|
).stdout.split()
|
|
if not ids:
|
|
return 0.0
|
|
values = subprocess.run(
|
|
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", *ids],
|
|
check=True, capture_output=True, text=True,
|
|
).stdout.splitlines()
|
|
total = 0.0
|
|
for value in values:
|
|
match = re.fullmatch(r"([0-9.]+)(KiB|MiB|GiB)", value.split("/")[0].strip())
|
|
if match is None:
|
|
raise ValueError("unexpected docker stats memory format")
|
|
number, unit = match.groups()
|
|
factor = {"KiB": 1 / 1024, "MiB": 1, "GiB": 1024}[unit]
|
|
total += float(number) * factor
|
|
return total
|
|
|
|
|
|
def sample_memory(stop, samples):
|
|
while not stop.is_set():
|
|
try:
|
|
samples.append(memory_mib())
|
|
except (OSError, subprocess.SubprocessError, ValueError, KeyError):
|
|
pass
|
|
stop.wait(0.1)
|
|
|
|
|
|
def proxy(client, actor, chat, content):
|
|
client.call(
|
|
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=bench.png", 201,
|
|
actor=actor, raw=content, headers={"Content-Type": "image/png"}, schema="Attachment",
|
|
)
|
|
|
|
|
|
def direct(client, actor, chat, content):
|
|
ticket = json.loads(client.call(
|
|
"POST", "/api/v1/chats/" + chat["id"] + "/attachments/presign", 201,
|
|
actor=actor,
|
|
body={
|
|
"filename": "bench.png", "content_type": "image/png",
|
|
"size_bytes": len(content), "sha256": hashlib.sha256(content).hexdigest(),
|
|
},
|
|
)["bytes"])
|
|
request = urllib.request.Request(
|
|
ticket["upload_url"], data=content, method="PUT", headers=ticket["required_headers"],
|
|
)
|
|
with urllib.request.urlopen(request, timeout=60) as response:
|
|
check(response.status == 200, "presigned PUT failed")
|
|
client.call(
|
|
"POST", "/api/v1/attachments/" + ticket["attachment_id"] + "/complete", 200,
|
|
actor=actor, body={}, schema="Attachment",
|
|
)
|
|
|
|
|
|
def measure(client, actor, chat, content, concurrency, mode):
|
|
stop = threading.Event()
|
|
samples = []
|
|
sampler = threading.Thread(target=sample_memory, args=(stop, samples), daemon=True)
|
|
sampler.start()
|
|
started = time.monotonic()
|
|
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
|
futures = [pool.submit(proxy if mode == "proxy" else direct, client, actor, chat, content)
|
|
for _ in range(concurrency)]
|
|
for future in futures:
|
|
future.result()
|
|
elapsed = time.monotonic() - started
|
|
stop.set()
|
|
sampler.join()
|
|
return {
|
|
"mode": mode,
|
|
"file_bytes": len(content),
|
|
"concurrency": concurrency,
|
|
"elapsed_seconds": round(elapsed, 3),
|
|
"api_upload_body_bytes": len(content) * concurrency if mode == "proxy" else 0,
|
|
"api_storage_body_bytes": len(content) * concurrency,
|
|
"api_total_binary_bytes": len(content) * concurrency * (2 if mode == "proxy" else 1),
|
|
"s3_upload_body_bytes": len(content) * concurrency,
|
|
"api_replicas_peak_memory_mib": round(max(samples), 3) if samples else None,
|
|
"sample_count": len(samples),
|
|
}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--base-url", default="http://localhost:8080")
|
|
parser.add_argument("--concurrency", type=int, default=10)
|
|
parser.add_argument("--output", default="evidence/upload_benchmark.json")
|
|
args = parser.parse_args()
|
|
client = Client(args.base_url, timeout=90)
|
|
alice, _, _, chat = client.bootstrap()
|
|
results = []
|
|
for content in (png(600, 600), png(1600, 1600)):
|
|
for mode in ("proxy", "direct"):
|
|
results.append(measure(client, alice, chat, content, args.concurrency, mode))
|
|
output = {
|
|
"run_id": uuid.uuid4().hex,
|
|
"method": "closed-loop, same valid PNG shared by 10 client threads; 100 ms docker-stats sampling",
|
|
"results": results,
|
|
}
|
|
Path(args.output).write_text(json.dumps(output, indent=2) + "\n", encoding="utf-8")
|
|
print(json.dumps(output, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|