Files
2026-11-06 18:54:28 +03:00

295 lines
10 KiB
Python

"""Трек 1: фиксированные/растущие ресурсы, насыщение PostgreSQL, улучшение.
Учебные данные добавляются; volumes не удаляются. По завершении восстанавливает
обычную конфигурацию. Сводки не содержат сессионных токенов.
"""
import argparse
import csv
import hashlib
import json
import os
import platform
import random
import subprocess
import time
from pathlib import Path
from benchmark import fixture, measure
from replicas import compose, containers, docker, wait_ready, wait_visible
ROOT = Path(__file__).resolve().parents[1]
DEST = ROOT / "evidence/lab3-research"
def cpu_stats():
result = {}
for service in ["api", "postgres", "redis", "lb"]:
for cid in compose("ps", "-q", service).split():
raw = docker("exec", cid, "cat", "/sys/fs/cgroup/cpu.stat")
memory = docker(
"exec", cid, "cat", "/sys/fs/cgroup/memory.current", "/sys/fs/cgroup/memory.peak"
).split()
result[cid] = {
"service": service,
"memory_current_bytes": int(memory[0]),
"memory_peak_bytes": int(memory[1]),
**{line.split()[0]: int(line.split()[1]) for line in raw.splitlines()},
}
return result
def pg_stats():
# Не выводим env/password и не меняем данные.
query = "SELECT json_build_object('commits',xact_commit,'rollbacks',xact_rollback,'blocks_read',blks_read,'blocks_hit',blks_hit,'temp_bytes',temp_bytes) FROM pg_stat_database WHERE datname=current_database()"
result = subprocess.check_output(
[
"docker",
"compose",
"--env-file",
os.environ.get("ENV_FILE", ".env"),
"exec",
"-T",
"postgres",
"sh",
"-c",
'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At',
],
input=query,
text=True,
cwd=ROOT,
)
return json.loads(result)
def configure(replicas, cpus, memory, db_cpus, pool=10):
os.environ.update(
API_REPLICAS=str(replicas),
API_CPUS=str(cpus),
API_MEMORY=memory,
POSTGRES_CPUS=str(db_cpus),
DATABASE_POOL_SIZE=str(pool),
LOG_LEVEL="WARNING",
)
compose("up", "-d", "--no-build", "--wait")
ids = containers()
assert len(ids) == replicas
for cid in ids:
wait_visible(wait_ready(cid))
def main():
global DEST
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--full",
action="store_true",
help="Полная матрица 1/2/4; без флага короткая проверка ресурса БД",
)
parser.add_argument("--duration", type=int, default=12)
parser.add_argument("--repeats", type=int, default=2)
parser.add_argument("--concurrency", type=int, default=32)
args = parser.parse_args()
if not args.full:
DEST = ROOT / "evidence/lab3-control"
args.duration = min(args.duration, 8)
args.repeats = 1
DEST.mkdir(parents=True, exist_ok=True)
machine = {
"host": platform.platform(),
"architecture": platform.machine(),
"docker": json.loads(docker("info", "--format", "{{json .}}")),
}
# Keep only hardware/runtime fields. Docker Info also has registry/user info.
machine["docker"] = {
k: machine["docker"].get(k)
for k in [
"ServerVersion",
"NCPU",
"MemTotal",
"KernelVersion",
"OperatingSystem",
"Architecture",
]
}
machine["application"] = {
"schema_revision": "006",
"message_storage": "UTF-8 bytea",
"source_sha256": {
str(path.relative_to(ROOT)): hashlib.sha256(path.read_bytes()).hexdigest()
for path in sorted((ROOT / "src").rglob("*.py"))
},
"configuration_sha256": {
name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest()
for name in ["compose.yaml", "infra/haproxy.cfg", "scripts/benchmark.py"]
},
}
machine["method"] = {
"warmup_seconds": 3,
"repeats": args.repeats,
"duration_seconds": args.duration,
"concurrency": args.concurrency,
"profile": "80% POST unique keys, 20% GET first page limit20, 20 seed messages; fresh chat per run",
"generator": "Python http.client, one persistent connection per thread, closed-loop, no retries",
"limits": "PostgreSQL 0.25 CPU/512MiB baseline, LB 0.5CPU/128MiB, Redis 0.25CPU/128MiB",
"randomization_seed": 20261106,
}
(DEST / "method.json").write_text(json.dumps(machine, ensure_ascii=False, indent=2) + "\n")
jobs = []
for repeat in range(args.repeats):
for count in [1, 2, 4]:
jobs += [
dict(
series="fixed",
replicas=count,
cpus=2 / count,
memory=f"{1024 // count}m",
db=0.25,
concurrency=args.concurrency,
repeat=repeat,
),
dict(
series="growing",
replicas=count,
cpus=1,
memory="256m",
db=0.25,
concurrency=args.concurrency,
repeat=repeat,
),
]
random.Random(20261106).shuffle(jobs)
# Additional load sweep establishes a plateau; the final series tests a
# hypothesis that allocating more CPU to PostgreSQL raises throughput.
for repeat in range(args.repeats):
for concurrency in [8, 64]:
jobs.append(
dict(
series="saturation",
replicas=4,
cpus=1,
memory="256m",
db=0.25,
concurrency=concurrency,
repeat=repeat,
)
)
jobs.append(
dict(
series="improved-db",
replicas=4,
cpus=1,
memory="256m",
db=1,
concurrency=args.concurrency,
repeat=repeat,
)
)
if not args.full:
jobs = [
job
for job in jobs
if job["replicas"] == 4
and job["series"] in {"growing", "improved-db"}
and job["concurrency"] == args.concurrency
]
results = []
try:
for index, job in enumerate(jobs):
print(f"[{index + 1}/{len(jobs)}] {job}", flush=True)
configure(job["replicas"], job["cpus"], job["memory"], job["db"])
actor, chat = fixture()
measure(actor, chat, 3, job["concurrency"])
before = cpu_stats()
pg_before = pg_stats()
began = time.monotonic()
report, _ = measure(actor, chat, args.duration, job["concurrency"])
elapsed = time.monotonic() - began
pg_after = pg_stats()
after = cpu_stats()
cpu = {}
for cid, end in after.items():
if cid not in before:
continue
delta = {k: end[k] - before[cid][k] for k in end if k != "service"}
cpu[cid[:12]] = {
"service": end["service"],
"memory_current_mib": round(end["memory_current_bytes"] / 1048576, 2),
"memory_peak_lifetime_mib": round(end["memory_peak_bytes"] / 1048576, 2),
"cpu_cores_used": round(delta["usage_usec"] / 1e6 / elapsed, 3),
"throttled_seconds": round(delta.get("throttled_usec", 0) / 1e6, 3),
"throttled_period_ratio": round(
delta.get("nr_throttled", 0) / max(1, delta.get("nr_periods", 0)), 3
),
}
row = {
"job": job,
"report": report,
"cpu": cpu,
"postgres_delta": {k: pg_after[k] - pg_before[k] for k in pg_before},
}
results.append(row)
(DEST / f"run-{index + 1:02}.json").write_text(
json.dumps(row, ensure_ascii=False, indent=2) + "\n"
)
print(
"RPS",
report["rps"],
"errors",
report["errors"],
"p95",
report["all_attempt_latency_ms"]["p95"],
flush=True,
)
(DEST / "results.json").write_text(json.dumps(results, ensure_ascii=False, indent=2) + "\n")
with (DEST / "summary.csv").open("w") as f:
fields = [
"series",
"replicas",
"cpus",
"memory",
"db",
"concurrency",
"repeat",
"rps",
"p95_ms",
"errors",
"postgres_cores",
]
writer = csv.DictWriter(f, fieldnames=fields)
writer.writeheader()
for row in results:
writer.writerow(
{
**row["job"],
"rps": row["report"]["rps"],
"p95_ms": row["report"]["all_attempt_latency_ms"]["p95"],
"errors": sum(row["report"]["errors"].values()),
"postgres_cores": sum(
c["cpu_cores_used"]
for c in row["cpu"].values()
if c["service"] == "postgres"
),
}
)
print(
"PASS: research series complete; inspect results before drawing conclusions", flush=True
)
finally:
for key in [
"API_REPLICAS",
"API_CPUS",
"API_MEMORY",
"POSTGRES_CPUS",
"DATABASE_POOL_SIZE",
"LOG_LEVEL",
]:
os.environ.pop(key, None)
compose("up", "-d", "--no-build", "--wait")
for cid in containers():
wait_visible(wait_ready(cid))
print("Default two-replica configuration restored", flush=True)
if __name__ == "__main__":
main()