forked from mtusi2026/mtusi2026lab3
@@ -0,0 +1,70 @@
|
||||
"""Run one reproducible track 1 case in a separate Compose project."""
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COMPOSE = ['docker', 'compose', '-p', 'mtusi2026lab3-track1',
|
||||
'-f', 'compose.yaml', '-f', 'infra/scaling.compose.yaml']
|
||||
|
||||
|
||||
def command(*args, env=None, quiet_errors=False):
|
||||
return subprocess.check_output(args, cwd=ROOT, env=env, text=True, timeout=180,
|
||||
stderr=subprocess.DEVNULL if quiet_errors else None).strip()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('name')
|
||||
parser.add_argument('--replicas', type=int, choices=[1, 2, 4], default=2)
|
||||
parser.add_argument('--cpu', type=float, default=1)
|
||||
parser.add_argument('--memory', default='512m')
|
||||
parser.add_argument('--concurrency', type=int, default=32)
|
||||
parser.add_argument('--duration', type=int, default=15)
|
||||
args = parser.parse_args()
|
||||
assert Path(args.name).name == args.name
|
||||
env = dict(os.environ, TRACK1_REPLICAS=str(args.replicas), TRACK1_CPU=str(args.cpu),
|
||||
TRACK1_MEMORY=args.memory)
|
||||
output = ROOT / 'evidence' / 'track1'
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
command(*COMPOSE, 'up', '-d', '--no-build', '--wait', '--wait-timeout', '90', env=env)
|
||||
time.sleep(3) # nginx DNS cache expires in two seconds.
|
||||
ids = command(*COMPOSE, 'ps', '-q', env=env).splitlines()
|
||||
template = '{{json .Name}} {{.HostConfig.NanoCpus}} {{.HostConfig.Memory}} {{.Image}}'
|
||||
limits = command('docker', 'inspect', '--format', template, *ids).splitlines()
|
||||
api_limits = [line.split() for line in limits if '/mtusi2026lab3-track1-api-' in line]
|
||||
assert len(api_limits) == args.replicas
|
||||
assert all(int(row[1]) == int(args.cpu*1e9)
|
||||
and int(row[2]) == int(args.memory.rstrip('m'))*1024*1024 for row in api_limits)
|
||||
metadata = {'case': vars(args), 'started_at': datetime.now(timezone.utc).isoformat(),
|
||||
'container_limits': limits, 'docker': command('docker', 'version', '--format', '{{.Server.Version}}'),
|
||||
'compose': command('docker', 'compose', 'version', '--short'),
|
||||
'host': command('docker', 'info', '--format', '{{.NCPU}} CPUs; {{.MemTotal}} bytes RAM'),
|
||||
'resource_samples': []}
|
||||
bench_name = 'mtusi2026lab3-track1-bench'
|
||||
process = subprocess.Popen([*COMPOSE, 'run', '--rm', '--no-deps', '--name', bench_name,
|
||||
'bench', 'python', '-u', '-m', 'scripts.scaling_load',
|
||||
'--name', args.name, '--concurrency', str(args.concurrency),
|
||||
'--duration', str(args.duration)], cwd=ROOT, env=env)
|
||||
while process.poll() is None:
|
||||
try:
|
||||
stats = command('docker', 'stats', '--no-stream', '--format', '{{json .}}', *ids, bench_name,
|
||||
quiet_errors=True)
|
||||
metadata['resource_samples'].append({'at': datetime.now(timezone.utc).isoformat(),
|
||||
'containers': [json.loads(line) for line in stats.splitlines()]})
|
||||
except subprocess.CalledProcessError:
|
||||
pass # The one-shot benchmark can exit between poll and stats.
|
||||
time.sleep(1)
|
||||
(output / (args.name + '-resources.json')).write_text(json.dumps(metadata, indent=2)+'\n')
|
||||
if process.returncode:
|
||||
raise SystemExit(process.returncode)
|
||||
run = json.loads((output / (args.name + '.json')).read_text())
|
||||
assert len(run['instances']) == args.replicas, 'Not every API handled the workload'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user