| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Provides a timeslice logging decorator.""" |
| 4 | |
| 5 | import functools |
| 6 | import time |
| 7 | |
| 8 | |
| 9 | def logtime(logger, msg=None): |
| 10 | """Logs the execution time of the decorated function. |
| 11 | Always place it beneath other decorators. |
| 12 | """ |
| 13 | |
| 14 | def _inner(func): |
| 15 | @functools.wraps(func) |
| 16 | def _wrapper(*args, **kwargs): |
| 17 | start = time.perf_counter() |
| 18 | result = func(*args, **kwargs) |
| 19 | elapsed = time.perf_counter() - start |
| 20 | |
| 21 | prefix = ( |
| 22 | f"Function '{func.__module__}.{func.__qualname__}'" |
| 23 | if msg is None |
| 24 | else msg |
| 25 | ) |
| 26 | logger.debug("%s: Elapsed time %.7f secs", prefix, elapsed) |
| 27 | return result |
| 28 | |
| 29 | return _wrapper |
| 30 | |
| 31 | return _inner |