def log_exec_time()

in cost-optimization/hpa-config-recommender/src/hpaconfigrecommender/utils/log.py [0:0]


def log_exec_time(logger):
    """
    Log the execution time of the decorated function, supporting both
    synchronous and asynchronous functions.
    """
    def decorator(func):
        if asyncio.iscoroutinefunction(func):  # Check if the function is async
            @functools.wraps(func)
            async def wrapper(*args, **kwargs):
                start_time = perftime.perf_counter()
                result = await func(*args, **kwargs)  # Await async function
                end_time = perftime.perf_counter()
                execution_time = end_time - start_time
                logger.info("[%s] [%s] Execution time: %.4f seconds",
                             func.__name__, execution_time)
                return result
        else:  # Handle synchronous functions
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                start_time = perftime.perf_counter()
                result = func(*args, **kwargs)
                end_time = perftime.perf_counter()
                execution_time = end_time - start_time
                logger.info("[%s] Execution time: %.4f seconds",
                              func.__name__, execution_time)
                return result
        return wrapper
    return decorator