carrotai-chat / shared /timing.py
Jaco9921's picture
Initial deploy: CarrotAI chat service
8f936fd verified
Raw
History Blame Contribute Delete
914 Bytes
"""
Simple timing utilities for performance logging.
"""
import time
import logging
from contextlib import contextmanager
from typing import Optional
logger = logging.getLogger(__name__)
@contextmanager
def timed(operation: str, log_level: int = logging.INFO, threshold_ms: Optional[float] = None):
"""
Context manager for timing operations.
Args:
operation: Description of the operation being timed
log_level: Logging level (default INFO)
threshold_ms: Only log if duration exceeds this threshold (milliseconds)
Usage:
with timed("LLM extraction"):
result = llm_complete_json(...)
"""
start = time.perf_counter()
try:
yield
finally:
elapsed_ms = (time.perf_counter() - start) * 1000
if threshold_ms is None or elapsed_ms >= threshold_ms:
logger.log(log_level, f"{operation}: {elapsed_ms:.0f}ms")