"""Создаёт изолированный стенд, измеряет SQL, делает dump и restore; пишет реальные результаты.""" from datetime import datetime, timezone import json import os from pathlib import Path import platform import subprocess import sys import time from uuid import uuid4 ROOT = Path(__file__).resolve().parents[1] project = "lab2-research-" + uuid4().hex[:10] env = dict(os.environ, COMPOSE_PROJECT_NAME=project) command = ["docker", "compose", "--profile", "research"] def compose(*args, stdin=None, stdout=subprocess.PIPE): return subprocess.run([*command, *args], cwd=ROOT, env=env, input=stdin, stdout=stdout, stderr=subprocess.PIPE, check=True, timeout=600) def run_module(operation, stdin=None): result = compose("run", "--rm", "--no-deps", "-T", "-e", "LAB2_RESEARCH=1", "api", "python", "-m", "research.benchmark", operation, stdin=stdin) return json.loads(result.stdout) def wait_database(service): for _ in range(60): cid = compose("ps", "-q", service).stdout.decode().strip() state = subprocess.run(["docker", "inspect", "--format", "{{.State.Health.Status}}", cid], capture_output=True, text=True, check=True) if state.stdout.strip() == "healthy": return time.sleep(1) raise RuntimeError(service + " did not become healthy") def main(): evidence = ROOT / "evidence" evidence.mkdir(exist_ok=True) backups = ROOT / "backups" backups.mkdir(exist_ok=True) dump = backups / (project + ".dump") metadata = {"utc": datetime.now(timezone.utc).isoformat(), "project": project, "command": "./scripts/research.sh", "host": platform.system() + " " + platform.machine(), "commit": subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True).stdout.strip() + " + working tree", "postgres_limits": {"cpus": 1, "memory_mib": 512}, "api_runner_limits": {"cpus": 1, "memory_mib": 256}} started = time.perf_counter() try: print("Research project:", project, flush=True) compose("up", "-d", "postgres", "postgres-restore") wait_database("postgres") wait_database("postgres-restore") print("Measuring two indexes on the same 100000 messages, three repeats...", flush=True) result = run_module("benchmark") plans = result.pop("plans") (evidence / "query-plans.json").write_text(json.dumps(plans, ensure_ascii=False, indent=2)) source_id = compose("ps", "-q", "postgres").stdout.decode().strip() restore_id = compose("ps", "-q", "postgres-restore").stdout.decode().strip() assert source_id != restore_id for label, cid in (("source", source_id), ("restored", restore_id)): inspected = json.loads(subprocess.run(["docker", "inspect", cid], capture_output=True, check=True).stdout)[0] metadata[label] = {"container": cid, "image": inspected["Image"], "volumes": [m["Name"] for m in inspected["Mounts"] if m["Type"] == "volume"]} assert metadata["source"]["volumes"] != metadata["restored"]["volumes"] metadata["docker"] = subprocess.run(["docker", "info", "--format", "{{.ServerVersion}} {{.Architecture}} CPUs={{.NCPU}} RAM={{.MemTotal}}"], capture_output=True, text=True, check=True).stdout.strip() metadata["stats_after_queries"] = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{json .}}", source_id], capture_output=True, text=True, check=True).stdout.strip() backup_started = time.perf_counter() with dump.open("wb") as output: compose("exec", "-T", "postgres", "pg_dump", "-U", "messenger", "-d", "messenger", "--format=custom", stdout=output) backup_seconds = time.perf_counter() - backup_started marker = run_module("after-backup") restore_started = time.perf_counter() # Dump подаётся через stdin, без вывода данных или credentials в журнал. with dump.open("rb") as input_file: subprocess.run([*command, "exec", "-T", "postgres-restore", "pg_restore", "-U", "messenger", "-d", "messenger", "--exit-on-error", "--single-transaction", "--no-owner"], cwd=ROOT, env=env, stdin=input_file, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, check=True, timeout=300) restore_seconds = time.perf_counter() - restore_started verification_started = time.perf_counter() verification = run_module("verify", stdin=json.dumps(result["snapshot"]).encode()) result.update(metadata) result["backup"] = {"file": "backups/" + dump.name, "bytes": dump.stat().st_size, "seconds": backup_seconds} result["restore"] = {"seconds": restore_seconds, "verification_seconds": time.perf_counter() - verification_started, "verification": verification, "post_backup_insert": marker} result["total_seconds"] = time.perf_counter() - started (evidence / "research.json").write_text(json.dumps(result, ensure_ascii=False, indent=2)) print(json.dumps(result["summary"], ensure_ascii=False, indent=2), flush=True) print(f"PASS: backup {backup_seconds:.2f}s; restore {restore_seconds:.2f}s; 100000 messages, all hashes and 12 samples match", flush=True) print("PASS: restored into a different container and fresh volume; one post-backup commit is absent as expected", flush=True) print("Results: evidence/research.json, evidence/query-plans.json; dump stays outside Git in backups/", flush=True) finally: compose("down", "--volumes", "--timeout", "10") print("Removed only temporary research project:", project, flush=True) if __name__ == "__main__": main()