From 359331f5791b187b9df30f46666001b920532a1f Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Thu, 7 Mar 2024 11:47:50 +0100 Subject: [PATCH] ENH: print subhook execution times (#306) --- src/compwa_policy/utilities/executor.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/compwa_policy/utilities/executor.py b/src/compwa_policy/utilities/executor.py index 0c77cd47..8b211db7 100644 --- a/src/compwa_policy/utilities/executor.py +++ b/src/compwa_policy/utilities/executor.py @@ -3,6 +3,7 @@ from __future__ import annotations import sys +import time from typing import Callable, TypeVar import attr @@ -32,11 +33,19 @@ def __call__( ) -> T | None: """Execute a function and collect any `.PrecommitError` exceptions.""" try: - return function(*args, **kwargs) + start_time = time.time() + result = function(*args, **kwargs) + end_time = time.time() + execution_time = end_time - start_time + if execution_time > 0.1: # noqa: PLR2004 + function_name = f"{function.__module__}.{function.__name__}" + print(f"{execution_time:>7.2f} s {function_name}") # noqa: T201 except PrecommitError as exception: error_message = str("\n".join(exception.args)) - self.error_messages.append(error_message) - return None + self.error_messages.append(error_message) + return None + else: + return result def finalize(self, exception: bool = True) -> int: error_msg = self.merge_messages()