forked from mtusi2026/mtusi2026lab5
Очереди и асинхронная обработка изображений, трек 1
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import argparse
|
||||
import base64
|
||||
import io
|
||||
import json
|
||||
import subprocess
|
||||
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"
|
||||
|
||||
|
||||
def _png(size=(1280, 800)):
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", size, (128, 64, 192)).save(buf, format="PNG")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def _user(c):
|
||||
u = "qr_" + 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 main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--files", type=int, default=100)
|
||||
parser.add_argument("--output", default="evidence/queue-recovery.json")
|
||||
args = parser.parse_args()
|
||||
|
||||
data = _png()
|
||||
with httpx.Client(timeout=30) as c:
|
||||
a = _user(c)
|
||||
b = _user(c)
|
||||
chat = c.post(
|
||||
f"{BASE}/api/v1/chats",
|
||||
json={"title": "recovery", "member_ids": [a["id"], b["id"]]},
|
||||
headers=_auth(a),
|
||||
).json()
|
||||
chat_id = chat["id"]
|
||||
|
||||
subprocess.run(COMPOSE + ["stop", "worker"], check=True,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
|
||||
ids = []
|
||||
with httpx.Client(timeout=30) as c:
|
||||
for i in range(args.files):
|
||||
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:
|
||||
ids.append(r.json()["id"])
|
||||
print(f"uploaded {len(ids)} files, queue accumulated", flush=True)
|
||||
|
||||
time.sleep(10)
|
||||
subprocess.run(COMPOSE + ["start", "worker"], check=True,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
worker_started = time.monotonic()
|
||||
|
||||
deadline = time.monotonic() + 600
|
||||
pending = set(ids)
|
||||
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
|
||||
if r.json()["status"] in ("ready", "failed"):
|
||||
pending.discard(att_id)
|
||||
if pending:
|
||||
time.sleep(0.5)
|
||||
finished = time.monotonic()
|
||||
report = {
|
||||
"files": args.files,
|
||||
"finished": args.files - len(pending),
|
||||
"pending_after_timeout": len(pending),
|
||||
"recovery_time_s": round(finished - worker_started, 3),
|
||||
}
|
||||
out = Path(args.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))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user