Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
CI: performance budget (cold start, first reply, idle memory, browser launch) (#534)
Oleksandr Ivanchenkoce869956dc23
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 51884256..800da315 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -125,6 +125,23 @@ jobs:name: native-droppath: ${{ runner.temp }}/native-dropretention-days: 7+ # P3: a performance budget on the same debug kwnt (no new build). Cold start, first reply (fake claude), idle memory+ # and the bot browser launch, each the median of 5 runs against scripts/perf_budget.json. Over a limit fails the step.+ # Not a required check yet: fresh-machine IS required for a release, so this step has continue-on-error: true. A FAIL stays+ # visible (the step, the job summary table, perf-results.json) but does not fail the job. Remove continue-on-error when+ # Ada makes the budget required (after a few days of numbers in the perf-results artifact).+ - name: Performance budget+ continue-on-error: true+ timeout-minutes: 10+ run: python3 scripts/perf_budget.py --kwnt target/debug/kwnt --browser "$(command -v google-chrome || command -v chromium)" --out "$RUNNER_TEMP/perf-results.json"+ - name: Performance budget results+ if: always()+ uses: actions/upload-artifact@v4+ with:+ name: perf-results+ path: ${{ runner.temp }}/perf-results.json+ if-no-files-found: ignore+ retention-days: 30rust:name: rustdiff --git a/scripts/perf_budget.json b/scripts/perf_budget.jsonnew file mode 100644index 00000000..c5c538de--- /dev/null+++ b/scripts/perf_budget.json@@ -0,0 +1,21 @@+{+ "note": "Limits are the baseline x 1.5 (memory x 1.3), rounded up. The baseline is the highest median of the CI runs of the fresh-machine job on ubuntu-latest (Nov 12, 2026): runner speed varies a lot between jobs (cold start median 0.30 s in one job, 0.55-0.65 s in four others). browser_launch_s comes from the two CI runs where Chrome could start (1.53 s, 1.57 s). Rewrite with `perf_budget.py --update-baseline` on purpose.",+ "baseline": {+ "cold_start_s": 0.65,+ "first_reply_s": 0.08,+ "idle_rss_mb": 108.82,+ "after_reply_rss_mb": 108.93,+ "browser_launch_s": 1.57+ },+ "baseline_from": {+ "runs": "fresh-machine on 1b2052c4, aff15db5, ff7279c5, 5fbde03f, 926435e2 (browser: the last two); 5 runs each, memory in 3",+ "kwnt": "target/debug/kwnt (CI debug build)"+ },+ "limits": {+ "cold_start_s": 1.0,+ "first_reply_s": 0.2,+ "idle_rss_mb": 142.0,+ "after_reply_rss_mb": 142.0,+ "browser_launch_s": 2.4+ }+}diff --git a/scripts/perf_budget.py b/scripts/perf_budget.pynew file mode 100644index 00000000..e168e14b--- /dev/null+++ b/scripts/perf_budget.py@@ -0,0 +1,492 @@+#!/usr/bin/env python3+"""P3: the performance budget. Measures kwnt's own speed and memory and fails when a median goes over its limit.++Every run starts the built binary the way scripts/fresh_machine_test.py does (`kwnt serve --no-laya`, `env -i`,+an empty temp HOME and data folder, a free port) with a fake `claude` CLI on PATH that answers at once, so what is+timed is kwnt, not a model or the network. Per run it takes:++ cold_start_s spawn until GET /api/health answers 200+ first_reply_s create a bot, send it one message, and time from the send until the first reply text+ (a TextDelta) arrives on /api/events, the stream the app itself listens to+ idle_rss_mb kwnt's VmRSS (/proc/<pid>/status) --idle-seconds after startup+ after_reply_rss_mb the same, --idle-seconds after that first reply+ browser_launch_s a bot opens its browser (local Chrome, launched cold) and loads a page; only when a Chrome or+ Chromium is found, otherwise `SKIP browser: no browser` (a missing browser never fails). When a+ Chrome is found but does not come up (kwnt reports it could not start Chrome, and it still cannot+ within --browser-wait seconds, default 60), the row is `SKIP` with the reason and the run goes on+ (a runner where Chrome cannot start is not a performance regression, so the browser is dropped for+ the remaining runs). A Chrome that does start and is slower than its limit still FAILS.++Memory is read in the first --memory-runs runs only (each costs 2 x --idle-seconds of waiting); the other measures+are taken in all --runs. The median of the runs is compared with scripts/perf_budget.json. A median over its limit+FAILS (exit 1); a median at its limit passes.++Usage:+ python3 scripts/perf_budget.py --kwnt target/debug/kwnt [--browser /usr/bin/chromium] [--out perf-results.json]+ python3 scripts/perf_budget.py --kwnt ... --update-baseline # rewrite perf_budget.json from this run (on purpose)+ python3 scripts/perf_budget.py --kwnt ... --limits /tmp/tiny.json # try other limits, e.g. to see a FAIL++Limits: baseline x 1.5 for times, x 1.3 for memory, rounded up (see compute_limits). Linux only (reads /proc).+Needs no packages: standard library only.+"""+import argparse+import datetime+import http.client+import json+import math+import os+import shutil+import signal+import socket+import statistics+import subprocess+import sys+import tempfile+import threading+import time+from pathlib import Path++LIMITS_FILE = Path(__file__).with_name("perf_budget.json")+FORBIDDEN_PORTS = {8799, 8800, 8810, 8852, 8931}+# readlink and touch (and the rest of the second row) are what Google Chrome's /usr/bin/google-chrome launcher script calls:+# without them it died with `readlink: command not found` (exit 127) on the CI runner, so Chrome never opened its port.+SYSTEM_TOOLS = ["sh", "bash", "cat", "mkdir", "chmod", "sleep", "which", "env", "uname", "dirname", "rm", "cp", "mv", "ls",+ "readlink", "touch", "basename", "grep", "sed", "tr", "head", "id"]++CHROME_DOWN_MARKER = "could not start kwant's Chrome" # what kwnt's 503 says when Chrome never opens its debugging port+++class ChromeDidNotStart(Exception):+ """kwnt's Chrome did not come up on this machine within the wait. Not a performance result: the row is skipped."""+++# measure id -> (what it is, unit, factor from baseline to limit, decimals the limit is rounded up to)+MEASURES = {+ "cold_start_s": ("cold start (spawn to /api/health 200)", "s", 1.5, 1),+ "first_reply_s": ("first reply (send to first text, fake claude)", "s", 1.5, 1),+ "idle_rss_mb": ("idle memory, 30 s after start", "MB", 1.3, 0),+ "after_reply_rss_mb": ("idle memory, 30 s after first reply", "MB", 1.3, 0),+ "browser_launch_s": ("bot browser launch (cold Chrome to page open)", "s", 1.5, 1),+}++# What the fake claude prints: signed in, then a reply streamed at once. Same idea as FAKE_CLAUDE in+# tests/turn_recovery_test.rs.+FAKE_CLAUDE = """#!/bin/sh+case "$1" in+ auth) echo '{"loggedIn":true}'; exit 0 ;;+ --version) echo "2.0.0 (Claude Code)"; exit 0 ;;+esac+echo '{"type":"system","subtype":"init"}'+echo '{"type":"stream_event","event":{"type":"content_block_delta","delta":{"type":"text_delta","text":"Hello from the fake claude."}}}'+echo '{"type":"assistant","message":{"content":[{"type":"text","text":"Hello from the fake claude."}]}}'+echo '{"type":"result","is_error":false,"num_turns":1,"result":"Hello from the fake claude."}'+"""+++# ---- The comparison logic (unit-tested in test_perf_budget.py; needs no binary) -------------------------------------+def median(values):+ """The median of the numbers, or None when there are none."""+ values = [v for v in values if v is not None]+ return statistics.median(values) if values else None+++def round_up(value, decimals):+ """Rounds up (never down): 0.31 -> 0.4 at 1 decimal, 100.2 -> 101 at 0. Guards float noise (0.3 stays 0.3)."""+ scale = 10 ** decimals+ return math.ceil(round(value * scale, 6)) / scale if decimals else float(math.ceil(round(value, 6)))+++def compute_limits(baseline):+ """A limit per measure from its baseline: x1.5 for times, x1.3 for memory, rounded up."""+ return {k: round_up(v * MEASURES[k][2], MEASURES[k][3]) for k, v in baseline.items() if k in MEASURES and v is not None}+++def judge(medians, limits, skipped=None):+ """One row per measure: {id, median, limit, status, reason}. Status is PASS (at or under the limit), FAIL (over),+ SKIP (not measured, e.g. no browser, or Chrome would not start; `skipped` maps a measure id to why) or NO LIMIT+ (measured, but perf_budget.json has no limit for it: not a failure). `reason` is set on a SKIP row only."""+ rows = []+ skipped = skipped or {}+ for key in MEASURES:+ med, limit = medians.get(key), limits.get(key)+ reason = None+ if med is None:+ status = "SKIP"+ reason = skipped.get(key)+ elif limit is None:+ status = "NO LIMIT"+ else:+ status = "PASS" if med <= limit else "FAIL"+ rows.append({"id": key, "median": med, "limit": limit, "status": status, "reason": reason})+ return rows+++def table(rows):+ """The small text table printed at the end."""+ lines = [f"{'measure':<46} {'median':>9} {'limit':>9} result"]+ for r in rows:+ what, unit = MEASURES[r["id"]][0], MEASURES[r["id"]][1]+ med = "-" if r["median"] is None else f"{r['median']:.2f} {unit}"+ lim = "-" if r["limit"] is None else f"{r['limit']:g} {unit}"+ lines.append(f"{what:<46} {med:>9} {lim:>9} {r['status']}")+ if r.get("reason"):+ lines.append(f" {r['status']} {r['id']}: {r['reason']}")+ return "\n".join(lines)+++def summary_markdown(rows):+ """The same table as Markdown, for the GitHub job summary ($GITHUB_STEP_SUMMARY); SKIP rows carry their reason."""+ out = ["### Performance budget", "", "| measure | median | limit | result |", "|---|---|---|---|"]+ for r in rows:+ what, unit = MEASURES[r["id"]][0], MEASURES[r["id"]][1]+ med = "-" if r["median"] is None else f"{r['median']:.2f} {unit}"+ lim = "-" if r["limit"] is None else f"{r['limit']:g} {unit}"+ out.append(f"| {what} | {med} | {lim} | {r['status']} |")+ out += [f"\n**{r['status']} {r['id']}**: {r['reason']}" for r in rows if r.get("reason")]+ return "\n".join(out) + "\n"+++def exit_code(rows):+ """1 when any median is over its limit, else 0."""+ return 1 if any(r["status"] == "FAIL" for r in rows) else 0+++# ---- Running kwnt ---------------------------------------------------------------------------------------------------+def free_port():+ while True:+ s = socket.socket()+ s.bind(("127.0.0.1", 0))+ port = s.getsockname()[1]+ s.close()+ if port not in FORBIDDEN_PORTS:+ return port+++def request(port, method, path, body=None, timeout=30):+ conn = http.client.HTTPConnection("127.0.0.1", port, timeout=timeout)+ try:+ conn.request(method, path, json.dumps(body) if body is not None else None, {"Content-Type": "application/json"})+ resp = conn.getresponse()+ raw = resp.read()+ try:+ return resp.status, json.loads(raw)+ except ValueError:+ return resp.status, raw.decode(errors="replace")+ finally:+ conn.close()+++def find_browser(explicit):+ for cand in [explicit, os.environ.get("CHROMIUM"), "google-chrome", "google-chrome-stable", "chromium", "chromium-browser"]:+ if cand and (shutil.which(cand) or Path(cand).is_file()):+ return shutil.which(cand) or cand+ return None+++def make_machine(root: Path):+ """An empty home and data folder, a bare PATH (a few system tools) and the fake claude."""+ home, binp, data = root / "home", root / "bin", root / "data"+ for d in (home, binp, data):+ d.mkdir(parents=True)+ for tool in SYSTEM_TOOLS:+ src = shutil.which(tool)+ if src:+ (binp / tool).symlink_to(src)+ fake = binp / "claude"+ fake.write_text(FAKE_CLAUDE)+ fake.chmod(0o755)+ return home, binp, data+++def rss_mb(pid):+ """kwnt's resident memory in MB (VmRSS), or None when /proc has no such process."""+ try:+ for line in Path(f"/proc/{pid}/status").read_text().splitlines():+ if line.startswith("VmRSS:"):+ return int(line.split()[1]) / 1024+ except OSError:+ return None+ return None+++class Events:+ """Listens to /api/events (the stream the app uses) and notes when the first reply text of a bot arrives."""++ def __init__(self, port):+ self.port, self.kwant, self.first_text, self.error = port, None, None, None+ self.ready = threading.Event()+ self.seen = threading.Event()+ self.conn = None+ threading.Thread(target=self._run, daemon=True).start()++ def _run(self):+ try:+ self.conn = http.client.HTTPConnection("127.0.0.1", self.port, timeout=120)+ self.conn.request("GET", "/api/events")+ resp = self.conn.getresponse()+ self.ready.set()+ while True:+ line = resp.readline()+ if not line:+ return+ if not line.startswith(b"data:"):+ continue+ try:+ event = json.loads(line[5:])+ except ValueError:+ continue+ if event.get("type") == "TextDelta" and event.get("kwant_id") == self.kwant and not self.first_text:+ self.first_text = time.perf_counter()+ self.seen.set()+ except Exception as e: # noqa: BLE001 (the run reports it)+ self.error = repr(e)+ self.ready.set()++ def close(self):+ if self.conn:+ self.conn.close()+++def wait_health(port, seconds):+ end = time.perf_counter() + seconds+ while time.perf_counter() < end:+ try:+ if request(port, "GET", "/api/health", timeout=2)[0] == 200:+ return time.perf_counter()+ except OSError:+ pass+ time.sleep(0.01)+ raise TimeoutError(f"/api/health did not answer in {seconds}s")+++def _read(path):+ try:+ return Path(path).read_text().strip()+ except OSError:+ return "n/a"+++def probe_chrome(browser, extra=(), seconds=20, env=None):+ """Starts `browser` the way kwnt's local Chrome starts (same flags, a fresh profile) but with its stderr kept, and+ says whether it opened a debugging port. Returns (opened, exit code or None, stderr tail). Never raises."""+ root = Path(tempfile.mkdtemp(prefix="perf-chrome-probe-"))+ err = root / "stderr.txt"+ proc = None+ try:+ args = [browser, f"--user-data-dir={root / 'p'}", "--remote-debugging-port=0", "--remote-debugging-address=127.0.0.1",+ "--no-first-run", "--no-default-browser-check", "--headless=new", "--window-size=1280,800", *extra, "about:blank"]+ proc = subprocess.Popen(args, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=open(err, "w"), start_new_session=True, env=env)+ opened, code = False, None+ deadline = time.monotonic() + seconds+ while time.monotonic() < deadline:+ if (root / "p" / "DevToolsActivePort").exists():+ opened = True+ break+ code = proc.poll()+ if code is not None:+ break+ time.sleep(0.25)+ tail = "\n".join(err.read_text(errors="replace").splitlines()[-12:])+ return opened, code, tail+ except Exception as e: # a diagnosis must never turn a SKIP into a crash+ return False, None, f"probe failed: {e}"+ finally:+ if proc:+ try:+ os.killpg(proc.pid, signal.SIGKILL)+ except ProcessLookupError:+ pass+++def diagnose_chrome(browser):+ """Why Chrome may not start here, for the log: the OS, the AppArmor switch for unprivileged user namespaces, and+ Chrome's own stderr with kwnt's flags and again with --no-sandbox (kwnt throws Chrome's stderr away)."""+ lines = [f"os: {next((l.split('=', 1)[1].strip(chr(34)) for l in _read('/etc/os-release').splitlines() if l.startswith('PRETTY_NAME=')), 'n/a')}",+ f"kernel.apparmor_restrict_unprivileged_userns = {_read('/proc/sys/kernel/apparmor_restrict_unprivileged_userns')}",+ f"kernel.unprivileged_userns_clone = {_read('/proc/sys/kernel/unprivileged_userns_clone')}",+ f"user.max_user_namespaces = {_read('/proc/sys/user/max_user_namespaces')}",+ f"chrome: {browser}"]+ try:+ lines.append("version: " + subprocess.run([browser, "--version"], capture_output=True, text=True, timeout=20).stdout.strip())+ except Exception as e:+ lines.append(f"version: failed ({e})")+ machine = Path(tempfile.mkdtemp(prefix="perf-chrome-env-"))+ home, binp, _ = make_machine(machine)+ bare = {"HOME": str(home), "PATH": str(binp)} # exactly what kwnt (and so the Chrome it starts) ran with+ lines.append(f"bare PATH holds: {' '.join(sorted(p.name for p in binp.iterdir()))}")+ for label, extra, env in (("kwnt's flags, the runner's own environment", (), None),+ ("kwnt's flags, the bare environment of the run (HOME and PATH only)", (), bare),+ ("kwnt's flags plus --no-sandbox, the bare environment", ("--no-sandbox",), bare)):+ opened, code, tail = probe_chrome(browser, extra, env=env)+ lines.append(f"{label}: " + ("opened its debugging port" if opened else f"NO debugging port (exit {code})"))+ if tail and not opened:+ lines.append(" stderr: " + tail.replace("\n", "\n "))+ return "\n".join(lines)+++def open_browser_page(port, kid, wait):+ """Time from asking the bot to open a page to the page being open (cold Chrome). kwnt answers 503 ("could not start+ kwant's Chrome ...") when Chrome never opens its debugging port; on that the call is repeated until `wait` seconds+ have passed since the first ask, and if Chrome is still down raises ChromeDidNotStart with kwnt's reason. Any other+ failure (another status, a page that did not open) is a real failure and raises RuntimeError."""+ t_nav = time.perf_counter()+ reason = None+ while True:+ code, page = request(port, "POST", f"/api/sandbox/{kid}/navigate", {"url": f"http://127.0.0.1:{port}/api/health"}, timeout=90)+ if code == 200 and isinstance(page, dict) and page.get("status") == "navigated":+ return time.perf_counter() - t_nav+ error = page.get("error", "") if isinstance(page, dict) else str(page)+ if code == 503 and CHROME_DOWN_MARKER in error:+ reason = error+ if time.perf_counter() - t_nav >= wait:+ raise ChromeDidNotStart(f"{reason} (no debugging port after {time.perf_counter() - t_nav:.0f} s)")+ time.sleep(1)+ continue+ raise RuntimeError(f"the bot browser did not open a page: {code} {str(page)[:200]}")+++def one_run(kwnt, browser, idle_seconds, take_memory, log, browser_wait=60):+ """One fresh kwnt: {measure id: value}. Raises on a failure of kwnt itself (not of a limit). When the bot browser's+ Chrome never comes up, the other measures are kept and the result carries "browser_skip": why (the caller then+ drops the browser from the remaining runs)."""+ root = Path(tempfile.mkdtemp(prefix="perf-budget-"))+ home, binp, data = make_machine(root)+ port = free_port()+ env = {"HOME": str(home), "PATH": str(binp), "KWNT_DATA_DIR": str(data), "KWNT_HOST": "127.0.0.1", "KWNT_PORT": str(port)}+ if browser:+ env["KWNT_COMPUTER_MODE"] = "local"+ env["KWNT_CHROME_PATH"] = browser+ out = {}+ t0 = time.perf_counter()+ proc = subprocess.Popen([str(kwnt), "serve", "--no-laya"], env=env, stdout=open(root / "kwnt.log", "w"),+ stderr=subprocess.STDOUT, cwd=str(root), start_new_session=True)+ events = None+ try:+ out["cold_start_s"] = wait_health(port, 60) - t0+ t_up = time.perf_counter()++ code, bot = request(port, "POST", "/api/kwants", {"name": "Clio", "title": "Tester", "persona_prompt": "Test persona",+ "avatar_sprite": "s", "driver_type": "api", "preferred_model": "claude"})+ if code not in (200, 201) or not isinstance(bot, dict) or "id" not in bot:+ raise RuntimeError(f"creating the bot failed: {code} {bot}")+ kid = bot["id"]+ events = Events(port)+ events.kwant = kid+ if not events.ready.wait(10) or events.error:+ raise RuntimeError(f"could not listen to /api/events: {events.error}")+ t_send = time.perf_counter()+ code, sent = request(port, "POST", "/api/chat/send", {"kwant_id": kid, "prompt": "hi", "sender_id": "user"})+ if code != 200:+ raise RuntimeError(f"sending the message failed: {code} {sent}")+ if not events.seen.wait(30):+ raise RuntimeError("no reply text arrived in 30 s")+ out["first_reply_s"] = events.first_text - t_send++ if take_memory:+ # 30 s after startup, then 30 s after the reply (measured from when each happened)+ time.sleep(max(0.0, t_up + idle_seconds - time.perf_counter()))+ out["idle_rss_mb"] = rss_mb(proc.pid)+ time.sleep(max(0.0, events.first_text + idle_seconds - time.perf_counter()))+ out["after_reply_rss_mb"] = rss_mb(proc.pid)++ if browser:+ try:+ out["browser_launch_s"] = open_browser_page(port, kid, browser_wait)+ except ChromeDidNotStart as e:+ out["browser_skip"] = str(e)+ return out+ except Exception:+ log(f"---- kwnt log (last 20 lines) ----\n" + "\n".join((root / "kwnt.log").read_text(errors="replace").splitlines()[-20:]))+ raise+ finally:+ try:+ os.killpg(proc.pid, signal.SIGTERM)+ proc.wait(10)+ except (ProcessLookupError, subprocess.TimeoutExpired):+ try:+ os.killpg(proc.pid, signal.SIGKILL)+ except ProcessLookupError:+ pass+ if events:+ events.close() # after kwnt stopped: closing a stream that is still open waits for it+ if browser:+ # kwnt's Chrome keeps running when kwnt stops: end everything that still names this run's folder+ subprocess.run(["pkill", "-f", "--", str(root)], check=False)+ shutil.rmtree(root, ignore_errors=True)+++def main():+ ap = argparse.ArgumentParser()+ ap.add_argument("--kwnt", required=True, help="path to the kwnt binary")+ ap.add_argument("--browser", help="Chrome or Chromium for the browser measure (default: found on PATH; none = skipped)")+ ap.add_argument("--runs", type=int, default=5, help="runs per measure (default 5)")+ ap.add_argument("--memory-runs", type=int, default=3, help="runs that also read memory (default 3; each waits 2 x idle-seconds)")+ ap.add_argument("--idle-seconds", type=float, default=30, help="how long kwnt idles before memory is read (default 30)")+ ap.add_argument("--browser-wait", type=float, default=60, help="how long to wait for Chrome's debugging port before the browser row is skipped (default 60)")+ ap.add_argument("--limits", default=str(LIMITS_FILE), help="the limits file (default scripts/perf_budget.json)")+ ap.add_argument("--out", default="perf-results.json", help="where the results are written")+ ap.add_argument("--update-baseline", action="store_true", help="rewrite the limits file from this run (on purpose, not in CI)")+ args = ap.parse_args()++ kwnt = Path(args.kwnt).resolve()+ if not kwnt.is_file():+ sys.exit(f"no kwnt binary at {kwnt}")+ if not sys.platform.startswith("linux"):+ sys.exit("perf_budget.py reads /proc, so it runs on Linux only")+ limits_path = Path(args.limits)+ limits = json.loads(limits_path.read_text()).get("limits", {}) if limits_path.is_file() else {}+ browser = find_browser(args.browser)+ skipped = {}+ diagnosis = None+ if not browser:+ skipped["browser_launch_s"] = "no browser"+ print("SKIP browser: no browser", flush=True)+ found_browser = browser++ runs = []+ for i in range(args.runs):+ print(f"run {i + 1}/{args.runs} ...", flush=True)+ runs.append(one_run(kwnt, browser, args.idle_seconds, i < args.memory_runs, lambda m: print(m, flush=True), args.browser_wait))+ print(" " + " ".join(f"{k}={v:.3f}" for k, v in runs[-1].items() if isinstance(v, float)), flush=True)+ if runs[-1].get("browser_skip"):+ skipped["browser_launch_s"] = runs[-1]["browser_skip"]+ print(f"SKIP browser: {skipped['browser_launch_s']}", flush=True)+ diagnosis = diagnose_chrome(browser)+ print("---- why Chrome did not start (diagnosis) ----\n" + diagnosis, flush=True)+ browser = None # Chrome cannot start here: the remaining runs do not try it again++ medians = {k: median([r.get(k) for r in runs]) for k in MEASURES}+ rows = judge(medians, limits, skipped)+ print("\n" + table(rows))++ results = {+ "when": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds"),+ "kwnt": str(kwnt), "runs": args.runs, "memory_runs": args.memory_runs, "idle_seconds": args.idle_seconds,+ "browser": found_browser, "browser_skip": skipped.get("browser_launch_s"), "chrome_diagnosis": diagnosis, "measures": rows, "samples": {k: [r.get(k) for r in runs] for k in MEASURES},+ }+ Path(args.out).write_text(json.dumps(results, indent=2) + "\n")+ print(f"\nwrote {args.out}")+ summary = os.environ.get("GITHUB_STEP_SUMMARY")+ if summary:+ with open(summary, "a") as f:+ f.write(summary_markdown(rows))+ if diagnosis:+ f.write("\n<details><summary>Why Chrome did not start</summary>\n\n```\n" + diagnosis + "\n```\n</details>\n")++ if args.update_baseline:+ baseline = {k: round(v, 3) for k, v in medians.items() if v is not None}+ limits_path.write_text(json.dumps({+ "note": "Limits are the baseline x 1.5 (memory x 1.3), rounded up. Rewrite with `perf_budget.py --update-baseline` on purpose.",+ "baseline": baseline,+ "baseline_from": {"date": results["when"], "runs": args.runs, "kwnt": kwnt.name},+ "limits": compute_limits(baseline),+ }, indent=2) + "\n")+ print(f"rewrote {limits_path}")+ return 0+ code = exit_code(rows)+ print("performance budget: " + ("FAILED, a median is over its limit" if code else "ok"))+ return code+++if __name__ == "__main__":+ sys.exit(main())diff --git a/scripts/test_perf_budget.py b/scripts/test_perf_budget.pynew file mode 100644index 00000000..5fdf9523--- /dev/null+++ b/scripts/test_perf_budget.py@@ -0,0 +1,172 @@+"""Tests for the performance budget's comparison logic (they need no kwnt binary and run in the `site` job's unittest step).++- over its limit fails, at its limit passes, under passes;+- a missing browser is skipped and never fails the run;+- a measure with no limit in perf_budget.json is reported, not failed;+- limits are the baseline x 1.5 (memory x 1.3), rounded up;+- the shipped perf_budget.json names only known measures, and every limit is above its baseline;+- the CI step exists in the fresh-machine job and is not a required check yet.+"""+import json+import re+import sys+import unittest+from unittest import mock+from pathlib import Path++sys.path.insert(0, str(Path(__file__).parent))+import perf_budget as pb # noqa: E402++ROOT = Path(__file__).resolve().parent.parent+LIMITS = {"cold_start_s": 2.0, "first_reply_s": 1.0, "idle_rss_mb": 150, "after_reply_rss_mb": 160, "browser_launch_s": 6.0}+++def status(rows, key):+ return next(r["status"] for r in rows if r["id"] == key)+++class Judge(unittest.TestCase):+ def test_under_the_limit_passes(self):+ rows = pb.judge({"cold_start_s": 1.2}, LIMITS)+ self.assertEqual(status(rows, "cold_start_s"), "PASS")+ self.assertEqual(pb.exit_code(rows), 0)++ def test_at_the_limit_passes(self):+ rows = pb.judge({"cold_start_s": 2.0, "idle_rss_mb": 150}, LIMITS)+ self.assertEqual(status(rows, "cold_start_s"), "PASS")+ self.assertEqual(status(rows, "idle_rss_mb"), "PASS")+ self.assertEqual(pb.exit_code(rows), 0)++ def test_over_the_limit_fails_and_exits_1(self):+ rows = pb.judge({"cold_start_s": 2.01, "first_reply_s": 0.2}, LIMITS)+ self.assertEqual(status(rows, "cold_start_s"), "FAIL")+ self.assertEqual(status(rows, "first_reply_s"), "PASS")+ self.assertEqual(pb.exit_code(rows), 1)++ def test_one_failing_measure_fails_the_run_even_when_the_rest_pass(self):+ medians = {k: v / 2 for k, v in LIMITS.items()}+ medians["after_reply_rss_mb"] = 161+ self.assertEqual(pb.exit_code(pb.judge(medians, LIMITS)), 1)++ def test_a_missing_browser_is_skipped_not_failed(self):+ medians = {k: v / 2 for k, v in LIMITS.items() if k != "browser_launch_s"}+ rows = pb.judge(medians, LIMITS)+ self.assertEqual(status(rows, "browser_launch_s"), "SKIP")+ self.assertEqual(pb.exit_code(rows), 0)++ def test_a_measure_without_a_limit_is_reported_not_failed(self):+ rows = pb.judge({"cold_start_s": 99.0}, {})+ self.assertEqual(status(rows, "cold_start_s"), "NO LIMIT")+ self.assertEqual(pb.exit_code(rows), 0)++ def test_the_table_has_one_line_per_measure_and_the_result(self):+ text = pb.table(pb.judge({"cold_start_s": 3.0}, LIMITS))+ self.assertEqual(len(text.splitlines()), 1 + len(pb.MEASURES))+ self.assertIn("FAIL", text)+ self.assertIn("SKIP", text)+++class Numbers(unittest.TestCase):+ def test_median_ignores_missing_values(self):+ self.assertEqual(pb.median([3, None, 1, 2]), 2)+ self.assertIsNone(pb.median([None]))+ self.assertIsNone(pb.median([]))++ def test_round_up_never_rounds_down(self):+ self.assertEqual(pb.round_up(0.31, 1), 0.4)+ self.assertEqual(pb.round_up(0.3, 1), 0.3)+ self.assertEqual(pb.round_up(100.2, 0), 101)+ self.assertEqual(pb.round_up(0.1 * 3, 1), 0.3) # 0.30000000000000004 must not become 0.4++ def test_limits_are_the_baseline_times_1_5_and_memory_times_1_3_rounded_up(self):+ limits = pb.compute_limits({"cold_start_s": 1.1, "first_reply_s": 0.2, "idle_rss_mb": 90.0, "browser_launch_s": 2.5})+ self.assertEqual(limits["cold_start_s"], 1.7) # 1.65+ self.assertEqual(limits["first_reply_s"], 0.3)+ self.assertEqual(limits["idle_rss_mb"], 117) # 90 x 1.3 = 117, not 118+ self.assertEqual(limits["browser_launch_s"], 3.8) # 3.75+++DOWN = {"error": "The bot browser could not start kwant's Chrome on this computer: Chrome started but did not open its debugging port in time.", "is_running": False}+++class ChromeThatWillNotStart(unittest.TestCase):+ """A runner where Chrome cannot start is SKIP with the reason, never FAIL; a Chrome that starts is still held to its limit."""++ def test_the_browser_row_is_skip_with_the_reason_and_the_run_passes(self):+ medians = {k: v / 2 for k, v in LIMITS.items() if k != "browser_launch_s"}+ rows = pb.judge(medians, LIMITS, {"browser_launch_s": "Chrome did not start"})+ row = next(r for r in rows if r["id"] == "browser_launch_s")+ self.assertEqual(row["status"], "SKIP")+ self.assertEqual(row["reason"], "Chrome did not start")+ self.assertEqual(pb.exit_code(rows), 0)++ def test_the_reason_is_in_the_printed_table_and_the_job_summary(self):+ rows = pb.judge({}, LIMITS, {"browser_launch_s": "Chrome did not start"})+ self.assertIn("Chrome did not start", pb.table(rows))+ self.assertIn("Chrome did not start", pb.summary_markdown(rows))+ self.assertIn("| SKIP |", pb.summary_markdown(rows))++ def test_a_slow_browser_that_did_start_still_fails(self):+ medians = {k: v / 2 for k, v in LIMITS.items()}+ medians["browser_launch_s"] = LIMITS["browser_launch_s"] + 0.1+ rows = pb.judge(medians, LIMITS, {"browser_launch_s": "ignored: it was measured"})+ self.assertEqual(status(rows, "browser_launch_s"), "FAIL")+ self.assertIsNone(next(r for r in rows if r["id"] == "browser_launch_s")["reason"])+ self.assertEqual(pb.exit_code(rows), 1)++ def test_a_503_from_kwnt_about_chrome_that_lasts_past_the_wait_is_chrome_did_not_start(self):+ with mock.patch.object(pb, "request", return_value=(503, DOWN)) as req, mock.patch.object(pb.time, "sleep"):+ with self.assertRaises(pb.ChromeDidNotStart) as ctx:+ pb.open_browser_page(1234, "kid", 0)+ self.assertIn("debugging port", str(ctx.exception))+ self.assertEqual(req.call_count, 1) # the wait was already over after the first answer++ def test_it_asks_again_inside_the_wait_and_times_the_whole_launch(self):+ answers = [(503, DOWN), (200, {"status": "navigated"})]+ with mock.patch.object(pb, "request", side_effect=answers), mock.patch.object(pb.time, "sleep"):+ seconds = pb.open_browser_page(1234, "kid", 60)+ self.assertGreaterEqual(seconds, 0)++ def test_any_other_failure_is_a_real_failure_not_a_skip(self):+ for answer in [(500, {"error": "boom"}), (503, {"error": "Sandbox browser is unavailable"}), (200, {"status": "failed"}), (404, "nope")]:+ with mock.patch.object(pb, "request", return_value=answer):+ with self.assertRaises(RuntimeError, msg=str(answer)):+ pb.open_browser_page(1234, "kid", 60)+++class ShippedFiles(unittest.TestCase):+ def test_perf_budget_json_names_known_measures_and_sits_above_its_baseline(self):+ data = json.loads((ROOT / "scripts" / "perf_budget.json").read_text())+ for key, limit in data["limits"].items():+ self.assertIn(key, pb.MEASURES)+ if key in data.get("baseline", {}):+ self.assertGreaterEqual(limit, data["baseline"][key], key)++ def test_the_ci_step_is_in_the_fresh_machine_job_and_not_required_yet(self):+ ci = (ROOT / ".github" / "workflows" / "ci.yml").read_text()+ job = re.search(r"\n fresh-machine:\n(.*?)\n \w[\w-]*:\n", ci, re.S)+ self.assertTrue(job, "no fresh-machine job in ci.yml")+ self.assertIn("name: Performance budget", job.group(1))+ self.assertIn("scripts/perf_budget.py", job.group(1))+ self.assertNotIn("Performance budget", (ROOT / "scripts" / "release_required_checks.txt").read_text())++ def test_the_step_has_continue_on_error_while_fresh_machine_is_a_required_check(self):+ # fresh-machine is in release_required_checks.txt; a budget FAIL on a slow runner must not block a release.+ # Drop continue-on-error only on purpose, when the budget becomes required.+ ci = (ROOT / ".github" / "workflows" / "ci.yml").read_text()+ step = re.search(r"\n - name: Performance budget\n(.*?)(?=\n - name:|\Z)", ci, re.S)+ self.assertTrue(step, "no Performance budget step in ci.yml")+ self.assertRegex(step.group(1), r"(?m)^ continue-on-error: true$")+ self.assertIn("fresh-machine", (ROOT / "scripts" / "release_required_checks.txt").read_text())+++class BarePathHasWhatChromesLauncherNeeds(unittest.TestCase):+ def test_readlink_and_touch_are_on_the_bare_path(self):+ # /usr/bin/google-chrome is a shell script: on the CI runner it died with "readlink: command not found" and+ # "touch: command not found" under the run's bare PATH, and Chrome never opened its debugging port+ for tool in ("readlink", "touch"):+ self.assertIn(tool, pb.SYSTEM_TOOLS)+++if __name__ == "__main__":+ unittest.main()