feat(perf): Expand instrumentation with context manager and extended metrics

This commit is contained in:
2026-05-06 14:30:22 -04:00
parent 022c39888c
commit 23c1e21661
5 changed files with 240 additions and 136 deletions
+54
View File
@@ -26,3 +26,57 @@ def test_perf_monitor_component_timing() -> None:
metrics = pm.get_metrics()
assert metrics['time_test_comp_ms'] >= 10.0
pm.stop()
def test_perf_monitor_scope_context_manager() -> None:
pm = PerformanceMonitor()
pm.enabled = True
# Test normal usage
with pm.scope("test_scope"):
time.sleep(0.01)
metrics = pm.get_metrics()
assert metrics['time_test_scope_ms'] >= 10.0
# Test exception handling
try:
with pm.scope("test_error"):
time.sleep(0.01)
raise ValueError("test error")
except ValueError:
pass
metrics = pm.get_metrics()
# Component should still be finished, so timing should be recorded
assert metrics['time_test_error_ms'] >= 10.0
pm.stop()
def test_perf_monitor_extended_metrics() -> None:
pm = PerformanceMonitor()
pm.enabled = True
# 1st call: 10ms
pm.start_component("test_comp")
time.sleep(0.01)
pm.end_component("test_comp")
# 2nd call: 30ms
pm.start_component("test_comp")
time.sleep(0.03)
pm.end_component("test_comp")
# 3rd call: 20ms
pm.start_component("test_comp")
time.sleep(0.02)
pm.end_component("test_comp")
metrics = pm.get_metrics()
assert metrics['count_test_comp'] == 3.0
assert metrics['max_test_comp_ms'] >= 30.0
assert metrics['min_test_comp_ms'] >= 10.0
assert metrics['min_test_comp_ms'] < 20.0
pm.stop()