forked from mtusi2026/mtusi2026lab5
ЛР5 — БВТ2302 — Никитина — на 5
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import sys
|
||||
import time
|
||||
import concurrent.futures
|
||||
sys.path.insert(0, 'tests')
|
||||
from client import Client
|
||||
|
||||
def wait_for_api(max_retries=20):
|
||||
c = Client('http://127.0.0.1:8080')
|
||||
for _ in range(max_retries):
|
||||
try:
|
||||
res = c.call('GET', '/health/ready')
|
||||
if res['status'] == 200:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(1)
|
||||
return False
|
||||
|
||||
def upload_and_wait(client, alice, chat):
|
||||
# Загружаем
|
||||
att, _ = client.attachment(alice, chat)
|
||||
att_id = att['id']
|
||||
|
||||
# Ждем
|
||||
while True:
|
||||
meta = client.call('GET', f'/api/v1/attachments/{att_id}', actor=alice, schema='Attachment')['json']
|
||||
if meta['status'] == 'ready':
|
||||
return True
|
||||
if meta['status'] == 'failed':
|
||||
return False
|
||||
time.sleep(0.1)
|
||||
|
||||
def main():
|
||||
print("Ожидаем готовности API...")
|
||||
if not wait_for_api():
|
||||
print("API не поднялся за 20 секунд.")
|
||||
return
|
||||
|
||||
# Создаем ОДНОГО пользователя и ОДИН чат
|
||||
c = Client('http://127.0.0.1:8080')
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
|
||||
NUM_IMAGES = 20
|
||||
print(f"Загружаем и обрабатываем {NUM_IMAGES} картинок в один чат...")
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
# Используем один общий Client для потоков, urllib потокобезопасен для вызовов
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
||||
futures = [executor.submit(upload_and_wait, c, alice, chat) for _ in range(NUM_IMAGES)]
|
||||
results = [f.result() for f in concurrent.futures.as_completed(futures)]
|
||||
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
|
||||
success = sum(1 for r in results if r)
|
||||
print(f"\n--- РЕЗУЛЬТАТ ---")
|
||||
print(f"Успешно обработано: {success} из {NUM_IMAGES}")
|
||||
print(f"Общее время: {duration:.2f} секунд")
|
||||
print(f"Throughput: {NUM_IMAGES / duration:.2f} картинок в секунду")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user