111 lines
4.3 KiB
Python
111 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Small synthetic image workload; reports upload and time-to-ready separately."""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import os
|
|
import struct
|
|
import sys
|
|
import time
|
|
import uuid
|
|
import zlib
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure # noqa: E402
|
|
|
|
|
|
def png_noise(width, height):
|
|
raw = b"".join(b"\0" + os.urandom(width * 3) for _ in range(height))
|
|
|
|
def chunk(kind, data):
|
|
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data))
|
|
|
|
return (b"\x89PNG\r\n\x1a\n" +
|
|
chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)) +
|
|
chunk(b"IDAT", zlib.compress(raw, 1)) + chunk(b"IEND", b""))
|
|
|
|
|
|
def percentile(values, percentile_value):
|
|
values = sorted(values)
|
|
return round(values[max(0, math.ceil(len(values) * percentile_value) - 1)], 3)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--base-url", default="http://127.0.0.1:8080")
|
|
parser.add_argument("--concurrency", type=int, default=4)
|
|
parser.add_argument("--per-size", type=int, default=4)
|
|
parser.add_argument("--profile", choices=("small_320x200", "noise_800x600"))
|
|
parser.add_argument("--output", required=True)
|
|
args = parser.parse_args()
|
|
if not 1 <= args.concurrency <= 16 or not 1 <= args.per_size <= 100:
|
|
parser.error("invalid workload size")
|
|
|
|
setup = Client(args.base_url)
|
|
alice, _bob, _eve, chat = setup.bootstrap()
|
|
images = {
|
|
"small_320x200": (ROOT / "tests/fixtures/sample.png").read_bytes(),
|
|
"noise_800x600": png_noise(800, 600),
|
|
}
|
|
if args.profile:
|
|
images = {args.profile: images[args.profile]}
|
|
items = [(name, data) for name, data in images.items() for _ in range(args.per_size)]
|
|
|
|
def work(item):
|
|
name, data = item
|
|
client = Client(args.base_url, timeout=20)
|
|
start = time.monotonic()
|
|
response = client.call(
|
|
"POST", f"/api/v1/chats/{chat['id']}/attachments?filename={name}.png",
|
|
202, actor=alice, raw=data, headers={"Content-Type": "image/png"}, schema="Attachment",
|
|
)
|
|
accepted = time.monotonic()
|
|
attachment = response["json"]
|
|
if attachment["sha256"] != hashlib.sha256(data).hexdigest():
|
|
raise Failure("accepted checksum mismatch")
|
|
client.wait_ready(alice, attachment, wait_seconds=180)
|
|
ready = time.monotonic()
|
|
return {"profile": name, "size_bytes": len(data),
|
|
"upload_ms": round((accepted - start) * 1000, 3),
|
|
"ready_ms": round((ready - start) * 1000, 3)}
|
|
|
|
started = time.monotonic()
|
|
completed, failures = [], []
|
|
with ThreadPoolExecutor(max_workers=args.concurrency) as executor:
|
|
for future in as_completed([executor.submit(work, item) for item in items]):
|
|
try:
|
|
completed.append(future.result())
|
|
except (Failure, OSError, ValueError) as exc:
|
|
failures.append(type(exc).__name__)
|
|
elapsed = time.monotonic() - started
|
|
result = {
|
|
"lab": 6, "workload": "synthetic PNG upload and poll until ready",
|
|
"concurrency": args.concurrency, "per_size": args.per_size,
|
|
"elapsed_seconds": round(elapsed, 3), "accepted_ready": len(completed),
|
|
"failed": len(failures), "failure_types": sorted(set(failures)),
|
|
"profiles": {},
|
|
}
|
|
for name, data in images.items():
|
|
rows = [item for item in completed if item["profile"] == name]
|
|
result["profiles"][name] = {
|
|
"size_bytes": len(data), "successful": len(rows),
|
|
"upload_ms": {f"p{p}": percentile([r["upload_ms"] for r in rows], p / 100)
|
|
for p in (50, 95, 99)} if rows else {},
|
|
"ready_ms": {f"p{p}": percentile([r["ready_ms"] for r in rows], p / 100)
|
|
for p in (50, 95, 99)} if rows else {},
|
|
}
|
|
Path(args.output).write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
print(json.dumps(result, indent=2, sort_keys=True))
|
|
if failures or len(completed) != len(items):
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|