diff --git a/tests/test_performance_tracker.py b/tests/test_performance_tracker.py new file mode 100644 index 0000000..78076ef --- /dev/null +++ b/tests/test_performance_tracker.py @@ -0,0 +1,137 @@ +"""douyin/logging/performance_tracker.PerformanceTracker 的 pytest 单元测试。 + +覆盖:start_timer / end_timer / get_elapsed / track_function / track_step / +get_statistics 共 6 个方法。 +""" +from __future__ import annotations + +import time + +import pytest + +from douyin.logging.performance_tracker import PerformanceTracker + + +@pytest.fixture +def tracker(): + return PerformanceTracker() + + +# ============================================================ +# start_timer / end_timer +# ============================================================ + +class TestStartEndTimer: + def test_start_returns_unique_id(self, tracker): + a = tracker.start_timer("foo") + b = tracker.start_timer("foo") + # 同名 timer 也应返回不同 id(包含时间戳) + assert isinstance(a, str) + assert isinstance(b, str) + + def test_end_returns_elapsed(self, tracker): + t = tracker.start_timer("foo") + time.sleep(0.01) + elapsed = tracker.end_timer(t) + assert elapsed > 0 + + def test_end_unknown_id_returns_zero(self, tracker): + # 未知 id 返回 0.0,不抛 + assert tracker.end_timer("nonexistent-1234") == 0.0 + + def test_end_idempotent(self, tracker): + t = tracker.start_timer("foo") + tracker.end_timer(t) + # 第二次 end 同 id 应返回 0.0(已被 pop) + assert tracker.end_timer(t) == 0.0 + + +# ============================================================ +# get_elapsed +# ============================================================ + +class TestGetElapsed: + def test_active_timer(self, tracker): + t = tracker.start_timer("foo") + time.sleep(0.01) + e = tracker.get_elapsed(t) + assert e > 0 + + def test_unknown_returns_zero(self, tracker): + assert tracker.get_elapsed("nonexistent") == 0.0 + + def test_after_end_returns_zero(self, tracker): + t = tracker.start_timer("foo") + tracker.end_timer(t) + # end 后 _start_times 已清,应返回 0 + assert tracker.get_elapsed(t) == 0.0 + + +# ============================================================ +# track_function 装饰器 +# ============================================================ + +class TestTrackFunction: + def test_wraps_and_returns_value(self, tracker): + @tracker.track_function + def add(a, b): + return a + b + + assert add(2, 3) == 5 + + def test_records_duration(self, tracker): + @tracker.track_function + def slow(): + time.sleep(0.01) + + slow() + stats = tracker.get_statistics() + # 函数名 = "TestTrackFunction.test_records_duration..slow" + # 记录在以 qualname 为前缀的 key 上 + assert any("slow" in k for k in stats), f"未记录 slow 的统计:{stats}" + + def test_exception_still_records(self, tracker): + @tracker.track_function + def raises(): + raise ValueError("boom") + + with pytest.raises(ValueError): + raises() + stats = tracker.get_statistics() + # 即使函数抛异常,时长也应被记录 + assert any("raises" in k for k in stats) + + +# ============================================================ +# track_step 装饰器 +# ============================================================ + +class TestTrackStep: + def test_records_under_custom_name(self, tracker): + @tracker.track_step("custom_step") + def work(): + return 42 + + assert work() == 42 + stats = tracker.get_statistics() + assert "custom_step" in stats + + +# ============================================================ +# get_statistics +# ============================================================ + +class TestGetStatistics: + def test_empty(self, tracker): + assert tracker.get_statistics() == {} + + def test_average_across_runs(self, tracker): + # 同一个 name 跑两次,统计应是平均 + for _ in range(2): + t = tracker.start_timer("repeat") + time.sleep(0.005) + tracker.end_timer(t) + stats = tracker.get_statistics() + assert "repeat" in stats + # 平均值应大于 0 且小于一个合理上限 + assert 0 < stats["repeat"] < 1.0