From 4c328c0d28895b4bb63e16843dfb0542231ee954 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 17 Nov 2024 22:12:56 +0000 Subject: [PATCH] [PR #9939/509fddfd backport][3.12] Fix test_resolve_gitapi_subapps benchmark (#9950) Co-authored-by: Andrew Svetlov --- tests/test_benchmarks_web_urldispatcher.py | 148 +++++++++++++++++---- 1 file changed, 120 insertions(+), 28 deletions(-) diff --git a/tests/test_benchmarks_web_urldispatcher.py b/tests/test_benchmarks_web_urldispatcher.py index 662a600c3b3..033e2543a73 100644 --- a/tests/test_benchmarks_web_urldispatcher.py +++ b/tests/test_benchmarks_web_urldispatcher.py @@ -6,7 +6,7 @@ import random import string from pathlib import Path -from typing import NoReturn +from typing import NoReturn, Optional from unittest import mock from multidict import CIMultiDict, CIMultiDictProxy @@ -53,9 +53,16 @@ async def handler(request: web.Request) -> NoReturn: router = app.router request = _mock_request(method="GET", path="/") - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for _ in range(resolve_count): - await router.resolve(request) + ret = await router.resolve(request) + + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ret.get_info()["path"] == "/", ret.get_info() @benchmark def _run() -> None: @@ -70,14 +77,22 @@ def test_resolve_static_root_route( resolve_count = 100 app = web.Application() - app.router.add_static("/", pathlib.Path(aiohttp.__file__).parent) + here = pathlib.Path(aiohttp.__file__).parent + app.router.add_static("/", here) app.freeze() router = app.router request = _mock_request(method="GET", path="/") - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for _ in range(resolve_count): - await router.resolve(request) + ret = await router.resolve(request) + + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ret.get_info()["directory"] == here, ret.get_info() @benchmark def _run() -> None: @@ -101,9 +116,16 @@ async def handler(request: web.Request) -> NoReturn: router = app.router request = _mock_request(method="GET", path="/api/server/dispatch/1/update") - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for _ in range(resolve_count): - await router.resolve(request) + ret = await router.resolve(request) + + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ret.get_info()["path"] == "/api/server/dispatch/1/update", ret.get_info() @benchmark def _run() -> None: @@ -130,9 +152,15 @@ async def handler(request: web.Request) -> NoReturn: for count in range(250) ] - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for request in requests: - await router.resolve(request) + ret = await router.resolve(request) + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ret.get_info()["path"] == "/api/server/dispatch/249/update", ret.get_info() @benchmark def _run() -> None: @@ -162,11 +190,18 @@ async def handler(request: web.Request) -> NoReturn: app.freeze() router = app.router - requests = [_mock_request(method="GET", path=url) for url in urls] + requests = [(_mock_request(method="GET", path=url), url) for url in urls] - async def run_url_dispatcher_benchmark() -> None: - for request in requests: - await router.resolve(request) + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None + for request, path in requests: + ret = await router.resolve(request) + + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ret.get_info()["path"] == url, ret.get_info() @benchmark def _run() -> None: @@ -194,9 +229,18 @@ async def handler(request: web.Request) -> NoReturn: for customer in range(250) ] - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for request in requests: - await router.resolve(request) + ret = await router.resolve(request) + + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ( + ret.get_info()["formatter"] == "/api/server/dispatch/{customer}/update" + ), ret.get_info() @benchmark def _run() -> None: @@ -226,9 +270,18 @@ async def handler(request: web.Request) -> NoReturn: for customer in range(250) ] - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for request in requests: - await router.resolve(request) + ret = await router.resolve(request) + + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ( + ret.get_info()["formatter"] == "/api/server/dispatch/{customer}/update" + ), ret.get_info() @benchmark def _run() -> None: @@ -256,9 +309,16 @@ async def handler(request: web.Request) -> NoReturn: for customer in range(250) ] - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for request in requests: - await router.resolve(request) + ret = await router.resolve(request) + + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ret.get_info()["formatter"] == "/api/{customer}/update", ret.get_info() @benchmark def _run() -> None: @@ -306,9 +366,18 @@ async def handler(request: web.Request) -> NoReturn: ) ) - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for request in requests: - await router.resolve(request) + ret = await router.resolve(request) + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ( + ret.get_info()["formatter"] + == "/repos/{owner}/{repo}/pulls/{pull_number}/reviews" + ), ret.get_info() @benchmark def _run() -> None: @@ -346,8 +415,14 @@ async def handler(request: web.Request) -> NoReturn: app = web.Application() for url in urls: parts = url.split("/") - subapp = subapps.get(parts[1], app) - subapp.router.add_get(url, handler) + subapp = subapps.get(parts[1]) + if subapp is not None: + sub_url = "/".join([""] + parts[2:]) + if not sub_url: + sub_url = "/" + subapp.router.add_get(sub_url, handler) + else: + app.router.add_get(url, handler) for key, subapp in subapps.items(): app.add_subapp("/" + key, subapp) app.freeze() @@ -370,9 +445,18 @@ async def handler(request: web.Request) -> NoReturn: ) ) - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for request in requests: - await router.resolve(request) + ret = await router.resolve(request) + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ( + ret.get_info()["formatter"] + == "/repos/{owner}/{repo}/pulls/{pull_number}/reviews" + ), ret.get_info() @benchmark def _run() -> None: @@ -404,9 +488,17 @@ async def handler(request: web.Request) -> NoReturn: for customer in range(250) ] - async def run_url_dispatcher_benchmark() -> None: + async def run_url_dispatcher_benchmark() -> Optional[web.UrlMappingMatchInfo]: + ret = None for request in requests: - await router.resolve(request) + ret = await router.resolve(request) + return ret + + ret = loop.run_until_complete(run_url_dispatcher_benchmark()) + assert ret is not None + assert ( + ret.get_info()["path"] == "/api/path/to/plugin/249/deep/enough/sub/path" + ), ret.get_info() @benchmark def _run() -> None: