lab4: бенчмарк — пользователь на поток, худший вход, классификация отказов

This commit is contained in:
2026-10-23 17:18:54 +03:00
parent 4b53e03fd6
commit 9caa0313be
17 changed files with 1343 additions and 877 deletions
+58 -33
View File
@@ -104,41 +104,56 @@ def percentile(values, fraction):
def one_upload(env, mode, token, chat_id, data, seed):
"""Returns (ok, elapsed_ms, outcome); outcome names the failing step and status, e.g. "complete:429"."""
digest = hashlib.sha256(data).hexdigest()
filename = f"bench-{seed}.png"
port = int(env.get("APP_PUBLIC_PORT", "8080"))
started = time.perf_counter()
if mode == "proxy":
status, payload = request(
"127.0.0.1", port, "POST",
f"/api/v1/chats/{chat_id}/attachments?filename={urllib.parse.quote(filename)}",
{"authorization": f"Bearer {token}", "content-type": "image/png"}, data,
)
ok = status == 201
attachment = json.loads(payload) if ok else None
else:
status, ticket = api_json(
env, "POST", f"/api/v1/chats/{chat_id}/attachments/direct-uploads", token,
{"filename": filename, "content_type": "image/png", "size_bytes": len(data), "sha256": digest},
)
ok = status == 201
attachment = None
if ok:
target = urllib.parse.urlsplit(ticket["url"])
put_status, _ = request(
target.hostname, target.port, "PUT", target.path + "?" + target.query,
{**ticket["headers"]}, data,
outcome, attachment = "ok", None
try:
if mode == "proxy":
status, body = request(
"127.0.0.1", port, "POST",
f"/api/v1/chats/{chat_id}/attachments?filename={urllib.parse.quote(filename)}",
{"authorization": f"Bearer {token}", "content-type": "image/png"}, data,
)
ok = put_status == 200
if ok:
status, attachment = api_json(
env, "POST", f"/api/v1/attachments/{ticket['attachment_id']}/complete", token
if status == 201:
attachment = json.loads(body)
else:
outcome = f"upload:{status}"
else:
headers = {"authorization": f"Bearer {token}", "content-type": "application/json"}
status, body = request(
"127.0.0.1", port, "POST", f"/api/v1/chats/{chat_id}/attachments/direct-uploads", headers,
json.dumps({"filename": filename, "content_type": "image/png",
"size_bytes": len(data), "sha256": digest}).encode(),
)
if status != 201:
outcome = f"init:{status}"
else:
ticket = json.loads(body)
target = urllib.parse.urlsplit(ticket["url"])
put_status, _ = request(
target.hostname, target.port, "PUT", target.path + "?" + target.query,
{**ticket["headers"]}, data,
)
ok = status == 200
if put_status != 200:
outcome = f"put:{put_status}"
else:
status, body = request(
"127.0.0.1", port, "POST", f"/api/v1/attachments/{ticket['attachment_id']}/complete",
{"authorization": f"Bearer {token}"},
)
if status == 200:
attachment = json.loads(body)
else:
outcome = f"complete:{status}"
except (OSError, http.client.HTTPException, ValueError) as error:
outcome = f"error:{type(error).__name__}"
elapsed_ms = (time.perf_counter() - started) * 1000
if ok and (attachment is None or attachment["sha256"] != digest):
ok = False
return ok, elapsed_ms
if outcome == "ok" and (attachment is None or attachment["sha256"] != digest):
outcome = "checksum_mismatch"
return outcome == "ok", elapsed_ms, outcome
def payload(args, seed):
@@ -155,6 +170,7 @@ def run_once(env, args, actors, ids):
baseline = {c: metrics(c) for c in ids}
net_before = {c: net_bytes(c) for c in ids}
latencies, failures = [], []
outcomes = {}
lock = threading.Lock()
def worker(index):
@@ -162,9 +178,10 @@ def run_once(env, args, actors, ids):
seed = index * 1000 + step
data = payload(args, seed)
token, chat_id = actors[index % len(actors)]
ok, elapsed = one_upload(env, args.mode, token, chat_id, data, seed)
ok, elapsed, outcome = one_upload(env, args.mode, token, chat_id, data, seed)
with lock:
(latencies if ok else failures).append(elapsed)
outcomes[outcome] = outcomes.get(outcome, 0) + 1
started = time.perf_counter()
threads = [threading.Thread(target=worker, args=(i,)) for i in range(args.concurrency)]
@@ -195,6 +212,7 @@ def run_once(env, args, actors, ids):
"uploads": total,
"succeeded": len(latencies),
"failed": len(failures),
"outcomes": outcomes,
"duration_s": round(duration, 3),
"latency_ms": {
"p50": round(percentile(latencies, 0.50), 1),
@@ -264,7 +282,8 @@ def command_run(args):
for index, item in enumerate(repeats):
print(f"repeat {index + 1}: ok={item['succeeded']}/{item['uploads']} "
f"rx/upload={item['rx_bytes_per_upload']} tx/upload={item['tx_bytes_per_upload']} "
f"maxΔrss={item['max_delta_rss_bytes']} p50={item['latency_ms']['p50']}ms")
f"maxΔrss={item['max_delta_rss_bytes']} p50={item['latency_ms']['p50']}ms "
f"outcomes={item['outcomes']}")
def mib(value):
@@ -282,15 +301,21 @@ def command_summarize(args):
label = p["mode"] + (f" ({p['image']})" if p.get("image") else "")
rows.append((label, p["size_bytes"], p["concurrency"], reps, med, data))
rows.sort(key=lambda r: (r[0].count("("), r[1], r[0], r[2]))
print("| mode | size, MiB | C | ok | API rx/upload, MiB | API tx/upload, MiB | max Δrss, MiB | peak RSS, MiB | p50, ms | p95, ms |")
print("|---|---|---|---|---|---|---|---|---|---|")
print("| mode | size, MiB | C | ok | failures | API rx/upload, MiB | API tx/upload, MiB | max Δrss, MiB | peak RSS, MiB | p50, ms | p95, ms |")
print("|---|---|---|---|---|---|---|---|---|---|---|")
for mode, size, conc, reps, med, _data in rows:
peak = max(x["peak_memory_bytes"]["rss"] for r in reps for x in r["replicas"])
failed = {}
for rep in reps:
for name, count in rep.get("outcomes", {}).items():
if name != "ok":
failed[name] = failed.get(name, 0) + count
failures = ", ".join(f"{name}×{count}" for name, count in sorted(failed.items())) or "—"
ok = sum(r["succeeded"] for r in reps)
total = sum(r["uploads"] for r in reps)
p50 = statistics.median(r["latency_ms"]["p50"] for r in reps)
p95 = statistics.median(r["latency_ms"]["p95"] for r in reps)
print(f"| {mode} | {mib(size)} | {conc} | {ok}/{total} | {mib(med('rx_bytes_per_upload'))} | "
print(f"| {mode} | {mib(size)} | {conc} | {ok}/{total} | {failures} | {mib(med('rx_bytes_per_upload'))} | "
f"{mib(med('tx_bytes_per_upload'))} | {mib(med('max_delta_rss_bytes'))} | {mib(peak)} | {p50:.0f} | {p95:.0f} |")
+3 -2
View File
@@ -22,8 +22,9 @@ bench proxy 9961472 20
# Worst case for memory: every request carries the heaviest valid image
# (40 MP RGBA16 Adam7 PNG padded to 10 MiB, ~340 MiB per decode) and C=32
# saturates UPLOAD_MAX_IN_FLIGHT (16) on both replicas.
worst=$(mktemp --suffix=.png)
trap 'rm -f "$worst"' EXIT
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
worst="$tmp/worst-40mp-rgba16-adam7.png"
python3 scripts/lab4/pngkit.py > "$worst"
for mode in proxy direct; do
echo "== $mode worst-case image c=32"