124 lines
4.0 KiB
Python
124 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
||
import concurrent.futures
|
||
import subprocess
|
||
import sys
|
||
import time
|
||
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):
|
||
"""Peak memory из cgroups v2 в KB; None если недоступно."""
|
||
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 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 слишком маленький')
|
||
|
||
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()
|
||
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) |