From 58a78f63083c82bbe71f85fcadf1ef0447e55b62 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 16 Nov 2024 19:11:32 -0600 Subject: [PATCH] [PR #9929/73691e49 backport][3.10] Add couple benchmarks for dynamic routes (#9930) Co-authored-by: Andrew Svetlov --- tests/test_benchmarks_web_urldispatcher.py | 64 +++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/tests/test_benchmarks_web_urldispatcher.py b/tests/test_benchmarks_web_urldispatcher.py index 452f4afa7b3..16a59516b35 100644 --- a/tests/test_benchmarks_web_urldispatcher.py +++ b/tests/test_benchmarks_web_urldispatcher.py @@ -173,7 +173,7 @@ def _run() -> None: loop.run_until_complete(run_url_dispatcher_benchmark()) -def test_resolve_dynamic_resource_url_with_many_routes( +def test_resolve_dynamic_resource_url_with_many_static_routes( loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture, ) -> None: @@ -203,6 +203,68 @@ def _run() -> None: loop.run_until_complete(run_url_dispatcher_benchmark()) +def test_resolve_dynamic_resource_url_with_many_dynamic_routes( + loop: asyncio.AbstractEventLoop, + benchmark: BenchmarkFixture, +) -> None: + """Resolve different a DynamicResource when there are 250 DynamicResources registered.""" + + async def handler(request: web.Request) -> NoReturn: + assert False + + app = web.Application() + for count in range(250): + app.router.add_route( + "GET", f"/api/server/other/{{customer}}/update{count}", handler + ) + app.router.add_route("GET", "/api/server/dispatch/{customer}/update", handler) + app.freeze() + router = app.router + + requests = [ + _mock_request(method="GET", path=f"/api/server/dispatch/{customer}/update") + for customer in range(250) + ] + + async def run_url_dispatcher_benchmark() -> None: + for request in requests: + await router.resolve(request) + + @benchmark + def _run() -> None: + loop.run_until_complete(run_url_dispatcher_benchmark()) + + +def test_resolve_dynamic_resource_url_with_many_dynamic_routes_with_common_prefix( + loop: asyncio.AbstractEventLoop, + benchmark: BenchmarkFixture, +) -> None: + """Resolve different a DynamicResource when there are 250 DynamicResources registered with the same common prefix.""" + + async def handler(request: web.Request) -> NoReturn: + assert False + + app = web.Application() + for count in range(250): + app.router.add_route("GET", f"/api/{{customer}}/show_{count}", handler) + app.router.add_route("GET", "/api/{customer}/update", handler) + app.freeze() + router = app.router + + requests = [ + _mock_request(method="GET", path=f"/api/{customer}/update") + for customer in range(250) + ] + + async def run_url_dispatcher_benchmark() -> None: + for request in requests: + await router.resolve(request) + + @benchmark + def _run() -> None: + loop.run_until_complete(run_url_dispatcher_benchmark()) + + def test_resolve_gitapi( loop: asyncio.AbstractEventLoop, benchmark: BenchmarkFixture,