From 2d4fe7c70ac599eafe316c7573757a46a1c16947 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 17 Jun 2024 17:38:51 -0600 Subject: [PATCH] Logging how long file searches take. --- wrolpi/common.py | 4 ++-- wrolpi/files/api.py | 7 ++++--- wrolpi/files/test/test_lib.py | 8 ++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/wrolpi/common.py b/wrolpi/common.py index 4645c923..b520d84d 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -1355,7 +1355,7 @@ def chunks_by_stem(it: List[Union[pathlib.Path, str, int]], size: int) -> Genera @contextlib.contextmanager -def timer(name, level: str = 'debug'): +def timer(name, level: str = 'debug', logger__: logging.Logger = logger_): """Prints out the time elapsed during the call of some block. Example: @@ -1364,7 +1364,7 @@ def timer(name, level: str = 'debug'): """ before = datetime.now() - log_method = getattr(logger_, level) + log_method = getattr(logger__, level) try: yield finally: diff --git a/wrolpi/files/api.py b/wrolpi/files/api.py index ce4c6944..b129554e 100644 --- a/wrolpi/files/api.py +++ b/wrolpi/files/api.py @@ -8,7 +8,7 @@ from sanic_ext import validate from sanic_ext.extensions.openapi import openapi from wrolpi.common import get_media_directory, wrol_mode_check, get_relative_to_media_directory, logger, \ - background_task, walk + background_task, walk, timer from wrolpi.errors import InvalidFile, UnknownDirectory, FileUploadFailed, FileConflict from . import lib, schema from ..api_utils import json_response, api_app @@ -98,8 +98,9 @@ async def refresh_progress(request: Request): ) @validate(schema.FilesSearchRequest) async def post_search_files(_: Request, body: schema.FilesSearchRequest): - file_groups, total = lib.search_files(body.search_str, body.limit, body.offset, body.mimetypes, body.model, - body.tag_names, body.headline, body.months, body.from_year, body.to_year) + with timer('Searching all files', 'info', logger__=logger): + file_groups, total = lib.search_files(body.search_str, body.limit, body.offset, body.mimetypes, body.model, + body.tag_names, body.headline, body.months, body.from_year, body.to_year) return json_response(dict(file_groups=file_groups, totals=dict(file_groups=total))) diff --git a/wrolpi/files/test/test_lib.py b/wrolpi/files/test/test_lib.py index 678313ee..094f00ae 100644 --- a/wrolpi/files/test/test_lib.py +++ b/wrolpi/files/test/test_lib.py @@ -383,15 +383,15 @@ async def test_refresh_a_text_no_indexer(test_async_client, test_session, make_f @pytest.mark.asyncio async def test_refresh_many_files(test_async_client, test_session, make_files_structure): """Used to profile file refreshing""" - count = 10_000 - make_files_structure([f'{uuid4()}.txt' for _ in range(count)]) + file_count = 10_000 + make_files_structure([f'{uuid4()}.txt' for _ in range(file_count)]) with timer('first refresh'): await lib.refresh_files() - assert test_session.query(FileGroup).count() == count + assert test_session.query(FileGroup).count() == file_count with timer('second refresh'): await lib.refresh_files() - assert test_session.query(FileGroup).count() == count + assert test_session.query(FileGroup).count() == file_count @pytest.mark.asyncio