""" 성능 통계 관리자 각 모델별 처리 시간 통계를 수집하고 계산합니다. """ import time from threading import Lock class StatsManager: def __init__(self): self.data = {} self.lock = Lock() self.model_keys = ['simple_lama', 'migan', 'rembg'] self._initialize_data() def _initialize_data(self): """데이터 구조를 초기화합니다.""" for model in self.model_keys + ['total']: self.data[model] = { 'count': 0, 'total_time': 0.0, 'min_time': float('inf'), 'max_time': 0.0, } def record_time(self, model_name: str, duration: float, count: int = 1): """처리 시간을 기록합니다.""" if model_name not in self.model_keys: # logger.warning(f"Unknown model for stats: {model_name}") # Assuming logger is defined elsewhere return with self.lock: # 모델별 통계 업데이트 self.data[model_name]['count'] += count self.data[model_name]['total_time'] += duration * count # 전체 시간에 (평균시간 * 개수) 를 더함 self.data[model_name]['min_time'] = min(self.data[model_name]['min_time'], duration) self.data[model_name]['max_time'] = max(self.data[model_name]['max_time'], duration) # 전체 통계 업데이트 self.data['total']['count'] += count self.data['total']['total_time'] += duration * count self.data['total']['min_time'] = min(self.data['total']['min_time'], duration) self.data['total']['max_time'] = max(self.data['total']['max_time'], duration) def get_stats(self) -> dict: """현재까지의 통계를 계산하여 반환합니다.""" with self.lock: stats_summary = {} for model, stats in self.data.items(): if stats['count'] > 0: avg_time = stats['total_time'] / stats['count'] min_time = stats['min_time'] if stats['min_time'] != float('inf') else 0 else: avg_time = 0 min_time = 0 stats_summary[model] = { 'count': stats['count'], 'avg_time': f"{avg_time:.3f}", 'min_time': f"{min_time:.3f}", 'max_time': f"{stats['max_time']:.3f}", 'total_time': f"{stats['total_time']:.3f}", } return stats_summary def reset(self): """모든 통계를 초기화합니다.""" with self.lock: self._initialize_data() # 전역 통계 관리자 인스턴스 stats_manager = StatsManager()