5a85653654
Lightweight, in-memory profiler for AppController init phases. Used by
the startup_speedup_20260606 track to measure where the time goes
during boot (config hydration, hook server start, subsystem init, etc.).
The profiler is exposed via /api/startup_profile (Phase 8 work) and
the Diagnostics panel so the user can see the exact per-phase cost.
Public API:
StartupProfiler() - create
.phase(name) - context manager
.snapshot() - {phases: {name: {start_ts, duration_ms}}, total_ms, count}
.reset() - clear recorded phases
.enable() / .disable() - toggle recording
Implementation:
- dataclass with list of _Phase(name, start_ts, end_ts)
- @contextmanager records wall-clock via time.perf_counter
- records duration even if the body raises (try/finally)
- snapshot is a copy, so consumers can't mutate the live state
TDD: 5 tests in tests/test_startup_profiler.py cover: basic
recording, total math, snapshot isolation, exception safety, empty
state.
55 lines
1.1 KiB
Python
55 lines
1.1 KiB
Python
import time
|
|
from contextlib import contextmanager
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Iterator
|
|
|
|
|
|
@dataclass
|
|
class _Phase:
|
|
name: str
|
|
start_ts: float
|
|
end_ts: float = 0.0
|
|
|
|
|
|
@dataclass
|
|
class StartupProfiler:
|
|
_phases: list[_Phase] = field(default_factory=list)
|
|
_enabled: bool = True
|
|
|
|
def enable(self) -> None:
|
|
self._enabled = True
|
|
|
|
def disable(self) -> None:
|
|
self._enabled = False
|
|
|
|
@contextmanager
|
|
def phase(self, name: str) -> Iterator[None]:
|
|
if not self._enabled:
|
|
yield
|
|
return
|
|
p = _Phase(name=name, start_ts=time.perf_counter())
|
|
try:
|
|
yield
|
|
finally:
|
|
p.end_ts = time.perf_counter()
|
|
self._phases.append(p)
|
|
|
|
def snapshot(self) -> dict[str, Any]:
|
|
phases: dict[str, dict[str, float]] = {}
|
|
total = 0.0
|
|
for p in self._phases:
|
|
duration_ms = max(0.0, (p.end_ts - p.start_ts) * 1000.0) if p.end_ts else 0.0
|
|
phases[p.name] = {
|
|
"start_ts": p.start_ts,
|
|
"duration_ms": round(duration_ms, 3),
|
|
}
|
|
total += duration_ms
|
|
return {
|
|
"phases": phases,
|
|
"total_ms": round(total, 3),
|
|
"count": len(phases),
|
|
}
|
|
|
|
def reset(self) -> None:
|
|
self._phases.clear()
|