import hashlib import json import os import secrets import subprocess import uuid from pathlib import Path HERE = Path(__file__).resolve().parent ROOT = HERE.parents[1] PRIVATE = ROOT / ".memory/benchmark" EVIDENCE = ROOT / "evidence/grade5" PROJECT = "highload-lab3-bench" BASE = "http://127.0.0.1:18080" STATS = "http://127.0.0.1:18404/stats;csv" CONFIGS = { "A1": {"replicas": 1, "cpu": 1, "memory": "256m", "pool": 12}, "A2": {"replicas": 2, "cpu": 0.5, "memory": "128m", "pool": 6}, "A4": {"replicas": 4, "cpu": 0.25, "memory": "64m", "pool": 3}, "B1": {"replicas": 1, "cpu": 0.5, "memory": "128m", "pool": 12}, "B2": {"replicas": 2, "cpu": 0.5, "memory": "128m", "pool": 6}, "B4": {"replicas": 4, "cpu": 0.5, "memory": "128m", "pool": 3}, } def shell(args, *, input=None): result = subprocess.run( args, check=False, cwd=ROOT, input=input, text=True, capture_output=True, ) if result.returncode: # Do not expose command/environment/stdin containing credentials. raise RuntimeError( f"Command {args[0]} failed (exit {result.returncode}); inspect local setup logs" ) return result.stdout.strip() def prepare_private(): PRIVATE.mkdir(parents=True, exist_ok=True) path = PRIVATE / "environment" if not path.exists(): path.write_text("BENCH_PASSWORD=" + secrets.token_hex(24) + "\n") path.chmod(0o600) def compose(args, config=None): env = os.environ.copy() env.update( { "BENCH_CPU": ".5", "BENCH_MEMORY": "128m", "BENCH_REPLICAS": "2", "BENCH_POOL": "6", "BENCH_GOMAXPROCS": "", } ) if config: env.update( { "BENCH_CPU": str(config["cpu"]), "BENCH_MEMORY": config["memory"], "BENCH_REPLICAS": str(config["replicas"]), "BENCH_POOL": str(config["pool"]), "BENCH_IMAGE": config.get("image", "highload-lab3-bench-api:baseline"), } ) env["BENCH_GOMAXPROCS"] = str(config.get("gomaxprocs", "")) result = subprocess.run( [ "docker", "compose", "--project-name", PROJECT, "--project-directory", str(ROOT), "--env-file", str(PRIVATE / "environment"), "-f", str(HERE / "compose.yml"), *args, ], check=False, cwd=ROOT, env=env, text=True, capture_output=True, ) if result.returncode: PRIVATE.joinpath("last-compose-error.log").write_text(result.stderr) raise RuntimeError( "Benchmark Compose failed; see private last-compose-error.log" ) return result.stdout.strip() def sql(query): return compose_psql(query) def compose_psql(query): db = compose(["ps", "-q", "db"]) return shell( [ "docker", "exec", "-i", db, "psql", "-X", "-q", "-t", "-A", "-v", "ON_ERROR_STOP=1", "-U", "benchmark", "-d", "benchmark", ], input=query, ) def md5uuid(value): return str(uuid.UUID(hashlib.md5(value.encode()).hexdigest())) def restore_fixture(): sql(HERE.joinpath("fixture.sql").read_text()) if sql("SELECT count(*) FROM messages;") != "10000": raise RuntimeError("Fixture restoration did not produce 10000 initial messages") redis = compose(["ps", "-q", "redis"]) # Only the benchmark project's Redis is eligible for clearing sessions. shell(["docker", "exec", redis, "redis-cli", "FLUSHDB"]) actors = [] protocol = bytearray() for i in range(200): token = secrets.token_hex(32) user_id = md5uuid("bench-user-" + str(i)) key = "messenger:session:" + hashlib.sha256(token.encode()).hexdigest() parts = ["SET", key, user_id, "EX", "86400"] protocol.extend(f"*{len(parts)}\r\n".encode()) for part in parts: data = part.encode() protocol.extend(f"${len(data)}\r\n".encode() + data + b"\r\n") actors.append({"id": user_id, "token": token}) result = subprocess.run( ["docker", "exec", "-i", redis, "redis-cli", "--pipe"], check=False, input=bytes(protocol), capture_output=True, ) if result.returncode or b"errors: 0" not in result.stdout: raise RuntimeError("Session fixture failed") return actors def inspect_public(container): return json.loads( shell( [ "docker", "inspect", "--format", '{"id":{{json .Id}},"instance":{{json .Config.Hostname}},"image":{{json .Image}},"cpu":{{.HostConfig.NanoCpus}},"memory":{{.HostConfig.Memory}},"state":{{json .State.Status}},"oom":{{.State.OOMKilled}},"restarts":{{.RestartCount}},"ports":{{json .HostConfig.PortBindings}}}', container, ] ) ) def current_containers(): result = [] for service in ("api", "db", "redis", "haproxy"): for container in compose(["ps", "-q", service]).split(): info = inspect_public(container) info["service"] = service result.append(info) return result def code_hash(): digest = hashlib.sha256() paths = list(ROOT.joinpath("src/backend").rglob("*")) paths.extend( ROOT / relative for relative in ("src/go.mod", "src/go.sum", "src/Dockerfile") ) for path in sorted(paths): if path.is_file(): digest.update(str(path.relative_to(ROOT)).encode()) digest.update(path.read_bytes()) return digest.hexdigest() def instrument_hash(): digest = hashlib.sha256() for path in sorted(HERE.iterdir()): if path.is_file() and path.suffix in ( ".py", ".sql", ".cfg", ".yml", ".txt", ".sh", ): digest.update(path.name.encode()) digest.update(path.read_bytes()) return digest.hexdigest() def write_json(path, value): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n")