Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(server): bind StageTimer's clock as a module attribute

alan_chengae6e1dac16f3
diff --git a/jiuwenswarm/common/stage_timer.py b/jiuwenswarm/common/stage_timer.py
index 2712686a0a..5000165df0 100644
--- a/jiuwenswarm/common/stage_timer.py
+++ b/jiuwenswarm/common/stage_timer.py
@@ -13,7 +13,10 @@ one line, so a slow turn can be read straight out of the log stream.
from __future__ import annotations
-import time
+# Bound as a module attribute rather than used as ``time.monotonic`` so tests can
+# substitute a deterministic clock here without touching the global ``time``
+# module — patching that would also retime asyncio timers and socket timeouts.
+from time import monotonic
class StageTimer:
@@ -25,7 +28,7 @@ class StageTimer:
def __init__(self) -> None:
"""Start the clock for the first stage."""
- self._started_at = time.monotonic()
+ self._started_at = monotonic()
self._stage_started_at = self._started_at
self._stages: list[tuple[str, float]] = []
@@ -37,7 +40,7 @@ class StageTimer:
as separate entries rather than merged, so a loop that marks the
same name shows each pass.
"""
- now = time.monotonic()
+ now = monotonic()
self._stages.append((stage, (now - self._stage_started_at) * 1000))
self._stage_started_at = now
@@ -47,7 +50,7 @@ class StageTimer:
Returns:
Total elapsed time, including any work after the last ``mark``.
"""
- return (time.monotonic() - self._started_at) * 1000
+ return (monotonic() - self._started_at) * 1000
def render(self, *, slowest_first: bool = False) -> str:
"""Render the recorded stages as a single-line ``name=ms`` breakdown.
diff --git a/tests/unit_tests/common/test_stage_timer.py b/tests/unit_tests/common/test_stage_timer.py
index 00d4791782..c10a4d0bd8 100644
--- a/tests/unit_tests/common/test_stage_timer.py
+++ b/tests/unit_tests/common/test_stage_timer.py
@@ -13,7 +13,7 @@ from jiuwenswarm.common.stage_timer import StageTimer
def _fake_clock(monkeypatch: pytest.MonkeyPatch):
"""Drive the timer off a hand-advanced clock so timings are exact."""
now = {"t": 100.0}
- monkeypatch.setattr("jiuwenswarm.common.stage_timer.time.monotonic", lambda: now["t"])
+ monkeypatch.setattr("jiuwenswarm.common.stage_timer.monotonic", lambda: now["t"])
def advance(seconds: float) -> None:
now["t"] += seconds