#!/usr/bin/env python3 """Run Track 1 scenarios on the user's Docker host; records only measured results.""" import argparse import csv import json from pathlib import Path import subprocess import sys import time ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / 'evidence' / 'scaling' BASE_ENV = ROOT / '.env' def run(cmd, env=None, check=True, capture=False): return subprocess.run(cmd, cwd=ROOT, env=env, check=check, text=True, stdout=subprocess.PIPE if capture else None, stderr=subprocess.STDOUT if capture else None) def stats(): p = run(['docker', 'stats', '--no-stream', '--format', '{{.Name}};{{.CPUPerc}};{{.MemUsage}}'], capture=True) return p.stdout.strip().splitlines() def env_value(source, key, default): prefix = key + '=' for line in source.splitlines(): if line.startswith(prefix): return line[len(prefix):].strip().strip('\"').strip("'") return default def db_activity(env_path, db_user, db_name): query = "SELECT count(*) FILTER (WHERE state='active'), count(*), current_setting('max_connections') FROM pg_stat_activity" result = run(['docker', 'compose', '--env-file', str(env_path), 'exec', '-T', 'postgres', 'psql', '-U', db_user, '-d', db_name, '-At', '-F', ',', '-c', query], capture=True, check=False) return result.stdout.strip() if result.returncode == 0 else 'unavailable' def scenario(name, replicas, total_cpu, total_mem, duration, concurrency): source = BASE_ENV.read_text(encoding='utf-8') db_user = env_value(source, 'POSTGRES_USER', 'messenger') db_name = env_value(source, 'POSTGRES_DB', 'messenger') api_cpu = total_cpu / replicas api_mem_mb = total_mem // replicas env_text = source.rstrip() + f'\nAPI_CPU_LIMIT={api_cpu:.3f}\nAPI_MEMORY_LIMIT={api_mem_mb}M\n' env_path = OUT / ('.env-' + name) env_path.write_text(env_text, encoding='utf-8') compose = ['docker', 'compose', '--env-file', str(env_path), 'up', '-d', '--build', '--scale', f'api={replicas}'] run(compose) deadline = time.time() + 120 while time.time() < deadline: result = run(['docker', 'compose', '--env-file', str(env_path), 'ps', '-q', 'api'], capture=True, check=False) ids = [line.strip() for line in result.stdout.splitlines() if line.strip()] healthy = 0 for container_id in ids: inspected = run(['docker', 'inspect', '--format', '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}', container_id], capture=True, check=False) if inspected.returncode == 0 and inspected.stdout.strip() == 'healthy': healthy += 1 if len(ids) >= replicas and healthy >= replicas: break time.sleep(2) else: raise RuntimeError(f'{name}: API replicas did not become healthy') output = OUT / (name + '.json') cmd = [sys.executable, 'tests/load.py', '--base-url', 'http://localhost:8080', '--duration', str(duration), '--concurrency', str(concurrency), '--output', str(output)] proc = subprocess.Popen(cmd, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) samples = [] while proc.poll() is None: timestamp = time.time() try: samples.append({'timestamp_epoch': timestamp, 'containers': stats(), 'postgres_active_total_max_connections': db_activity(env_path, db_user, db_name)}) except Exception as exc: samples.append({'timestamp_epoch': timestamp, 'stats_error': type(exc).__name__}) time.sleep(1) stdout = proc.stdout.read() if proc.stdout else '' if proc.returncode != 0: raise RuntimeError(f'load failed for {name}: {stdout}') metrics = json.loads(output.read_text(encoding='utf-8')) metrics.update({'scenario': name, 'api_replicas': replicas, 'total_api_cpu_budget': total_cpu, 'total_api_memory_budget_mb': total_mem, 'per_replica_cpu_limit': api_cpu, 'per_replica_memory_limit_mb': api_mem_mb, 'container_stats_samples': samples}) output.write_text(json.dumps(metrics, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') run(['docker', 'compose', '--env-file', str(env_path), 'down']) return metrics def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('--duration', type=int, default=30) p.add_argument('--concurrency', type=int, default=8) p.add_argument('--fixed-cpu', type=float, default=2.0) p.add_argument('--fixed-memory-mb', type=int, default=2048) p.add_argument('--expanded-cpu', type=float, nargs=3, default=[2.0, 4.0, 8.0]) p.add_argument('--expanded-memory-mb', type=int, nargs=3, default=[2048, 4096, 8192]) args = p.parse_args() if not BASE_ENV.exists(): p.error('Create .env from .env.example before running') OUT.mkdir(parents=True, exist_ok=True) results = [] for n in (1, 2, 4): results.append(scenario(f'fixed_{n}replicas', n, args.fixed_cpu, args.fixed_memory_mb, args.duration, args.concurrency)) for cpu, mem in zip(args.expanded_cpu, args.expanded_memory_mb): results.append(scenario(f'expanded_4replicas_{cpu:g}cpu_{mem}mb', 4, cpu, mem, args.duration, args.concurrency)) with (OUT / 'summary.csv').open('w', newline='', encoding='utf-8') as f: fields = ['scenario', 'api_replicas', 'total_api_cpu_budget', 'total_api_memory_budget_mb', 'successful_rps', 'failed_requests', 'error_ratio', 'p50_ms', 'p95_ms', 'p99_ms'] writer = csv.DictWriter(f, fieldnames=fields) writer.writeheader() for x in results: lat = x['successful_latency_ms'] writer.writerow({'scenario': x['scenario'], 'api_replicas': x['api_replicas'], 'total_api_cpu_budget': x['total_api_cpu_budget'], 'total_api_memory_budget_mb': x['total_api_memory_budget_mb'], 'successful_rps': x['successful_rps'], 'failed_requests': x['failed_requests'], 'error_ratio': x['error_ratio'], 'p50_ms': lat['p50'], 'p95_ms': lat['p95'], 'p99_ms': lat['p99']}) print(f'Wrote measured results to {OUT}') if __name__ == '__main__': main()