Skip to content

decorators

django_spire.contrib.performance.decorators

P = ParamSpec('P') module-attribute

R = TypeVar('R') module-attribute

log = logging.getLogger(__name__) module-attribute

performance_timer

Source code in django_spire/contrib/performance/decorators.py
def performance_timer(func: Callable[P, R]) -> Callable[P, R]:
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
        if settings.DEBUG:
            start_time = time.perf_counter()
            result = func(*args, **kwargs)
            end_time = time.perf_counter()

            message = f'{end_time - start_time:.4f} seconds runtime for "{func.__module__}.{func.__qualname__}"'
            log.warning(message)

            return result

        return func(*args, **kwargs)

    return wrapper