|
| 1 | +""" |
| 2 | +Performance tracking helpers. |
| 3 | +
|
| 4 | +This module provides a small, stable API used by examples. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import time |
| 10 | +from typing import Any, Dict, DefaultDict |
| 11 | +from collections import defaultdict |
| 12 | + |
| 13 | + |
| 14 | +class PerformanceTracker: |
| 15 | + """ |
| 16 | + Tracks latency/success per (modality, model_id). |
| 17 | +
|
| 18 | + Expected example surface: |
| 19 | + - get_current_time() -> float |
| 20 | + - track_latency(modality, latency) -> None |
| 21 | + - track_error(modality, error) -> None |
| 22 | + - get_modality_metrics(modality) -> dict[model_id] -> {"success_rate", "avg_latency"} |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self) -> None: |
| 26 | + # stats[modality][model_id] = {"success": int, "fail": int, "lat_total": float, "lat_count": int} |
| 27 | + self._stats: DefaultDict[str, Dict[str, Dict[str, Any]]] = defaultdict(dict) |
| 28 | + |
| 29 | + def _ensure(self, modality: str, model_id: str) -> Dict[str, Any]: |
| 30 | + if model_id not in self._stats[modality]: |
| 31 | + self._stats[modality][model_id] = { |
| 32 | + "success": 0, |
| 33 | + "fail": 0, |
| 34 | + "lat_total": 0.0, |
| 35 | + "lat_count": 0, |
| 36 | + } |
| 37 | + return self._stats[modality][model_id] |
| 38 | + |
| 39 | + def get_current_time(self) -> float: |
| 40 | + return time.time() |
| 41 | + |
| 42 | + def track_latency(self, modality: str, latency: float, model_id: str = "unknown", success: bool = True) -> None: |
| 43 | + stat = self._ensure(modality, model_id) |
| 44 | + if success: |
| 45 | + stat["success"] += 1 |
| 46 | + else: |
| 47 | + stat["fail"] += 1 |
| 48 | + try: |
| 49 | + lat = float(latency) |
| 50 | + except (TypeError, ValueError): |
| 51 | + lat = 0.0 |
| 52 | + stat["lat_total"] += lat |
| 53 | + stat["lat_count"] += 1 |
| 54 | + |
| 55 | + def track_error(self, modality: str, error: str, model_id: str = "unknown") -> None: |
| 56 | + stat = self._ensure(modality, model_id) |
| 57 | + stat["fail"] += 1 |
| 58 | + |
| 59 | + def track_success(self, modality: str, model_id: str = "unknown") -> None: |
| 60 | + """Convenience method for recording a success without latency.""" |
| 61 | + stat = self._ensure(modality, model_id) |
| 62 | + stat["success"] += 1 |
| 63 | + |
| 64 | + def get_modality_metrics(self, modality: str) -> Dict[str, Dict[str, Any]]: |
| 65 | + metrics: Dict[str, Dict[str, Any]] = {} |
| 66 | + for model_id, stat in self._stats.get(modality, {}).items(): |
| 67 | + success = int(stat.get("success", 0)) |
| 68 | + fail = int(stat.get("fail", 0)) |
| 69 | + total = success + fail |
| 70 | + lat_count = int(stat.get("lat_count", 0)) |
| 71 | + lat_total = float(stat.get("lat_total", 0.0)) |
| 72 | + metrics[model_id] = { |
| 73 | + "success_rate": (success / total) if total else 0.0, |
| 74 | + "avg_latency": (lat_total / lat_count) if lat_count else 0.0, |
| 75 | + "success": success, |
| 76 | + "fail": fail, |
| 77 | + } |
| 78 | + return metrics |
| 79 | + |
0 commit comments