forked from mtusi2026/mtusi2026lab4
189 lines
6.5 KiB
Python
189 lines
6.5 KiB
Python
"""Эксперимент: 10 одновременных загрузок 10 MiB. Замер пика RAM API.
|
||
|
||
Запуск:
|
||
python3 test_concurrent_uploads.py --base-url http://localhost:8080
|
||
|
||
Требует docker CLI для `docker stats`.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import base64
|
||
import io
|
||
import json
|
||
import subprocess
|
||
import sys
|
||
import threading
|
||
import time
|
||
import urllib.error
|
||
import urllib.request
|
||
import uuid
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
|
||
BASE = (
|
||
sys.argv[sys.argv.index("--base-url") + 1]
|
||
if "--base-url" in sys.argv
|
||
else "http://localhost:8080"
|
||
)
|
||
CONCURRENCY = 10
|
||
SIZE_MB = 10
|
||
API_CONTAINER = "mtusi2026lab4-api-1"
|
||
|
||
|
||
def call(method, path, headers=None, body=None, raw=None, timeout=60):
|
||
data = None
|
||
h = dict(headers or {})
|
||
if body is not None:
|
||
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||
h["Content-Type"] = "application/json"
|
||
elif raw is not None:
|
||
data = raw
|
||
req = urllib.request.Request(BASE + path, data=data, headers=h, method=method)
|
||
try:
|
||
resp = urllib.request.urlopen(req, timeout=timeout)
|
||
payload = resp.read()
|
||
return resp.status, json.loads(payload) if payload else None
|
||
except urllib.error.HTTPError as e:
|
||
payload = e.read()
|
||
try:
|
||
return e.code, json.loads(payload) if payload else None
|
||
except Exception:
|
||
return e.code, None
|
||
|
||
|
||
def make_user(label):
|
||
username = f"ram_{label}_{uuid.uuid4().hex[:10]}"
|
||
password = "ram-test-pass-1234567890"
|
||
code, user = call("POST", "/api/v1/users", body={
|
||
"username": username, "display_name": f"RAM {label}", "password": password,
|
||
})
|
||
assert code == 201, f"register failed: {code} {user}"
|
||
cred = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||
code, session = call("POST", "/api/v1/auth/sessions",
|
||
headers={"Authorization": "Basic " + cred})
|
||
assert code == 201, f"login failed: {code}"
|
||
code, chat = call("POST", "/api/v1/chats",
|
||
headers={"Authorization": "Bearer " + session["token"]},
|
||
body={"title": "RAM", "member_ids": [user["id"]]})
|
||
assert code == 201, f"chat failed: {code}"
|
||
return {"token": session["token"], "chat_id": chat["id"]}
|
||
|
||
|
||
def measure_ram_mb(container):
|
||
"""Возвращает текущее RSS контейнера в MiB."""
|
||
try:
|
||
result = subprocess.run(
|
||
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container],
|
||
capture_output=True, text=True, timeout=5,
|
||
)
|
||
if result.returncode != 0:
|
||
return None
|
||
# "123.4MiB / 256MiB"
|
||
mem = result.stdout.strip().split("/")[0].strip()
|
||
if "MiB" in mem:
|
||
return float(mem.replace("MiB", "").strip())
|
||
if "GiB" in mem:
|
||
return float(mem.replace("GiB", "").strip()) * 1024
|
||
if "KiB" in mem:
|
||
return float(mem.replace("KiB", "").strip()) / 1024
|
||
return None
|
||
except Exception:
|
||
return None
|
||
|
||
|
||
def make_png_of_size(size_bytes):
|
||
"""Создаёт PNG ~size_bytes (не гарантированно точно, но близко)."""
|
||
from PIL import Image
|
||
# Создаём RGB изображение, сжимаем в PNG
|
||
# ~size_bytes: подбираем размеры
|
||
# 1 пиксель = ~3 байта + заголовки. Для 10 MiB — ~3500x1000
|
||
side = int((size_bytes / 3) ** 0.5)
|
||
img = Image.new("RGB", (side, side), color=(200, 100, 50))
|
||
buf = io.BytesIO()
|
||
img.save(buf, format="PNG", compress_level=0) # без сжатия — близко к size
|
||
data = buf.getvalue()
|
||
# Подгоняем: если больше — обрезаем? Нет, PNG с фиксированным размером.
|
||
return data
|
||
|
||
|
||
def main():
|
||
print(f"=== concurrent uploads: {CONCURRENCY} × {SIZE_MB} MiB ===")
|
||
|
||
user = make_user("user")
|
||
png = make_png_of_size(SIZE_MB * 1024 * 1024)
|
||
print(f"fixture size: {len(png)} bytes ({len(png) / 1024 / 1024:.2f} MiB)")
|
||
|
||
# Baseline RAM
|
||
baseline = measure_ram_mb(API_CONTAINER)
|
||
print(f"baseline RAM: {baseline:.1f} MiB" if baseline else "baseline RAM: unknown")
|
||
|
||
# Мониторинг RAM в фоне
|
||
peak = baseline or 0.0
|
||
stop = threading.Event()
|
||
samples = []
|
||
|
||
def monitor():
|
||
nonlocal peak
|
||
while not stop.is_set():
|
||
ram = measure_ram_mb(API_CONTAINER)
|
||
if ram is not None:
|
||
samples.append(ram)
|
||
peak = max(peak, ram)
|
||
time.sleep(0.2)
|
||
|
||
monitor_thread = threading.Thread(target=monitor, daemon=True)
|
||
monitor_thread.start()
|
||
|
||
# Запуск 10 одновременных загрузок
|
||
results = []
|
||
results_lock = threading.Lock()
|
||
barrier = threading.Barrier(CONCURRENCY)
|
||
|
||
def worker(i):
|
||
barrier.wait()
|
||
code, body = call(
|
||
"POST",
|
||
f"/api/v1/chats/{user['chat_id']}/attachments?filename=ram-{i}.png",
|
||
headers={
|
||
"Authorization": "Bearer " + user["token"],
|
||
"Content-Type": "image/png",
|
||
},
|
||
raw=png,
|
||
timeout=120,
|
||
)
|
||
with results_lock:
|
||
results.append((code, body))
|
||
|
||
started = time.monotonic()
|
||
with ThreadPoolExecutor(max_workers=CONCURRENCY) as pool:
|
||
list(pool.map(worker, range(CONCURRENCY)))
|
||
elapsed = time.monotonic() - started
|
||
|
||
stop.set()
|
||
monitor_thread.join(timeout=2)
|
||
|
||
codes = {}
|
||
for code, _ in results:
|
||
codes[code] = codes.get(code, 0) + 1
|
||
|
||
print(f"\nResults:")
|
||
print(f" duration: {elapsed:.2f}s")
|
||
print(f" status codes: {codes}")
|
||
print(f" peak RAM: {peak:.1f} MiB (baseline {baseline:.1f} MiB)")
|
||
print(f" delta: {peak - baseline:.1f} MiB")
|
||
print(f" theoretical max (if no streaming): {CONCURRENCY * SIZE_MB} MiB")
|
||
print(f" samples: {len(samples)}, min={min(samples):.1f}, max={max(samples):.1f}")
|
||
|
||
assert all(c == 201 for c, _ in results), f"not all uploads succeeded: {codes}"
|
||
|
||
# Ожидаем: delta < CONCURRENCY * SIZE_MB (иначе нет потоковой обработки)
|
||
theoretical = CONCURRENCY * SIZE_MB
|
||
if peak - baseline < theoretical * 0.5:
|
||
print(f"\nPASS: streaming works — peak delta {peak - baseline:.1f} MiB "
|
||
f"< 50% of theoretical {theoretical} MiB")
|
||
else:
|
||
print(f"\nWARN: peak delta {peak - baseline:.1f} MiB "
|
||
f">= 50% of theoretical {theoretical} MiB — check streaming")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main() |