Files
gpu-rent/tests/test_timing.py
T
Leonid Pershin ccba40a228 Enhance logging and timing in CLI and session operations
- Updated the `_print_checks` function to replace console prints with logging functions for better traceability.
- Introduced timing functionality in the `doctor`, `dry_run`, and `up` functions to log the duration of preflight checks.
- Modified the `wait_ssh` function to accept a logging callback, improving SSH wait feedback.
- Enhanced the `mark` method in `PhaseTimes` to log phase durations, aiding in performance analysis.
- Updated various remote scripts to ensure error messages are printed to stderr for better error handling.
2026-08-21 08:03:04 +03:00

81 lines
2.5 KiB
Python

from gpu_rent.timing import (
PhaseTimes,
WaitLog,
clock_elapsed,
clock_prefix,
clock_reset,
format_duration,
)
def test_format_duration():
assert format_duration(5) == "5s"
assert format_duration(65) == "1m 5s"
assert format_duration(60) == "1m"
assert format_duration(3661) == "1h 1m"
def test_phase_times_summary(monkeypatch):
times = iter([100.0, 110.0, 130.0, 190.0])
monkeypatch.setattr("gpu_rent.timing.time.monotonic", lambda: next(times))
clock = PhaseTimes()
clock.mark("SSH")
clock.mark("bootstrap")
line = clock.summary_line()
assert "SSH 10s" in line
assert "bootstrap 20s" in line
assert "всего" in line
def test_phase_times_mark_logs(monkeypatch):
times = iter([0.0, 45.0])
monkeypatch.setattr("gpu_rent.timing.time.monotonic", lambda: next(times))
logs: list[str] = []
clock = PhaseTimes()
clock.mark("SSH", logs.append)
assert logs == ["фаза SSH: 45s · всего 45s"]
def test_phase_summary_lines_annotate_long_work(monkeypatch):
# t0=0, mark provision at 120s
times = iter([0.0, 120.0, 120.0])
monkeypatch.setattr("gpu_rent.timing.time.monotonic", lambda: next(times))
clock = PhaseTimes()
clock.mark("provision")
lines = clock.summary_lines()
assert lines[0] == "тайминг up:"
assert any("provision" in ln and "оптимизировать?" in ln for ln in lines)
assert any("seed/extensions/LLM" in ln for ln in lines)
def test_phase_summary_lines_annotate_wait(monkeypatch):
times = iter([0.0, 40.0, 40.0])
monkeypatch.setattr("gpu_rent.timing.time.monotonic", lambda: next(times))
clock = PhaseTimes()
clock.mark("SSH")
lines = clock.summary_lines()
assert any("SSH" in ln and "ожидание" in ln for ln in lines)
def test_wait_log_throttles(monkeypatch):
logs: list[str] = []
t = {"now": 0.0}
monkeypatch.setattr("gpu_rent.timing.time.monotonic", lambda: t["now"])
w = WaitLog(logs.append, every=30.0)
w.tick("a")
t["now"] = 10.0
w.tick("b")
t["now"] = 31.0
w.tick("c")
assert logs == ["a", "c"]
def test_clock_prefix(monkeypatch):
t = {"now": 100.0}
monkeypatch.setattr("gpu_rent.timing.time.monotonic", lambda: t["now"])
clock_reset()
assert clock_prefix() == "[+0s] "
t["now"] = 112.0
assert clock_prefix() == "[+12s] "
assert clock_elapsed() == 12.0