Files
douyin_guaji-main/douyin/logging/performance_tracker.py
T
CodexandClaude Sonnet 4.5 96fdc96f24 refactor(cleanup): 移除剩余死代码模块
- 删除 douyin/utils/douyin_text_utils.py 及其测试
- 移除 performance_tracker.py 中未使用的 track_function 方法
- 移除 param_usage_tracer.py 中未使用的 stop_tracing / is_enabled
- 清理 bin/run_cli.py 和 tests/test_imports.py 中的残留引用

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-05-23 21:58:41 +08:00

57 lines
1.9 KiB
Python

"""性能追踪模块:耗时统计与简单分析工具"""
from __future__ import annotations
import time
import threading
from typing import Callable, Dict, List, Optional
class PerformanceTracker:
def __init__(self) -> None:
self._start_times: Dict[str, float] = {}
self._durations: Dict[str, List[float]] = {}
self._lock = threading.Lock()
def start_timer(self, name: str) -> str:
with self._lock:
timer_id = f"{name}-{int(time.time()*1000)}"
self._start_times[timer_id] = time.perf_counter()
return timer_id
def end_timer(self, timer_id: str) -> float:
with self._lock:
start = self._start_times.pop(timer_id, None)
if start is None:
return 0.0
elapsed = time.perf_counter() - start
self._durations.setdefault(timer_id.split("-")[0], []).append(elapsed)
return elapsed
def get_elapsed(self, timer_id: str) -> float:
with self._lock:
start = self._start_times.get(timer_id)
if start is None:
return 0.0
return time.perf_counter() - start
def track_step(self, name: str) -> Callable:
def decorator(func: Callable) -> Callable:
def inner(*args, **kwargs):
t = self.start_timer(name)
try:
return func(*args, **kwargs)
finally:
self.end_timer(t)
return inner
return decorator
def get_statistics(self) -> Dict[str, float]:
# 返回一个简单的平均耗时统计字典,使用最近的 durations
with self._lock:
stats = {}
for key, arr in self._durations.items():
if arr:
stats[key] = sum(arr) / len(arr)
return stats
__all__ = ["PerformanceTracker"]