Files
doczyai-pipelines/fieldExtraction/tests/test_timing_utils.py
T

36 lines
977 B
Python
Raw Normal View History

import time
from src.utils import timing_utils
def test_timing_stats_add_and_avg():
stats = timing_utils.TimingStats(name="unit")
stats.add(0.1)
stats.add(0.3)
assert stats.count == 2
assert stats.min_time == 0.1
assert stats.max_time == 0.3
assert abs(stats.avg_time - 0.2) < 1e-6
def test_timing_tracker_record_and_reset():
tracker = timing_utils.TimingTracker()
tracker.record("step", 0.05, context="fileA")
tracker.record("step", 0.10, context="fileA")
stats = tracker.get_stats()
assert "fileA.step" in stats
assert stats["fileA.step"].count == 2
tracker.reset()
assert tracker.get_stats() == {}
def test_timed_block_records_duration():
tracker = timing_utils.get_tracker()
tracker.reset()
with timing_utils.timed_block("sleep_test", context="unit"):
time.sleep(0.01)
stats = tracker.get_stats()
key = "unit.sleep_test"
assert key in stats
assert stats[key].count == 1