""" 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")