Добавить PostgreSQL, постоянные сессии, кэш и проверки ЛР2
This commit is contained in:
+143
@@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Isolated SQL index study and real pg_dump/pg_restore round trip."""
|
||||
|
||||
import argparse
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import statistics
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def command(args, *, data=None, timeout=180):
|
||||
result = subprocess.run(args, input=data, capture_output=True, cwd=ROOT, timeout=timeout)
|
||||
if result.returncode:
|
||||
# Never echo connection strings or arbitrary container stderr into evidence.
|
||||
raise RuntimeError("Research subprocess failed: " + args[0])
|
||||
return result.stdout
|
||||
|
||||
|
||||
def worker(mode, original, restored):
|
||||
code = (ROOT / "research/worker.py").read_bytes()
|
||||
raw = command(
|
||||
["docker", "compose", "exec", "-T", "api", "python", "-", mode, original, restored],
|
||||
data=code,
|
||||
)
|
||||
return json.loads(raw)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--output", default="evidence/query-study.json")
|
||||
args = parser.parse_args()
|
||||
output = ROOT / args.output
|
||||
if output.exists():
|
||||
raise SystemExit("Choose a new evidence output; existing results are immutable")
|
||||
suffix = uuid.uuid4().hex[:12]
|
||||
original, restored = "research_" + suffix, "restored_" + suffix
|
||||
result = {
|
||||
"started_utc": dt.datetime.now(dt.UTC).isoformat(),
|
||||
"databases": {"original": original, "restored": restored},
|
||||
"command": "uv run --locked python research/run.py --output " + args.output,
|
||||
"method": "Single PostgreSQL container, warmed SQL reads; not HTTP latency",
|
||||
"repeats_per_index": 3,
|
||||
"read_trials_per_phase": 7,
|
||||
"warmup_queries_per_phase": 5,
|
||||
}
|
||||
try:
|
||||
result["source_commit"] = command(["git", "rev-parse", "HEAD"]).decode().strip()
|
||||
result["source_worktree_dirty"] = bool(command(["git", "status", "--porcelain"]))
|
||||
cid = command(["docker", "compose", "ps", "-q", "postgres"]).decode().strip()
|
||||
inspect = json.loads(command(["docker", "inspect", cid]))[0]
|
||||
result["postgres_container"] = {
|
||||
"id": cid,
|
||||
"image_id": inspect["Image"],
|
||||
"nano_cpus": inspect["HostConfig"]["NanoCpus"],
|
||||
"memory_limit_bytes": inspect["HostConfig"]["Memory"],
|
||||
}
|
||||
result["study"] = worker("measure", original, restored)
|
||||
print("Measured both indexes on 100000 messages; preparing logical backup.", flush=True)
|
||||
start = time.perf_counter()
|
||||
dump = command(
|
||||
[
|
||||
"docker",
|
||||
"compose",
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"pg_dump",
|
||||
"-U",
|
||||
"messenger",
|
||||
"-d",
|
||||
original,
|
||||
"-Fc",
|
||||
"--no-owner",
|
||||
"--no-acl",
|
||||
]
|
||||
)
|
||||
result["backup"] = {
|
||||
"seconds": time.perf_counter() - start,
|
||||
"bytes": len(dump),
|
||||
"sha256": hashlib.sha256(dump).hexdigest(),
|
||||
}
|
||||
worker("create_restore", original, restored)
|
||||
start = time.perf_counter()
|
||||
command(
|
||||
[
|
||||
"docker",
|
||||
"compose",
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"pg_restore",
|
||||
"-U",
|
||||
"messenger",
|
||||
"-d",
|
||||
restored,
|
||||
"--no-owner",
|
||||
"--no-acl",
|
||||
"--exit-on-error",
|
||||
],
|
||||
data=dump,
|
||||
)
|
||||
result["restore"] = {"seconds": time.perf_counter() - start}
|
||||
result["verification"] = worker("verify", original, restored)
|
||||
summary = {}
|
||||
for variant in ("chat_only", "composite"):
|
||||
phases = [p for p in result["study"]["phases"] if p["variant"] == variant]
|
||||
summary[variant] = {
|
||||
"sql_execution_median_ms": statistics.median(
|
||||
plan["Execution Time"] for phase in phases for plan in phase["plans"]
|
||||
),
|
||||
"insert_5000_median_seconds": statistics.median(
|
||||
p["insert_commit_seconds"] for p in phases
|
||||
),
|
||||
"index_bytes": statistics.median(p["index_bytes_before_write"] for p in phases),
|
||||
"insert_wal_median_bytes": statistics.median(p["insert_wal_bytes"] for p in phases),
|
||||
}
|
||||
result["summary"] = summary
|
||||
result["status"] = "PASS"
|
||||
finally:
|
||||
result["cleanup"] = worker("cleanup", original, restored)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
output.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
"status": result["status"],
|
||||
"summary": result["summary"],
|
||||
"backup": result["backup"],
|
||||
"restore": result["restore"],
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user