forked from mtusi2026/mtusi2026lab5
Очереди и асинхронная обработка изображений, трек 1
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
import argparse
|
||||
import base64
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
from PIL import Image
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
||||
EVIDENCE = Path(__file__).resolve().parent.parent / "evidence"
|
||||
WORKER_CONTAINER = "mtusi2026lab5-worker-1"
|
||||
SIZES = {
|
||||
"small": (320, 200),
|
||||
"medium": (1280, 800),
|
||||
"large": (4000, 3000),
|
||||
}
|
||||
|
||||
|
||||
def _png(size):
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", size, (64, 128, 192)).save(buf, format="PNG")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def _user(c):
|
||||
u = "qb_" + uuid.uuid4().hex[:10]
|
||||
p = "Test-password-1234"
|
||||
c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "Q", "password": p})
|
||||
cred = base64.b64encode(f"{u}:{p}".encode()).decode()
|
||||
s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json()
|
||||
return {"id": s["user"]["id"], "token": s["token"]}
|
||||
|
||||
|
||||
def _auth(a):
|
||||
return {"Authorization": "Bearer " + a["token"]}
|
||||
|
||||
|
||||
def _worker_mem():
|
||||
out = subprocess.run(
|
||||
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", WORKER_CONTAINER],
|
||||
capture_output=True, text=True,
|
||||
).stdout.strip()
|
||||
if not out or "/" not in out:
|
||||
return None
|
||||
v = out.split("/")[0].strip()
|
||||
units = {"B": 1, "KiB": 1024, "Ki": 1024, "MiB": 1024**2, "Mi": 1024**2,
|
||||
"GiB": 1024**3, "Gi": 1024**3}
|
||||
for u in sorted(units, key=len, reverse=True):
|
||||
if v.endswith(u):
|
||||
return round(int(float(v[: -len(u)]) * units[u]) / 1024**2, 1)
|
||||
return None
|
||||
|
||||
|
||||
def _restart_worker(prefetch):
|
||||
env = dict(os.environ)
|
||||
env["PREFETCH_COUNT"] = str(prefetch)
|
||||
subprocess.run(
|
||||
COMPOSE + ["up", "-d", "--force-recreate", "worker"],
|
||||
env=env, check=True,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
def _start_worker(prefetch):
|
||||
env = dict(os.environ)
|
||||
env["PREFETCH_COUNT"] = str(prefetch)
|
||||
subprocess.run(
|
||||
COMPOSE + ["up", "-d", "--force-recreate", "worker"],
|
||||
env=env, check=True,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
|
||||
def _percentiles(values, ps=(0.5, 0.95, 0.99)):
|
||||
if not values:
|
||||
return {f"p{int(p*100)}": None for p in ps}
|
||||
s = sorted(values)
|
||||
return {f"p{int(p*100)}": round(s[max(0, int(len(s)*p)-1)], 3) for p in ps}
|
||||
|
||||
|
||||
def run(prefetch, size_name, files, output):
|
||||
size = SIZES[size_name]
|
||||
data = _png(size)
|
||||
with httpx.Client(timeout=30) as c:
|
||||
a = _user(c)
|
||||
b = _user(c)
|
||||
chat = c.post(
|
||||
f"{BASE}/api/v1/chats",
|
||||
json={"title": f"qb-{prefetch}-{size_name}", "member_ids": [a["id"], b["id"]]},
|
||||
headers=_auth(a),
|
||||
).json()
|
||||
chat_id = chat["id"]
|
||||
|
||||
_restart_worker(prefetch)
|
||||
|
||||
uploads = []
|
||||
started = time.monotonic()
|
||||
with httpx.Client(timeout=30) as c:
|
||||
for i in range(files):
|
||||
t = time.monotonic()
|
||||
r = c.post(
|
||||
f"{BASE}/api/v1/chats/{chat_id}/attachments",
|
||||
params={"filename": f"f{i}.png"},
|
||||
content=data,
|
||||
headers={**_auth(a), "Content-Type": "image/png"},
|
||||
)
|
||||
if r.status_code != 202:
|
||||
print(f"upload failed: {r.status_code} {r.text[:100]}", file=sys.stderr)
|
||||
continue
|
||||
uploads.append({"id": r.json()["id"], "t_upload": t})
|
||||
|
||||
states = {u["id"]: {"t_upload": u["t_upload"], "t_processing": None, "t_ready": None}
|
||||
for u in uploads}
|
||||
deadline = time.monotonic() + 600
|
||||
pending = set(states)
|
||||
with httpx.Client(timeout=10) as c:
|
||||
while pending and time.monotonic() < deadline:
|
||||
for att_id in list(pending):
|
||||
r = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers=_auth(a))
|
||||
if r.status_code != 200:
|
||||
continue
|
||||
st = r.json()["status"]
|
||||
now = time.monotonic()
|
||||
if st == "processing" and states[att_id]["t_processing"] is None:
|
||||
states[att_id]["t_processing"] = now
|
||||
if st in ("ready", "failed"):
|
||||
if states[att_id]["t_processing"] is None:
|
||||
states[att_id]["t_processing"] = now
|
||||
states[att_id]["t_ready"] = now
|
||||
pending.discard(att_id)
|
||||
if pending:
|
||||
time.sleep(0.2)
|
||||
|
||||
finished = time.monotonic()
|
||||
rss = _worker_mem()
|
||||
|
||||
queue_lags = [
|
||||
s["t_processing"] - s["t_upload"]
|
||||
for s in states.values() if s["t_processing"] is not None
|
||||
]
|
||||
ready_times = [
|
||||
s["t_ready"] - s["t_upload"]
|
||||
for s in states.values() if s["t_ready"] is not None
|
||||
]
|
||||
duration = finished - started
|
||||
ok = len(ready_times)
|
||||
report = {
|
||||
"prefetch": prefetch,
|
||||
"size": size_name,
|
||||
"files_requested": files,
|
||||
"files_ok": ok,
|
||||
"files_failed": files - ok,
|
||||
"duration_s": round(duration, 3),
|
||||
"throughput_files_per_s": round(ok / duration, 3) if duration else None,
|
||||
"queue_lag_s": _percentiles(queue_lags),
|
||||
"time_to_ready_s": _percentiles(ready_times),
|
||||
"worker_rss_mib": rss,
|
||||
}
|
||||
out = Path(output)
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
out.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8")
|
||||
print(json.dumps(report, indent=2, ensure_ascii=False))
|
||||
_start_worker(prefetch)
|
||||
return report
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--prefetch", type=int, required=True)
|
||||
parser.add_argument("--size", choices=list(SIZES), required=True)
|
||||
parser.add_argument("--files", type=int, default=50)
|
||||
parser.add_argument("--output", required=True)
|
||||
args = parser.parse_args()
|
||||
run(args.prefetch, args.size, args.files, args.output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user