mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
Create performance_monitor.py
This commit is contained in:
parent
7553476ca4
commit
2866b9e7f7
1 changed files with 49 additions and 0 deletions
49
core/utils/performance_monitor.py
Normal file
49
core/utils/performance_monitor.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import time
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List
|
||||
|
||||
@dataclass
|
||||
class ModuleStats:
|
||||
total_time: float = 0.0
|
||||
call_count: int = 0
|
||||
max_time: float = 0.0
|
||||
min_time: float = float('inf')
|
||||
history: List[float] = field(default_factory=list)
|
||||
|
||||
class PerformanceMonitor:
|
||||
def __init__(self):
|
||||
self.stats: Dict[str, ModuleStats] = defaultdict(ModuleStats)
|
||||
self.current_session = {}
|
||||
|
||||
def record(self, module: str, duration: float):
|
||||
stats = self.stats[module]
|
||||
stats.total_time += duration
|
||||
stats.call_count += 1
|
||||
stats.max_time = max(stats.max_time, duration)
|
||||
stats.min_time = min(stats.min_time, duration)
|
||||
stats.history.append(duration)
|
||||
|
||||
def get_summary(self) -> Dict:
|
||||
return {
|
||||
module: {
|
||||
"avg": stats.total_time / stats.call_count if stats.call_count else 0,
|
||||
"max": stats.max_time,
|
||||
"min": stats.min_time,
|
||||
"total_calls": stats.call_count
|
||||
}
|
||||
for module, stats in self.stats.items()
|
||||
}
|
||||
|
||||
monitor = PerformanceMonitor()
|
||||
|
||||
def track_performance(module_name: str):
|
||||
def decorator(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
start = time.perf_counter()
|
||||
result = func(*args, **kwargs)
|
||||
duration = time.perf_counter() - start
|
||||
monitor.record(module_name, duration)
|
||||
return result
|
||||
return wrapper
|
||||
return decorator
|
||||
Loading…
Reference in a new issue