Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ backend/media

backend/celerybeat-schedule

test/assets
test/data
test/graphs

.idea
.env
87 changes: 87 additions & 0 deletions test/calculate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import json
import re
import numpy as np
import matplotlib.pyplot as plt
from collections import defaultdict

# Путь к папке с файлами
DATA_DIR = "./data"
OUTPUT_DIR = "graphs"
os.makedirs(OUTPUT_DIR, exist_ok=True)

# Хранилища
data_by_worker_and_size = defaultdict(list)
summary_by_size = defaultdict(lambda: defaultdict(list))

# Регулярка для парсинга имени файла
pattern = re.compile(r"workers-(\d+)_size-(\w+)_count-(\d+)\.json")

# Чтение файлов
for file_name in os.listdir(DATA_DIR):
match = pattern.match(file_name)
if not match:
continue

workers = int(match.group(1))
size = match.group(2)
count = int(match.group(3))

with open(os.path.join(DATA_DIR, file_name), "r") as f:
data = json.load(f)
times = data["times"]

data_by_worker_and_size[(workers, size)].append((count, times))
mean_time = np.mean(times)
summary_by_size[size][workers].append((count, mean_time))

for (workers, size), batch_data in data_by_worker_and_size.items():
batch_data.sort(key=lambda x: x[0])

counts, mins, maxs, means, medians = [], [], [], [], []

for count, times in batch_data:
times_np = np.array(times)
counts.append(count)
mins.append(np.min(times_np))
maxs.append(np.max(times_np))
means.append(np.mean(times_np))
medians.append(np.median(times_np))

plt.figure(figsize=(10, 6))
plt.plot(counts, maxs, label="Максимум", color="red")
plt.plot(counts, mins, label="Минимум", color="blue")
plt.plot(counts, means, label="Среднее", color="green")
plt.plot(counts, medians, label="Медиана", color="orange")
plt.fill_between(counts, mins, maxs, color="gray", alpha=0.2, label="Диапазон (мин-макс)")

plt.xlabel("Количество запросов")
plt.ylabel("Время обработки (сек)")
plt.title(f"Размер: {size}, Воркеры: {workers}")
plt.legend()
plt.grid(True)
plt.tight_layout()

filename = f"size-{size}_workers-{workers}.png"
plt.savefig(os.path.join(OUTPUT_DIR, filename))
plt.close()

# Сводные графики: сравнение воркеров (только среднее время)
for size, workers_data in summary_by_size.items():
plt.figure(figsize=(10, 6))
for workers, datapoints in sorted(workers_data.items()):
datapoints.sort(key=lambda x: x[0])
counts = [x[0] for x in datapoints]
means = [x[1] for x in datapoints]
plt.plot(counts, means, label=f"{workers} воркеров")

plt.xlabel("Количесвто запросов)")
plt.ylabel("Среднее время обработки (сек)")
plt.title(f"Сравнение по воркерам — размер изображения: {size}")
plt.legend()
plt.grid(True)
plt.tight_layout()

filename = f"size-{size}_summary.png"
plt.savefig(os.path.join(OUTPUT_DIR, filename))
plt.close()
93 changes: 93 additions & 0 deletions test/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import requests
import json
import time
import argparse
import os

def get_correct_image_path(size):
for filename in os.listdir("./assets"):
if size in filename:
return f"./assets/{filename}"

print(f"Невозможно найти изображение, содержащее {size} в названии")
exit(0)

def get_request_ids(size, count):
print("[1] Отправление изображений")

request_ids = []

for i in range(count):
url_upload = 'http://localhost/api/upload/'
files = {'files': open(get_correct_image_path(size), 'rb')}
response_upload = requests.post(url_upload, files=files, allow_redirects=True)
request_id = json.loads(response_upload.text)['id']
request_ids.append(request_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше конечно не в цикле это делать, а через ThreadPool executor


print("[1] Изображения отправлены")

return request_ids

def get_exec_times(request_ids):
print("[2] Получение времени обработки изображений")

exec_times = []

for request_id in request_ids:
# Waiting till the end of image proccesing
url_status = f'http://localhost/api/status/{request_id}/'
sleep_duration = 0.0
sleep_addition = 0.5
while json.loads(requests.get(url_status).text)['status'] != 'ready':
sleep_duration += sleep_addition
time.sleep(sleep_duration)

# Get the processing time
url_exec_time = f'http://localhost/request/exec_time/{request_id}/'
response_exec_time = requests.get(url_exec_time)
exec_time = json.loads(response_exec_time.text)['seconds']
exec_times.append(exec_time)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и здесь тоже


print("[2] Информация по всем изображениям получена")

return exec_times

def save_data(count_workers, count, size, exec_times):
filename = f'./data/workers-{count_workers}_size-{size}_count-{count}.json'
dirname = os.path.dirname(filename)

if not os.path.exists(dirname):
os.mkdir(dirname)

data = {
'workers-count': count_workers,
'count': count,
'size': size,
'times': exec_times
}

with open(filename, 'w') as file:
json.dump(data, file)

print(f'[3] Время обработки изображений записано в {filename}\n')


def main():
parser = argparse.ArgumentParser(description='Получить время выполнения --count POST-запросов с изображениями размера --size в секундах')

parser.add_argument('--workers-count', required=True, type=int, help='Количество воркеров')
parser.add_argument('--count', required=True, type=int, help='Количество запросов')
parser.add_argument('--size', required=True, choices=['small', 'big'], help='Размер изображения')

args = parser.parse_args()

print(f"[0] {args.count} {args.size}".upper())

request_ids = get_request_ids(size=args.size, count=args.count)
exec_times = get_exec_times(request_ids=request_ids)

save_data(count_workers=args.workers_count, count=args.count, size=args.size, exec_times=exec_times)


if __name__ == '__main__':
main()
24 changes: 24 additions & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
altgraph==0.17.2
certifi==2025.1.31
charset-normalizer==3.4.1
contourpy==1.3.0
cycler==0.12.1
fonttools==4.57.0
future==0.18.2
idna==3.10
importlib_resources==6.5.2
kiwisolver==1.4.7
macholib==1.15.2
matplotlib==3.9.4
numpy==2.0.2
packaging==24.2
pillow==11.2.1
pip==21.2.4
pyparsing==3.2.3
python-dateutil==2.9.0.post0
requests==2.32.3
setuptools==58.0.4
six==1.15.0
urllib3==1.26.6
wheel==0.37.0
zipp==3.21.0