| 1 |
"""Measure what an import costs before anyone has done any work. |
| 2 |
|
| 3 |
Cold start is the budget the headless CLI has to live inside, and it is spent |
| 4 |
almost entirely on imports: the modules a front end touches on its way to a |
| 5 |
connection, not the query. This reports the wall time and the module count for |
| 6 |
each step, so a regression points at the import that caused it. |
| 7 |
|
| 8 |
Usage: |
| 9 |
uv run python scripts/cold_start.py |
| 10 |
uv run python scripts/cold_start.py --runs 10 |
| 11 |
uv run python scripts/cold_start.py --json # for CI job summaries |
| 12 |
|
| 13 |
The numbers are informational. The blocking guards are the import-linter |
| 14 |
contracts in pyproject.toml and tests/unit_tests/test_import_hygiene.py, which |
| 15 |
fail identically on every machine; a wall clock on a shared CI runner does not. |
| 16 |
""" |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import argparse |
| 21 |
import json |
| 22 |
import subprocess |
| 23 |
import sys |
| 24 |
import time |
| 25 |
|
| 26 |
# (label, statement). Ordered cheapest to dearest, which is also roughly the |
| 27 |
# order a headless invocation touches them. |
| 28 |
STEPS: list[tuple[str, str]] = [ |
| 29 |
("interpreter only", "pass"), |
| 30 |
("harlequin", "import harlequin"), |
| 31 |
("harlequin.statements", "import harlequin.statements"), |
| 32 |
("harlequin.catalog", "import harlequin.catalog"), |
| 33 |
("harlequin.options", "import harlequin.options"), |
| 34 |
("harlequin.config", "import harlequin.config"), |
| 35 |
("harlequin.plugins", "import harlequin.plugins"), |
| 36 |
( |
| 37 |
"fastdatatable backend", |
| 38 |
"from textual_fastdatatable.backend import create_backend", |
| 39 |
), |
| 40 |
("harlequin.query", "import harlequin.query"), |
| 41 |
("harlequin.layout", "import harlequin.layout"), |
| 42 |
("harlequin.export", "import harlequin.export"), |
| 43 |
("harlequin.hsql.cli", "import harlequin.hsql.cli"), |
| 44 |
("harlequin_sqlite", "import harlequin_sqlite"), |
| 45 |
("harlequin_duckdb", "import harlequin_duckdb"), |
| 46 |
("harlequin.cli", "import harlequin.cli"), |
| 47 |
("the TUI", "from harlequin import Harlequin"), |
| 48 |
] |
| 49 |
|
| 50 |
PROBE = """ |
| 51 |
{statement} |
| 52 |
import sys |
| 53 |
print(len(sys.modules), "textual" in sys.modules, sep=",") |
| 54 |
""" |
| 55 |
|
| 56 |
|
| 57 |
def measure(statement: str, runs: int) -> tuple[float, int, bool]: |
| 58 |
"""Returns (best milliseconds, module count, whether Textual was imported). |
| 59 |
|
| 60 |
The best of N, not the mean: we want the floor the import costs, and a |
| 61 |
shared runner only ever adds noise on top of it. |
| 62 |
""" |
| 63 |
best = float("inf") |
| 64 |
modules = 0 |
| 65 |
imported_textual = False |
| 66 |
for _ in range(runs): |
| 67 |
start = time.perf_counter() |
| 68 |
proc = subprocess.run( |
| 69 |
[sys.executable, "-c", PROBE.format(statement=statement)], |
| 70 |
capture_output=True, |
| 71 |
text=True, |
| 72 |
check=True, |
| 73 |
) |
| 74 |
best = min(best, (time.perf_counter() - start) * 1000) |
| 75 |
count, textual = proc.stdout.strip().split(",") |
| 76 |
modules = int(count) |
| 77 |
imported_textual = textual == "True" |
| 78 |
return best, modules, imported_textual |
| 79 |
|
| 80 |
|
| 81 |
def main() -> int: |
| 82 |
parser = argparse.ArgumentParser(description=__doc__) |
| 83 |
parser.add_argument( |
| 84 |
"--runs", type=int, default=5, help="iterations per step (best wins)" |
| 85 |
) |
| 86 |
parser.add_argument( |
| 87 |
"--json", action="store_true", help="emit JSON instead of a table" |
| 88 |
) |
| 89 |
args = parser.parse_args() |
| 90 |
|
| 91 |
results = [] |
| 92 |
for label, statement in STEPS: |
| 93 |
elapsed, modules, textual = measure(statement, args.runs) |
| 94 |
results.append( |
| 95 |
{ |
| 96 |
"step": label, |
| 97 |
"statement": statement, |
| 98 |
"ms": round(elapsed, 1), |
| 99 |
"modules": modules, |
| 100 |
"textual": textual, |
| 101 |
} |
| 102 |
) |
| 103 |
|
| 104 |
if args.json: |
| 105 |
print(json.dumps(results, indent=2)) |
| 106 |
return 0 |
| 107 |
|
| 108 |
print(f"{'step':<24}{'ms':>8}{'modules':>10} textual") |
| 109 |
print("-" * 52) |
| 110 |
for row in results: |
| 111 |
flag = "yes" if row["textual"] else "no" |
| 112 |
print(f"{row['step']:<24}{row['ms']:>8}{row['modules']:>10} {flag}") |
| 113 |
return 0 |
| 114 |
|
| 115 |
|
| 116 |
if __name__ == "__main__": |
| 117 |
raise SystemExit(main()) |