Files
gps-denied-onboard/helpers/performance_monitor.py
T
Oleksandr Bezdieniezhnykh abc26d5c20 initial structure implemented
docs -> _docs
2025-12-01 14:20:56 +02:00

45 lines
1.3 KiB
Python

import time
from typing import Optional
from contextlib import contextmanager
class PerformanceMonitor:
def __init__(self):
self._timings: dict[str, list[float]] = {}
self._counters: dict[str, int] = {}
@contextmanager
def measure(self, operation: str):
start = time.perf_counter()
try:
yield
finally:
elapsed = time.perf_counter() - start
if operation not in self._timings:
self._timings[operation] = []
self._timings[operation].append(elapsed)
def increment(self, counter: str, value: int = 1) -> None:
if counter not in self._counters:
self._counters[counter] = 0
self._counters[counter] += value
def get_average(self, operation: str) -> Optional[float]:
if operation not in self._timings or not self._timings[operation]:
return None
return sum(self._timings[operation]) / len(self._timings[operation])
def get_statistics(self, operation: str) -> Optional[dict]:
raise NotImplementedError
def get_counter(self, counter: str) -> int:
return self._counters.get(counter, 0)
def reset(self) -> None:
self._timings.clear()
self._counters.clear()
def get_report(self) -> dict:
raise NotImplementedError