From 80965c5c037002a6d708e4d9ee8fd089ed3c3cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 24 Oct 2026 16:23:00 +0300 Subject: [PATCH] fix(student-tests):lab4 generate valid PNGs in memory test --- student-tests/test_memory.py | 44 ++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/student-tests/test_memory.py b/student-tests/test_memory.py index 07cd6e9..3f3a4cb 100644 --- a/student-tests/test_memory.py +++ b/student-tests/test_memory.py @@ -1,8 +1,12 @@ #!/usr/bin/env python3 +"""10 параллельных ~10-MiB uploads; пик RAM api-реплик из cgroups v2 и docker stats.""" import concurrent.futures +import os +import struct import subprocess import sys import time +import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] @@ -21,7 +25,6 @@ def api_containers(): def peak_kb(cid): - """Peak memory из cgroups v2 в KB; None если недоступно.""" try: out = subprocess.check_output( ['docker', 'exec', cid, 'cat', '/sys/fs/cgroup/memory.peak'], @@ -44,20 +47,43 @@ def current_mem_usage(): return '(docker stats недоступны)' +def make_valid_png(target_bytes): + """Валидный PNG ~target_bytes с шумом (level 0, без сжатия).""" + def chunk(typ, data): + return (struct.pack('>I', len(data)) + typ + data + + struct.pack('>I', zlib.crc32(typ + data) & 0xffffffff)) + + width = 1000 + overhead = 8 + 25 + 12 + 12 # sig + IHDR + IDAThdr + IEND + target_raw = target_bytes - overhead - 16 + height = max(1, target_raw // (1 + width * 3)) + + ihdr = struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0) + raw = bytearray() + for _ in range(height): + raw.append(0) + raw.extend(os.urandom(width * 3)) + + idat = zlib.compress(bytes(raw), 0) + return (b'\x89PNG\r\n\x1a\n' + + chunk(b'IHDR', ihdr) + + chunk(b'IDAT', idat) + + chunk(b'IEND', b'')) + + def main(): args = arguments('RAM-эксперимент: 10 параллельных upload по 10 MiB') c = Client(args.base_url, args.ca_file, timeout=120) alice, bob, eve, chat = c.bootstrap() - sample = (ROOT / 'tests/fixtures/sample.png').read_bytes() - big = (sample * 100)[:MAX_UPLOAD - 1] - check(len(big) >= MAX_UPLOAD - 1024, 'fixture слишком маленький') + big = make_valid_png(MAX_UPLOAD - 50000) + check(8_000_000 <= len(big) <= MAX_UPLOAD, + f'PNG не в диапазоне: {len(big)}') + print(f'размер одного файла: {len(big)} байт (~{len(big) / 1024 / 1024:.1f} MiB)') replicas = api_containers() check(len(replicas) >= 2, f'ожидалось >=2 api-реплики, найдено {len(replicas)}') - - print(f'api-реплики: {replicas}') - print(f'размер одного файла: {len(big)} байт (~{len(big) / 1024 / 1024:.1f} MiB)') + print(f'api-реплики: {[r[:12] for r in replicas]}') print() print('--- memory.peak ДО ---') for cid in replicas: @@ -115,9 +141,9 @@ def main(): f'{cid[:12]}: пик {p} KB > лимита {limit_mib * 1024} KB') if any_measured: - print(f'PASS: 10 × 10 MiB uploads, пик RAM каждой реплики <= {limit_mib} MiB') + print(f'PASS: 10 × ~10 MiB uploads, пик RAM каждой реплики <= {limit_mib} MiB') else: - print('PASS: 10 × 10 MiB uploads (cgroups v2 peak недоступен, см. docker stats)') + print('PASS: 10 × ~10 MiB uploads (cgroups v2 peak недоступен, см. docker stats)') if __name__ == '__main__':