Skip to content

Commit

Permalink
Add couple benchmarks for dynamic routes (#9929)
Browse files Browse the repository at this point in the history
It helps tuning UrlDispatcher performance
  • Loading branch information
asvetlov authored Nov 16, 2024
1 parent 69756b4 commit 73691e4
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion tests/test_benchmarks_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 73691e4

Please sign in to comment.