lab4: исправления второго ревью (40 Мп в tmpfs, таймауты S3, параллельный complete, auth до Content-Type, лимит на пользователя, бюджет памяти декода)

This commit is contained in:
2026-10-23 17:09:49 +03:00
parent a06471a6c6
commit 4b53e03fd6
27 changed files with 642 additions and 141 deletions
+43 -21
View File
@@ -141,7 +141,14 @@ def one_upload(env, mode, token, chat_id, data, seed):
return ok, elapsed_ms
def run_once(env, args, token, chat_id, ids):
def payload(args, seed):
if args.image:
with open(args.image, "rb") as handle:
return handle.read()
return noise_png(args.size_bytes, seed)
def run_once(env, args, actors, ids):
for container in ids:
reset_metrics(container)
time.sleep(0.2)
@@ -153,7 +160,8 @@ def run_once(env, args, token, chat_id, ids):
def worker(index):
for step in range(args.uploads_per_worker):
seed = index * 1000 + step
data = noise_png(args.size_bytes, seed)
data = payload(args, seed)
token, chat_id = actors[index % len(actors)]
ok, elapsed = one_upload(env, args.mode, token, chat_id, data, seed)
with lock:
(latencies if ok else failures).append(elapsed)
@@ -203,29 +211,39 @@ def run_once(env, args, token, chat_id, ids):
def command_run(args):
if args.image:
args.size_bytes = os.path.getsize(args.image)
if args.size_bytes <= 0:
sys.exit("--size-bytes or --image is required")
env = read_env()
ids = containers()
if not ids:
sys.exit("no api containers found")
name = f"bench_{uuid.uuid4().hex[:10]}"
password = "bench:" + uuid.uuid4().hex
status, user = api_json(env, "POST", "/api/v1/users",
body={"username": name, "display_name": "bench", "password": password})
assert status == 201, status
status, session = api_json(env, "POST", "/api/v1/auth/sessions", basic=f"{name}:{password}")
assert status == 201, status
token = session["token"]
status, chat = api_json(env, "POST", "/api/v1/chats", token, {"title": "bench", "member_ids": [user["id"]]})
assert status == 201, status
# One user per worker: the API caps concurrent uploads per user
# (UPLOAD_MAX_IN_FLIGHT_PER_USER), and real traffic comes from many users.
actors = []
for _ in range(args.concurrency):
name = f"bench_{uuid.uuid4().hex[:10]}"
password = "bench:" + uuid.uuid4().hex
status, user = api_json(env, "POST", "/api/v1/users",
body={"username": name, "display_name": "bench", "password": password})
assert status == 201, status
status, session = api_json(env, "POST", "/api/v1/auth/sessions", basic=f"{name}:{password}")
assert status == 201, status
token = session["token"]
status, chat = api_json(env, "POST", "/api/v1/chats", token, {"title": "bench", "member_ids": [user["id"]]})
assert status == 201, status
actors.append((token, chat["id"]))
# Warm-up: one upload per replica path, not measured.
# Warm-up: two uploads per replica, not measured.
for seed in range(len(ids) * 2):
one_upload(env, args.mode, token, chat["id"], noise_png(args.size_bytes, 900000 + seed), seed)
token, chat_id = actors[seed % len(actors)]
one_upload(env, args.mode, token, chat_id, payload(args, 900000 + seed), seed)
limits = []
for c in ids:
limits.append(sh("docker", "inspect", "-f", "{{.HostConfig.NanoCpus}} {{.HostConfig.Memory}}", c))
repeats = [run_once(env, args, token, chat["id"], ids) for _ in range(args.repeat)]
repeats = [run_once(env, args, actors, ids) for _ in range(args.repeat)]
mem_total = next(int(l.split()[1]) * 1024 for l in open("/proc/meminfo") if l.startswith("MemTotal"))
result = {
"date": datetime.now(timezone.utc).isoformat(timespec="seconds"),
@@ -233,6 +251,7 @@ def command_run(args):
"params": {
"mode": args.mode, "size_bytes": args.size_bytes, "concurrency": args.concurrency,
"uploads_per_worker": args.uploads_per_worker, "repeat": args.repeat,
"users": args.concurrency, "image": os.path.basename(args.image) if args.image else None,
},
"host": {"nproc": os.cpu_count(), "mem_total_bytes": mem_total},
"api_container_limits_nanocpus_memory": limits,
@@ -260,17 +279,19 @@ def command_summarize(args):
p = data["params"]
reps = data["repeats"]
med = lambda key: statistics.median(r[key] for r in reps) # noqa: E731
rows.append((p["mode"], p["size_bytes"], p["concurrency"], reps, med, data))
rows.sort(key=lambda r: (r[1], r[0], r[2]))
print("| mode | size, MiB | C | ok | API rx/upload, MiB | API tx/upload, MiB | max Δrss, MiB | p50, ms | p95, ms |")
print("|---|---|---|---|---|---|---|---|---|")
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("|---|---|---|---|---|---|---|---|---|---|")
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"])
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'))} | "
f"{mib(med('tx_bytes_per_upload'))} | {mib(med('max_delta_rss_bytes'))} | {p50:.0f} | {p95:.0f} |")
f"{mib(med('tx_bytes_per_upload'))} | {mib(med('max_delta_rss_bytes'))} | {mib(peak)} | {p50:.0f} | {p95:.0f} |")
def main():
@@ -278,7 +299,8 @@ def main():
sub = parser.add_subparsers(dest="command", required=True)
run = sub.add_parser("run")
run.add_argument("--mode", choices=["proxy", "direct"], required=True)
run.add_argument("--size-bytes", type=int, required=True)
run.add_argument("--size-bytes", type=int, default=0)
run.add_argument("--image", help="upload this file instead of generated noise PNGs")
run.add_argument("--concurrency", type=int, default=10)
run.add_argument("--uploads-per-worker", type=int, default=5)
run.add_argument("--repeat", type=int, default=3)
+17 -6
View File
@@ -1,22 +1,33 @@
// Peak RSS of one full decode check (same sharp options as ImageDecoder) on
// worst-case inputs near MAX_IMAGE_PIXELS. Run from app/: node ../scripts/lab4/decode_memory.mjs <files...>
// Peak RSS of one full decode check with the same sharp pipeline as
// app/src/attachments/image/image-decoder.ts, on worst-case inputs near
// MAX_IMAGE_PIXELS. Runs where the API runs (same libvips, VIPS_DISC_THRESHOLD):
// docker compose exec -T api node - < scripts/lab4/decode_memory.mjs # reads files from IMAGES (paths in the container)
// or on the host from app/: IMAGES="a.png b.jpg" node ../scripts/lab4/decode_memory.mjs
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
const sharp = createRequire(`${process.cwd()}/`)('sharp');
sharp.cache(false);
sharp.concurrency(1);
for (const file of process.argv.slice(2)) {
for (const file of (process.env.IMAGES ?? '').split(/\s+/u).filter(Boolean)) {
const data = readFileSync(file);
const meta = await sharp(data).metadata();
const base = process.memoryUsage().rss;
let peak = base;
const timer = setInterval(() => {
peak = Math.max(peak, process.memoryUsage().rss);
}, 10);
}, 5);
const started = Date.now();
await sharp(data, { failOn: 'warning', sequentialRead: true, limitInputPixels: 40_000_000 }).stats();
let verdict = 'ok';
try {
await sharp(data, { failOn: 'warning', sequentialRead: true, limitInputPixels: 40_000_000 })
.resize(64, 64, { fit: 'inside' })
.toBuffer();
} catch (error) {
verdict = `rejected: ${String(error.message).split('\n')[0]}`;
}
clearInterval(timer);
console.log(
`${meta.format} ${meta.width}x${meta.height}${meta.isProgressive ? ' progressive' : ''} bytes=${data.length} decode_ms=${Date.now() - started} peak_delta_rss_mib=${((peak - base) / 1048576).toFixed(1)}`,
`${meta.format} ${meta.width}x${meta.height} ${meta.channels}ch ${meta.depth}${meta.isProgressive ? ' progressive' : ''}${meta.isPalette ? ' palette' : ''} bytes=${data.length} decode_ms=${Date.now() - started} peak_delta_rss_mib=${((peak - base) / 1048576).toFixed(1)} ${verdict}`,
);
}
+28
View File
@@ -48,3 +48,31 @@ def noise_png(size: int, seed: int = 0) -> bytes:
raise ValueError("size is too small")
continue
return pad_png(png, size)
ADAM7 = [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)]
def worst_decode_png(size: int = 10_485_760, width: int = 8000, height: int = 5000) -> bytes:
"""The heaviest valid upload to decode: 40 MP RGBA 16-bit, Adam7-interlaced
(the decoder must hold the whole image), padded to exactly `size` bytes."""
compressor = zlib.compressobj(1)
parts = []
for x0, y0, dx, dy in ADAM7:
pass_width = (width - x0 + dx - 1) // dx if width > x0 else 0
pass_height = (height - y0 + dy - 1) // dy if height > y0 else 0
if pass_width == 0 or pass_height == 0:
continue
row = bytes(1 + pass_width * 8)
for _ in range(pass_height):
parts.append(compressor.compress(row))
parts.append(compressor.flush())
ihdr = struct.pack(">IIBBBBB", width, height, 16, 6, 0, 0, 1)
png = SIGNATURE + chunk(b"IHDR", ihdr) + chunk(b"IDAT", b"".join(parts)) + chunk(b"IEND", b"")
return pad_png(png, size)
if __name__ == "__main__":
import sys
sys.stdout.buffer.write(worst_decode_png())
+12
View File
@@ -19,5 +19,17 @@ for size in 131072 1048576 9961472; do
done
echo "== proxy size=9961472 c=20"
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
python3 scripts/lab4/pngkit.py > "$worst"
for mode in proxy direct; do
echo "== $mode worst-case image c=32"
python3 scripts/lab4/attachments_bench.py run --mode "$mode" --image "$worst" \
--concurrency 32 --uploads-per-worker 2 --repeat 3 \
--output "$out/$mode-worst-c32.json"
done
python3 scripts/lab4/attachments_bench.py summarize "$out"/*.json > evidence/lab4/summary.md
cat evidence/lab4/summary.md