150 lines
4.8 KiB
Python
150 lines
4.8 KiB
Python
#!/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]
|
||
sys.path.insert(0, str(ROOT / 'tests'))
|
||
|
||
from client import Client, arguments, check, run # noqa: E402
|
||
|
||
MAX_UPLOAD = 10485760
|
||
|
||
|
||
def api_containers():
|
||
out = subprocess.check_output(
|
||
['docker', 'compose', 'ps', '-q', 'api'], text=True
|
||
)
|
||
return [c.strip() for c in out.splitlines() if c.strip()]
|
||
|
||
|
||
def peak_kb(cid):
|
||
try:
|
||
out = subprocess.check_output(
|
||
['docker', 'exec', cid, 'cat', '/sys/fs/cgroup/memory.peak'],
|
||
text=True, stderr=subprocess.DEVNULL,
|
||
)
|
||
return int(out.strip()) // 1024
|
||
except (subprocess.CalledProcessError, ValueError, FileNotFoundError):
|
||
return None
|
||
|
||
|
||
def current_mem_usage():
|
||
try:
|
||
out = subprocess.check_output(
|
||
['docker', 'stats', '--no-stream', '--format',
|
||
'{{.Name}} {{.MemUsage}}'],
|
||
text=True,
|
||
)
|
||
return '\n'.join(l for l in out.splitlines() if '-api-' in l)
|
||
except subprocess.CalledProcessError:
|
||
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()
|
||
|
||
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-реплики: {[r[:12] for r in replicas]}')
|
||
print()
|
||
print('--- memory.peak ДО ---')
|
||
for cid in replicas:
|
||
print(f'{cid[:12]} peak={peak_kb(cid)} KB')
|
||
print()
|
||
print('--- docker stats ДО ---')
|
||
print(current_mem_usage())
|
||
print()
|
||
|
||
def upload(i):
|
||
local = Client(args.base_url, args.ca_file, timeout=120)
|
||
return local.call(
|
||
'POST',
|
||
f'/api/v1/chats/{chat["id"]}/attachments?filename=big{i}.png',
|
||
201,
|
||
actor=alice,
|
||
raw=big,
|
||
headers={'Content-Type': 'image/png'},
|
||
)
|
||
|
||
started = time.monotonic()
|
||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
|
||
futures = [pool.submit(upload, i) for i in range(10)]
|
||
results = [f.result() for f in futures]
|
||
elapsed = time.monotonic() - started
|
||
|
||
print(f'--- {len(results)} uploads за {elapsed:.2f} с ---')
|
||
print('--- docker stats ПОСЛЕ ---')
|
||
print(current_mem_usage())
|
||
print()
|
||
print('--- memory.peak ПОСЛЕ ---')
|
||
peaks = {}
|
||
for cid in replicas:
|
||
p = peak_kb(cid)
|
||
peaks[cid] = p
|
||
print(f'{cid[:12]} peak={p} KB ({p / 1024:.1f} MiB)' if p else f'{cid[:12]} peak=N/A')
|
||
print()
|
||
|
||
limit_bytes = int(subprocess.check_output(
|
||
['docker', 'inspect', replicas[0], '--format', '{{.HostConfig.Memory}}'],
|
||
text=True,
|
||
).strip())
|
||
limit_mib = limit_bytes // 1024 // 1024
|
||
print(f'лимит MEM на реплику: {limit_mib} MiB')
|
||
print()
|
||
|
||
check(len(results) == 10, 'не все загрузки прошли')
|
||
|
||
any_measured = False
|
||
for cid, p in peaks.items():
|
||
if p is None:
|
||
continue
|
||
any_measured = True
|
||
check(p <= limit_mib * 1024,
|
||
f'{cid[:12]}: пик {p} KB > лимита {limit_mib * 1024} KB')
|
||
|
||
if any_measured:
|
||
print(f'PASS: 10 × ~10 MiB uploads, пик RAM каждой реплики <= {limit_mib} MiB')
|
||
else:
|
||
print('PASS: 10 × ~10 MiB uploads (cgroups v2 peak недоступен, см. docker stats)')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
run(main) |