forked from mtusi2026/mtusi2026lab4
lab4: S3, MinIO, reconciler, трек 2, README
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import io
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
import requests
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
|
||||
BASE_URL = "http://localhost:8080"
|
||||
CONCURRENCY = 10
|
||||
|
||||
|
||||
def make_png(width, height):
|
||||
img = Image.frombytes('RGB', (width, height), os.urandom(width * height * 3))
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format='PNG', optimize=False)
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def register_and_login(username):
|
||||
requests.post(f"{BASE_URL}/api/v1/users", json={
|
||||
"username": username, "display_name": "Test", "password": "valid_password_123"
|
||||
})
|
||||
resp = requests.post(f"{BASE_URL}/api/v1/auth/sessions",
|
||||
auth=(username, "valid_password_123"))
|
||||
return resp.json()["token"]
|
||||
|
||||
|
||||
def create_chat(token):
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
me = requests.get(f"{BASE_URL}/api/v1/users/me", headers=headers).json()
|
||||
resp = requests.post(f"{BASE_URL}/api/v1/chats", headers=headers, json={
|
||||
"title": "RAM Test", "member_ids": [me["id"]]
|
||||
})
|
||||
return resp.json()["id"]
|
||||
|
||||
|
||||
def upload(token, chat_id, content, filename):
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "image/png"}
|
||||
start = time.monotonic()
|
||||
resp = requests.post(
|
||||
f"{BASE_URL}/api/v1/chats/{chat_id}/attachments?filename={filename}",
|
||||
headers=headers, data=content, timeout=60,
|
||||
)
|
||||
elapsed = time.monotonic() - start
|
||||
return resp.status_code, elapsed, len(content)
|
||||
|
||||
|
||||
def main():
|
||||
print(f"RAM Experiment: {CONCURRENCY} concurrent uploads, ~5 MB each")
|
||||
token = register_and_login("ram_tester")
|
||||
chat_id = create_chat(token)
|
||||
print(f"Chat ID: {chat_id}")
|
||||
|
||||
print(f"Генерируем {CONCURRENCY} картинок (шум, ~5 МБ)...")
|
||||
contents = [make_png(1400 + i * 20, 1400 + i * 20) for i in range(CONCURRENCY)]
|
||||
sizes = [len(c) / (1024 * 1024) for c in contents]
|
||||
print(f"Размеры: min={min(sizes):.2f}, max={max(sizes):.2f}, avg={sum(sizes)/len(sizes):.2f} МБ")
|
||||
print(f"Суммарно: {sum(sizes):.2f} МБ")
|
||||
|
||||
print(f"\nЗапускаем {CONCURRENCY} одновременных загрузок...")
|
||||
start = time.monotonic()
|
||||
results, errors = [], []
|
||||
|
||||
with ThreadPoolExecutor(max_workers=CONCURRENCY) as pool:
|
||||
futures = [
|
||||
pool.submit(upload, token, chat_id, contents[i], f"ram-{i}-{uuid.uuid4().hex[:8]}.png")
|
||||
for i in range(CONCURRENCY)
|
||||
]
|
||||
for f in futures:
|
||||
try:
|
||||
results.append(f.result())
|
||||
except Exception as e:
|
||||
errors.append(str(e))
|
||||
|
||||
total = time.monotonic() - start
|
||||
print(f"\n=== Результаты ===")
|
||||
print(f"Всего: {len(results) + len(errors)}")
|
||||
print(f"Успешных (201): {sum(1 for r in results if r[0] == 201)}")
|
||||
print(f"Ошибок: {len(errors)}")
|
||||
if results:
|
||||
lat = [r[1] for r in results]
|
||||
print(f"Latency: min={min(lat):.2f}s, max={max(lat):.2f}s, avg={sum(lat)/len(lat):.2f}s")
|
||||
print(f"Общее время: {total:.2f}s")
|
||||
if errors:
|
||||
print(f"Ошибки: {errors[:5]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user