-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a decorator that logs how long a function took / was slow.
- Loading branch information
Showing
5 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import time | ||
from typing import Callable, TypeVar | ||
|
||
from typing_extensions import ParamSpec | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
ReturnType = TypeVar("ReturnType") | ||
ParametersType = ParamSpec("ParametersType") | ||
|
||
|
||
def log_runtime( | ||
runtime_warning_threshold: float = 3.0, | ||
) -> Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]: | ||
"""Logs how long a function took to run. | ||
If the runtime exceeds runtime_warning_threshold, then a warning is logged. | ||
""" | ||
|
||
def decorator(wrapped_function: Callable[ParametersType, ReturnType]) -> Callable[ParametersType, ReturnType]: | ||
def _inner(*args: ParametersType.args, **kwargs: ParametersType.kwargs) -> ReturnType: | ||
# __qualname__ includes the path like MyClass.my_function | ||
function_name = f"{wrapped_function.__qualname__}()" | ||
start_time = time.time() | ||
logger.info(f"Starting {function_name}") | ||
|
||
try: | ||
result = wrapped_function(*args, **kwargs) | ||
finally: | ||
runtime = time.time() - start_time | ||
logger.info(f"Finished {function_name} in {runtime:.1f}s") | ||
if runtime > runtime_warning_threshold: | ||
logger.warning(f"{function_name} is slow with a runtime of {runtime:.1f}s") | ||
|
||
return result | ||
|
||
return _inner | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters