"""Measure isolated daemon startup from exec through its discovery file.""" import json import os from pathlib import Path import signal import subprocess import sys import tempfile import time launcher, destination = sys.argv[1:3] records = [] with tempfile.TemporaryDirectory(prefix="albedo-boot-probe-") as parent: for repetition in range(int(sys.argv[3]) if len(sys.argv) > 3 else 5): home = Path(parent) / str(repetition) home.mkdir() for kind in ("fresh", "existing"): manifest = home / "daemon.json" manifest.unlink(missing_ok=True) env = dict( os.environ, ALBEDO_HOME=str(home), ALBEDO_PRELOAD="0", ALBEDO_KERNEL_SPARES="0", ALBEDO_PARENT_PID=str(os.getpid()), ALBEDO_BOOT_TIMING="1", ALBEDO_NO_BROWSER="1", ALBEDO_WEBHOOK_PORT="0", ) with (home / "probe.log").open("w") as log: began = time.monotonic() process = subprocess.Popen( [launcher], env=env, stdout=log, stderr=subprocess.STDOUT, start_new_session=True, ) try: while not manifest.exists(): if process.poll() is not None or time.monotonic() - began > 30: raise RuntimeError((home / "probe.log").read_text()) time.sleep(0.001) elapsed = (time.monotonic() - began) * 1000 finally: if process.poll() is None: os.killpg(process.pid, signal.SIGTERM) process.wait(timeout=10) record = dict( repetition=repetition, kind=kind, elapsed_ms=elapsed, log=(home / "probe.log").read_text(), ) records.append(record) print(json.dumps(record), flush=True) Path(destination).write_text(json.dumps(records, indent=2))