Files
mtusi2026lab5/backpressure_test.py

260 lines
6.3 KiB
Python

import csv
import re
import subprocess
import time
from pathlib import Path
from benchmark_rss import command, start_worker, remove_test_worker
RESULTS_DIR = Path("benchmark_results")
RESULTS_DIR.mkdir(exist_ok=True)
QUEUE_NAME = "image_tasks"
COUNT_PER_SIZE = 40
TOTAL = COUNT_PER_SIZE * 3
def queue_stats():
try:
result = subprocess.run(
[
"docker", "exec",
"mtusi_messenger_rabbitmq",
"rabbitmqctl", "-q", "list_queues",
"name",
"messages_ready",
"messages_unacknowledged",
],
capture_output=True,
text=True,
timeout=20,
)
except subprocess.TimeoutExpired as exc:
raise RuntimeError(
"RabbitMQ не ответил на запрос статистики"
) from exc
if result.returncode != 0:
raise RuntimeError(
"Не удалось получить статистику RabbitMQ:\n"
+ result.stderr
)
for line in result.stdout.splitlines():
parts = line.split()
if len(parts) >= 3 and parts[0] == QUEUE_NAME:
return int(parts[1]), int(parts[2])
raise RuntimeError(
f"Очередь {QUEUE_NAME} не найдена:\n"
+ result.stdout
)
def run_test():
print("Останавливаем обычный Worker...")
command(["docker", "compose", "stop", "worker"])
print("Запускаем Worker 1/1...")
start_worker(1, 1)
initial_ready, initial_unacked = queue_stats()
print(
f"Начальная очередь: ready={initial_ready}, "
f"unacked={initial_unacked}"
)
if initial_ready or initial_unacked:
raise RuntimeError(
"Очередь не пустая. Для чистого эксперимента "
"сначала нужно завершить предыдущие задания."
)
print(f"\nЗапускаем нагрузку: {TOTAL} фотографий")
log_path = RESULTS_DIR / "backpressure_load.log"
samples = []
with log_path.open("w") as log_file:
process = subprocess.Popen(
[
"docker", "compose", "exec",
"-T",
"-w", "/tmp",
"-e", "PYTHONPATH=/app",
"app1",
"python", "-m", "app.load_test",
"--count", str(COUNT_PER_SIZE),
"--timeout", "600",
],
stdout=log_file,
stderr=subprocess.STDOUT,
)
started = time.monotonic()
try:
while process.poll() is None:
ready, unacked = queue_stats()
elapsed = time.monotonic() - started
samples.append({
"elapsed_s": round(elapsed, 2),
"queue_ready": ready,
"unacked": unacked,
"total_pending": ready + unacked,
})
print(
f"[QUEUE] {elapsed:6.1f}s | "
f"ready={ready:3} | "
f"unacked={unacked} | "
f"pending={ready + unacked:3}",
flush=True,
)
time.sleep(1)
return_code = process.wait()
except BaseException:
process.terminate()
try:
process.wait(timeout=10)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
raise
output = log_path.read_text()
if return_code != 0:
print(output)
raise RuntimeError("Нагрузочный тест завершился с ошибкой")
print("\n" + output)
ready_match = re.search(
r"^Ready:\s*(\d+)",
output,
re.MULTILINE,
)
failed_match = re.search(
r"^Failed:\s*(\d+)",
output,
re.MULTILINE,
)
completed = (
int(ready_match.group(1))
if ready_match else -1
)
failed = (
int(failed_match.group(1))
if failed_match else -1
)
print("Проверяем окончательное состояние очереди...")
final_ready = None
final_unacked = None
for _ in range(15):
final_ready, final_unacked = queue_stats()
if final_ready == 0 and final_unacked == 0:
break
time.sleep(1)
peak_ready = max(
(row["queue_ready"] for row in samples),
default=0,
)
peak_unacked = max(
(row["unacked"] for row in samples),
default=0,
)
csv_path = RESULTS_DIR / "backpressure_queue.csv"
with csv_path.open("w", newline="") as file:
writer = csv.DictWriter(
file,
fieldnames=[
"elapsed_s",
"queue_ready",
"unacked",
"total_pending",
],
)
writer.writeheader()
writer.writerows(samples)
print("\n" + "=" * 60)
print("BACKPRESSURE RESULTS")
print("=" * 60)
print(f"Total jobs: {TOTAL}")
print(f"Ready: {completed}")
print(f"Failed: {failed}")
print(f"Peak queued: {peak_ready}")
print(f"Peak unacked: {peak_unacked}")
print(f"Final queued: {final_ready}")
print(f"Final unacked: {final_unacked}")
backlog_observed = peak_ready > 0
prefetch_respected = peak_unacked <= 1
recovered = (
completed == TOTAL
and failed == 0
and final_ready == 0
and final_unacked == 0
)
print(
"Backlog observed:",
"YES" if backlog_observed else "NO",
)
print(
"Prefetch limit respected:",
"YES" if prefetch_respected else "NO",
)
print(
"Recovery:",
"PASSED" if recovered else "FAILED",
)
print(f"CSV: {csv_path}")
print("=" * 60)
def main():
try:
run_test()
finally:
print("\nВосстанавливаем обычный Worker...")
remove_test_worker()
result = command(
["docker", "compose", "up", "-d", "worker"],
check=False,
)
if result.returncode != 0:
print(result.stderr)
if __name__ == "__main__":
main()