|
| 1 | +import functools |
| 2 | +import time |
| 3 | +import tracemalloc |
| 4 | +import psutil |
| 5 | +import logging |
| 6 | + |
| 7 | + |
| 8 | +def track_metrics(metrics=("time", "memory", "cpu")): |
| 9 | + """Decorator to track specified metrics (time, memory, cpu) during function execution. |
| 10 | + The decorator logs the metrics using the provided logger or a default logger if none is provided. |
| 11 | + Args: |
| 12 | + metrics (tuple): Metrics to track. Options are "time", "memory", "cpu". |
| 13 | + Usage: |
| 14 | + @track_metrics(metrics=("time", "memory", "cpu")) |
| 15 | + def example_function(): |
| 16 | + data = [i for i in range(10**6)] # Simulate work |
| 17 | + time.sleep(1) # Simulate delay |
| 18 | + return sum(data) |
| 19 | + """ |
| 20 | + |
| 21 | + def decorator(funct): |
| 22 | + @functools.wraps(funct) |
| 23 | + def wrapper(*args, **kwargs): |
| 24 | + logger = kwargs.get("logger") |
| 25 | + if not logger: |
| 26 | + # Use a default logger if none is provided |
| 27 | + logger = logging.getLogger(funct.__name__) |
| 28 | + |
| 29 | + process = psutil.Process() |
| 30 | + tracemalloc.start() if "memory" in metrics else None |
| 31 | + start_time = time.time() if "time" in metrics else None |
| 32 | + cpu_before = ( |
| 33 | + process.cpu_percent(interval=None) if "cpu" in metrics else None |
| 34 | + ) |
| 35 | + |
| 36 | + try: |
| 37 | + result = funct(*args, **kwargs) |
| 38 | + except Exception as e: |
| 39 | + logger.error(f"Function '{funct.__name__}' raised an exception: {e}") |
| 40 | + raise |
| 41 | + finally: |
| 42 | + metrics_message = "" |
| 43 | + if "time" in metrics: |
| 44 | + duration = time.time() - start_time |
| 45 | + metrics_message = f"time: {duration:.2f} seconds" |
| 46 | + if "memory" in metrics: |
| 47 | + current, peak = tracemalloc.get_traced_memory() |
| 48 | + tracemalloc.stop() |
| 49 | + if metrics_message: |
| 50 | + metrics_message += ", " |
| 51 | + metrics_message += f"memory: {current / (1024 ** 2):.2f} MB (peak: {peak / (1024 ** 2):.2f} MB)" |
| 52 | + if "cpu" in metrics: |
| 53 | + cpu_after = process.cpu_percent(interval=None) |
| 54 | + if metrics_message: |
| 55 | + metrics_message += ", " |
| 56 | + metrics_message += f"cpu: {cpu_after - cpu_before:.2f}%" |
| 57 | + if len(metrics_message) > 0: |
| 58 | + logger.info( |
| 59 | + "Function metrics('%s'): %s", funct.__name__, metrics_message |
| 60 | + ) |
| 61 | + return result |
| 62 | + |
| 63 | + return wrapper |
| 64 | + |
| 65 | + return decorator |
0 commit comments