fix: address upload-memory verify findings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-22 16:11:03 +03:00
co-authored by Claude Opus 5.5
parent 79cc06b1e0
commit 9d2ee30cd2
17 changed files with 391 additions and 65 deletions
+132 -17
View File
@@ -461,6 +461,25 @@ def per_client_bodies(row: dict[str, Any]) -> float | None:
return (peak - before) * MIB / body / clients
def growth_per_client(
base: float | None,
value: float | None,
clients: int,
) -> float | None:
"""Рост медианы ячейки над базовой ячейкой на клиента, MiB."""
if base is None or value is None or clients <= 0:
return None
return (value - base) / clients
def cell_median(
grouped: dict[tuple[str, str, int], list[dict[str, Any]]],
key: tuple[str, str, int],
column: str,
) -> float | None:
return median(num(r.get(column)) for r in grouped.get(key, []))
def hypotheses(
rows: list[dict[str, Any]],
*,
@@ -480,19 +499,20 @@ def hypotheses(
out = []
# H1: с лимитом пик ≤ половины лимита, рост сверх C=10 ≤ 1 MiB на
# клиента, OOM и 5xx нет, лишние — 429
limited = [r for r in rows if r["variant"] == "limited"]
# клиента, OOM и 5xx нет, лишние — 429. Всё — по ячейкам тела big и
# C = 10, 20, 40, как в тексте гипотезы (метка baseline считала OOM,
# 5xx и 429 по всем размерам и C = 1)
levels = [c for c in (10, 20, 40) if group("limited", big, c)]
if levels:
limited = [r for c in levels for r in group("limited", big, c)]
peaks = {c: peak("limited", c) for c in levels}
base = peaks.get(10)
growth_ok = all(
base is not None
and peaks[c] is not None
and peaks[c] - base <= (c - 10)
growth = {
c: growth_per_client(base, peaks[c], c - 10)
for c in levels
if c > 10
)
}
growth_ok = all(v is not None and v <= 1 for v in growth.values())
half_ok = all(
v is not None and v <= memory_limit_mib / 2 for v in peaks.values()
)
@@ -508,8 +528,13 @@ def hypotheses(
"и 5xx нет, лишние — 429",
"numbers": "пик, MiB: "
+ ", ".join(f"C={c} {fmt(peaks[c])}" for c in levels)
+ f" (половина лимита {memory_limit_mib / 2:g}); OOM "
f"{oom}, 5xx {fives}, 429 {busy}",
+ f" (половина лимита {memory_limit_mib / 2:g}); сверх "
"C=10, MiB на клиента: "
+ (
", ".join(f"C={c} {fmt(v, 2)}" for c, v in growth.items())
or "—"
)
+ f"; OOM {oom}, 5xx {fives}, 429 {busy}",
"result": "да" if verdict else "нет",
},
)
@@ -749,16 +774,34 @@ def scenario_lines(rows: list[dict[str, Any]], big: str) -> list[str]:
lim = [grouped.get(("limited", big, c), []) for c in (10, 20, 40)]
if any(lim):
flat = [r for items in lim for r in items]
base = cell_median(grouped, ("limited", big, 10), "peak_mib")
over = [
f"C={c} "
+ fmt(
growth_per_client(
base,
cell_median(grouped, ("limited", big, c), "peak_mib"),
c - 10,
),
2,
)
for c, items in zip((20, 40), lim[1:], strict=True)
if items
]
lines.append(
"- `attachments`, «Память не растёт с числом загрузок» и "
"`load-balancing`, «Буферы соединений ограничены» («с лимитом», "
f"{big}): пик реплики "
"- `attachments`, «Память не растёт с числом загрузок» («с "
f"лимитом», {big}): пик реплики "
+ ", ".join(
f"C={c} {fmt(median(num(r['peak_mib']) for r in items))}"
for c, items in zip((10, 20, 40), lim, strict=True)
if items
)
+ " MiB, sock "
+ (
" MiB (сверх C=10, MiB на клиента: " + ", ".join(over) + ")"
if over
else " MiB"
)
+ ", sock "
+ ", ".join(
f"C={c} {fmt(median(num(r['sock_max_mib']) for r in items))}"
for c, items in zip((10, 20, 40), lim, strict=True)
@@ -774,10 +817,52 @@ def scenario_lines(rows: list[dict[str, Any]], big: str) -> list[str]:
f"{sum(errors_5xx(r) for r in flat)}, 429 "
f"{sum(int(num(r.get('s429')) or 0) for r in flat)}.",
)
lines.append(buffers_line(grouped, big))
lines.append("")
return lines
def buffers_line(
grouped: dict[tuple[str, str, int], list[dict[str, Any]]],
big: str,
) -> str:
"""`load-balancing`, «Буферы соединений ограничены»: рост медианы пика
LB и наибольшего `sock` реплики «с лимитом» от C = 1 до C = 40 на
клиента; выполнен, если их сумма ≤ 1 MiB и при C = 40 нет OOM.
Сумма — строже прочтения «каждый ≤ 1 MiB» (design, «Уточнения apply»,
п. 13)."""
one, top = ("limited", big, 1), ("limited", big, 40)
name = (
"- `load-balancing`, «Буферы соединений ограничены» («с лимитом», "
f"{big}, C = 1 → 40): "
)
if not grouped.get(one) or not grouped.get(top):
return name + "нет данных (нужны ячейки C = 1 и C = 40)."
parts, total = [], 0.0
for column, label in (
("lb_peak_mib", "пик LB"),
("sock_max_mib", "sock"),
):
a = cell_median(grouped, one, column)
b = cell_median(grouped, top, column)
slope = growth_per_client(a, b, 39)
if slope is None:
return name + f"нет данных ({label})."
total += slope
parts.append(
f"{label} ({fmt(b)} − {fmt(a)}) / 39 = {fmt(slope, 2)} MiB на "
"клиента",
)
ooms = sum(is_oom(r) for r in grouped[top])
verdict = "да" if total <= 1 and not ooms else "нет"
return (
name
+ "; ".join(parts)
+ f"; сумма {fmt(total, 2)} (порог 1 MiB на клиента), OOM при "
f"C = 40 {ooms}/{len(grouped[top])}; выполнен — {verdict}."
)
# --------------------------------------------------------------- selftest
@@ -912,19 +997,24 @@ def selftest() -> int:
return {**base, **extra}
rows = [
synth("limited", 1, 120, s429=7, lb_peak_mib=30, sock_max_mib=4),
synth("limited", 10, 170),
synth("limited", 20, 178, s429=5),
synth("limited", 40, 180, s429=60),
# другое тело: в H1 не входит ни 429, ни OOM
synth("limited", 40, 140, size="1MiB", s429=1000, flags="oom"),
synth("unlimited", 10, 230),
synth("unlimited", 20, 350),
synth("unlimited", 40, 512, flags="oom"),
synth("before", 10, 316),
synth("before", 20, 512, flags="oom"),
]
result = {
h["id"]: h["result"]
for h in hypotheses(rows, memory_limit_mib=512, big="10MiB")
}
checked = hypotheses(rows, memory_limit_mib=512, big="10MiB")
result = {h["id"]: h["result"] for h in checked}
h1 = checked[0]["numbers"]
expect("H1 429 of big body, C >= 10", "429 65" in h1, True)
expect("H1 OOM of big body", "OOM 0," in h1, True)
expect("H1 growth", "C=20 0.80, C=40 0.33" in h1, True)
expect("H1", result["H1"], "да")
expect("H2", result["H2"], "да")
expect("H3", result["H3"], "да")
@@ -960,6 +1050,31 @@ def selftest() -> int:
text = render_summary(meta, rows)
expect("summary has H1", "| H1 |" in text, True)
expect("summary has cells", "| с лимитом | 10MiB | 40 |" in text, True)
expect(
"replica growth over C=10",
"(сверх C=10, MiB на клиента: C=20 0.80, C=40 0.33)" in text,
True,
)
expect(
"buffers verdict",
"пик LB (50.0 − 30.0) / 39 = 0.51 MiB на клиента; sock (5.0 − 4.0) "
"/ 39 = 0.03 MiB на клиента; сумма 0.54"
in text
and "выполнен — да." in text,
True,
)
wide = rows + [synth("limited", 40, 181, lb_peak_mib=100)]
expect(
"buffers over the bound",
"выполнен — нет." in render_summary(meta, wide),
True,
)
expect(
"buffers without C=1",
"нет данных (нужны ячейки C = 1 и C = 40)"
in "\n".join(scenario_lines(rows[1:], "10MiB")),
True,
)
if failures:
print("selftest failed:\n " + "\n ".join(failures), flush=True)
return 1