diff --git a/nonebot_plugin_tetris_stats/utils/screenshot.py b/nonebot_plugin_tetris_stats/utils/screenshot.py index fa4579f..48a9da2 100644 --- a/nonebot_plugin_tetris_stats/utils/screenshot.py +++ b/nonebot_plugin_tetris_stats/utils/screenshot.py @@ -4,11 +4,13 @@ from playwright.async_api import TimeoutError, ViewportSize from ..config.config import Config from .browser import BrowserManager from .retry import retry +from .time_it import time_it config = get_plugin_config(Config) @retry(exception_type=TimeoutError, reply='截图失败, 重试中') +@time_it async def screenshot(url: str) -> bytes: browser = await BrowserManager.get_browser() async with ( diff --git a/nonebot_plugin_tetris_stats/utils/time_it.py b/nonebot_plugin_tetris_stats/utils/time_it.py new file mode 100644 index 0000000..1706b4d --- /dev/null +++ b/nonebot_plugin_tetris_stats/utils/time_it.py @@ -0,0 +1,21 @@ +from collections.abc import Callable, Coroutine +from functools import wraps +from time import time_ns +from typing import Any, ParamSpec, TypeVar + +from nonebot.log import logger + +T = TypeVar('T') +P = ParamSpec('P') + + +def time_it(func: Callable[P, Coroutine[Any, Any, T]]) -> Callable[P, Coroutine[Any, Any, T]]: + @wraps(func) + async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: + start = time_ns() + try: + return await func(*args, **kwargs) + finally: + logger.debug(f'{func.__name__} took {(time_ns() - start) / 1e6}ms') + + return wrapper