Files

200 lines
6.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Трек 5.1: цена масштабирования API при фиксированном и растущем бюджете CPU/RAM."""
import json
import math
import os
import subprocess
import sys
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check, run
EVIDENCE = ROOT / "evidence"
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/")
DURATION = float(os.environ.get("TRACK1_DURATION", "30"))
CONCURRENCY = int(os.environ.get("TRACK1_CONCURRENCY", "8"))
WARMUP = float(os.environ.get("TRACK1_WARMUP", "8"))
def compose(*args, timeout=180):
proc = subprocess.run(
["docker", "compose", "--env-file", ".env", *args],
cwd=str(ROOT),
capture_output=True,
text=True,
timeout=timeout,
encoding="utf-8",
errors="replace",
)
if proc.returncode != 0:
raise Failure("docker compose %s failed: %s" % (args[0], proc.stderr[-400:]))
return proc.stdout
def api_ids():
return [line.strip() for line in compose("ps", "-q", "api").splitlines() if line.strip()]
def wait_ready(timeout=90):
client = Client(BASE, timeout=3)
deadline = time.monotonic() + timeout
last = ""
while time.monotonic() < deadline:
try:
client.call("GET", "/health/ready", schema="Health")
return
except Failure as exc:
last = str(exc)
time.sleep(0.4)
raise Failure("ready wait failed: " + last)
def scale_api(n):
compose("up", "-d", "--no-deps", "--scale", "api=%d" % n, "api")
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
if len(api_ids()) >= n:
wait_ready()
return
time.sleep(0.5)
raise Failure("failed to scale api to %s" % n)
def apply_limits(cpus_each, memory_mb_each):
for container_id in api_ids():
args = ["docker", "update", "--cpus", "%.3f" % cpus_each, "--memory", "%dm" % memory_mb_each, container_id]
proc = subprocess.run(args, capture_output=True, text=True, timeout=30)
if proc.returncode != 0:
raise Failure("docker update failed")
def docker_stats():
proc = subprocess.run(
["docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"],
capture_output=True,
text=True,
timeout=30,
encoding="utf-8",
errors="replace",
)
rows = []
for line in proc.stdout.splitlines():
parts = line.split("\t")
if len(parts) == 3:
rows.append({"name": parts[0], "cpu": parts[1], "mem": parts[2]})
return rows
def run_load(duration, concurrency, warmup):
client = Client(BASE, timeout=8)
alice, _bob, _eve, chat = client.bootstrap()
if warmup > 0:
deadline = time.monotonic() + warmup
index = 0
while time.monotonic() < deadline:
client.message(alice, chat, "warmup-%s" % index)
index += 1
barrier = threading.Barrier(concurrency)
def worker(worker_id):
local = Client(BASE, timeout=5)
barrier.wait()
end = time.monotonic() + duration
latencies, errors = [], 0
index = 0
while time.monotonic() < end:
started = time.monotonic()
try:
local.message(alice, chat, "track1-%s-%s" % (worker_id, index))
latencies.append(time.monotonic() - started)
except Failure:
errors += 1
index += 1
return latencies, errors
started = time.monotonic()
with ThreadPoolExecutor(max_workers=concurrency) as pool:
results = list(pool.map(worker, range(concurrency)))
elapsed = time.monotonic() - started
latencies = sorted(item for values, _ in results for item in values)
errors = sum(n for _, n in results)
total = len(latencies) + errors
def percentile(p):
if not latencies:
return None
return round(1000 * latencies[max(0, math.ceil(p * len(latencies)) - 1)], 3)
return {
"elapsed_seconds": round(elapsed, 3),
"concurrency": concurrency,
"successful_requests": len(latencies),
"failed_requests": errors,
"error_ratio": round(errors / total, 6) if total else None,
"successful_rps": round(len(latencies) / elapsed, 3) if elapsed else None,
"successful_latency_ms": {"p50": percentile(0.50), "p95": percentile(0.95), "p99": percentile(0.99)},
"docker_stats": docker_stats(),
}
def measure(label, replicas, cpus_each, memory_mb_each):
scale_api(replicas)
apply_limits(cpus_each, memory_mb_each)
wait_ready()
time.sleep(2)
result = run_load(DURATION, CONCURRENCY, WARMUP)
result.update(
{
"label": label,
"replicas": replicas,
"cpus_each": cpus_each,
"memory_mb_each": memory_mb_each,
"total_cpu": round(replicas * cpus_each, 3),
"total_memory_mb": replicas * memory_mb_each,
}
)
print(json.dumps({k: result[k] for k in ("label", "replicas", "total_cpu", "successful_rps", "error_ratio")}, ensure_ascii=False))
return result
def main():
wait_ready()
fixed_budget = [
measure("fixed-1", 1, 1.00, 512),
measure("fixed-2", 2, 0.50, 256),
measure("fixed-4", 4, 0.25, 128),
]
growing = [
measure("grow-2x0.25", 2, 0.25, 128),
measure("grow-2x0.50", 2, 0.50, 256),
measure("grow-2x1.00", 2, 1.00, 512),
]
scale_api(2)
apply_limits(0.50, 256)
report = {
"date": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"hypothesis": "При фиксированном суммарном CPU/RAM добавление реплик перестаёт помогать, когда узким местом становится PostgreSQL/HAProxy, а не CPU процесса API.",
"profile": {
"duration_seconds": DURATION,
"concurrency": CONCURRENCY,
"warmup_seconds": WARMUP,
"workload": "closed-loop POST /messages through HAProxy",
},
"fixed_total_budget": fixed_budget,
"growing_resources": growing,
}
EVIDENCE.mkdir(exist_ok=True)
(EVIDENCE / "track1.json").write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
check(all(item["successful_requests"] > 0 for item in fixed_budget + growing), "a track1 series had no successes")
print("PASS: track1 series written to evidence/track1.json")
if __name__ == "__main__":
run(main)