00eaa460fd
hot_reloader.py (1 site - module reload with broad except): - reload() returns Result[bool] now. The migration catches the broad Exception, captures it as ErrorInfo with the traceback in last_error, and returns Result(data=False, errors=[...]). - reload_all() returns Result[bool]; aggregates per-module errors. - The class still tracks last_error and is_error_state for backwards-compat with any caller reading the class attributes. warmup.py (5 sites): - L139 (on_complete callback fire): was except ...: pass. Now logs to sys.stderr with the exception. - L215 (_record_success callback fire): same. - L249 (_record_failure callback fire): same. - L276 (_log_canary stderr.write): was except OSError: pass. Now logs the OSError itself. - L300 (_log_summary stderr.write): same. startup_profiler.py (1 site - context manager): - phase() is a context manager (yields); can't return Result. The except inside the finally block now logs the OSError. Tests updated for hot_reloader to check result.ok and result.data. Tests verified: - tests/test_hot_reloader.py (9 tests) PASS - tests/test_hot_reload_integration.py (13 tests) PASS - tests/test_warmup.py (10 tests) PASS - tests/test_warmup_canaries.py (18 tests) PASS
64 lines
1.4 KiB
Python
64 lines
1.4 KiB
Python
import time
|
|
import sys
|
|
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)
|
|
try:
|
|
sys.stderr.write(f"[startup] {name}: {(p.end_ts - p.start_ts) * 1000.0:.1f}ms\n")
|
|
sys.stderr.flush()
|
|
except OSError as e:
|
|
sys.stderr.write(f"[startup] phase output failed for {name}: {e}\n")
|
|
|
|
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()
|
|
|
|
|
|
startup_profiler: StartupProfiler = StartupProfiler()
|