forked from mtusi2026/mtusi2026lab4
119 lines
4.1 KiB
Python
119 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Эксперимент: 10 параллельных загрузок 5МБ файла. Снимает метрики docker stats."""
|
|
import sys
|
|
import uuid
|
|
import threading
|
|
import time
|
|
import subprocess
|
|
from pathlib import Path
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
from client import Client, Failure, run, check
|
|
|
|
# Автоматически находим имя первого контейнера API
|
|
def get_api_container():
|
|
try:
|
|
out = subprocess.check_output(
|
|
["docker", "ps", "--filter", "name=api", "--format", "{{.Names}}"],
|
|
text=True
|
|
).strip().split('\n')
|
|
# Берем первый попавшийся
|
|
for name in out:
|
|
if 'api' in name:
|
|
return name
|
|
except Exception:
|
|
pass
|
|
return "mtusi2026lab4-fork-api-1" # fallback
|
|
|
|
API_CONTAINER = get_api_container()
|
|
FIXTURE_PATH = Path(__file__).parent / "fixtures" / "Snake_River_(5mb).jpg"
|
|
|
|
def get_api_memory():
|
|
"""Возвращает RSS контейнера API в MB."""
|
|
try:
|
|
out = subprocess.check_output(
|
|
["docker", "stats", API_CONTAINER, "--no-stream", "--format", "{{.MemUsage}}"],
|
|
text=True, stderr=subprocess.DEVNULL
|
|
).strip()
|
|
mem_str = out.split('/')[0].strip()
|
|
if 'MiB' in mem_str:
|
|
return float(mem_str.replace('MiB', ''))
|
|
if 'GiB' in mem_str:
|
|
return float(mem_str.replace('GiB', '')) * 1024
|
|
if 'KiB' in mem_str:
|
|
return float(mem_str.replace('KiB', '')) / 1024
|
|
return float(mem_str.replace('B', '')) / (1024*1024)
|
|
except Exception:
|
|
return 0.0
|
|
|
|
def main():
|
|
if not FIXTURE_PATH.exists():
|
|
raise FileNotFoundError(f"Файл {FIXTURE_PATH} не найден. Положите картинку в student-tests/fixtures/")
|
|
|
|
c = Client()
|
|
alice, _, _, chat = c.bootstrap()
|
|
content = FIXTURE_PATH.read_bytes()
|
|
print(f"File size: {len(content) / 1024 / 1024:.2f} MB")
|
|
print(f"Target API container: {API_CONTAINER}")
|
|
|
|
peak_mem = 0.0
|
|
stop_monitor = threading.Event()
|
|
|
|
def monitor():
|
|
nonlocal peak_mem
|
|
while not stop_monitor.is_set():
|
|
mem = get_api_memory()
|
|
if mem > peak_mem:
|
|
peak_mem = mem
|
|
time.sleep(0.1)
|
|
|
|
mon_thread = threading.Thread(target=monitor)
|
|
mon_thread.start()
|
|
start = time.monotonic()
|
|
|
|
def upload_one(_):
|
|
local_c = Client()
|
|
local_c.timeout = 60
|
|
fname = f"test_{uuid.uuid4().hex[:8]}.jpg"
|
|
path = f"/api/v1/chats/{chat['id']}/attachments?filename={fname}"
|
|
|
|
# Один ретрай при 503 (S3/БД могут быть перегружены при 10 потоках)
|
|
for attempt in range(2):
|
|
try:
|
|
res = local_c.call('POST', path, 201, actor=alice, raw=content, headers={'Content-Type': 'image/jpeg'})
|
|
return res['status']
|
|
except Failure as e:
|
|
if 'received 503' in str(e) and attempt == 0:
|
|
time.sleep(0.5)
|
|
continue
|
|
raise
|
|
|
|
try:
|
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
|
results = list(pool.map(upload_one, range(10)))
|
|
except Failure as e:
|
|
print(f"FAIL: {e}")
|
|
stop_monitor.set()
|
|
mon_thread.join()
|
|
return
|
|
|
|
stop_monitor.set()
|
|
mon_thread.join()
|
|
|
|
elapsed = time.monotonic() - start
|
|
print(f"All {len(results)} uploads completed in {elapsed:.2f}s")
|
|
print(f"Peak API Memory (RSS): {peak_mem:.2f} MB")
|
|
|
|
Path("evidence").mkdir(exist_ok=True)
|
|
with open("evidence/mem_report.txt", "w") as f:
|
|
f.write(f"Concurrent uploads: 10\n")
|
|
f.write(f"File size: {len(content) / 1024 / 1024:.2f} MB\n")
|
|
f.write(f"Elapsed: {elapsed:.2f}s\n")
|
|
f.write(f"Peak API RSS: {peak_mem:.2f} MB\n")
|
|
|
|
check(peak_mem < 500, "Memory exceeded 500MB limit!")
|
|
print("PASS: Memory limits respected.")
|
|
|
|
if __name__ == "__main__":
|
|
run(main) |