Skip to content

Commit 9c81667

Browse files
[PR #9935/0c312496 backport][3.11] Add benchmark for routing sub_applications (#9937)
**This is a backport of PR #9935 as merged into master (0c31249).** Router should be optimized for handling 2 kind of prefix resources (sub_apps and static files are handled equally now): 1. sub_apps are placed under non-overlapped top-level single-segment prefixed. `test_resolve_gitapi_subapps` benchmark test this case already. 2. sub_apps shares the same prefix segments, e.g. `/api/manager/plugin/{name}`. This PR covers the second case. Co-authored-by: Andrew Svetlov <[email protected]>
1 parent 07bf925 commit 9c81667

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/test_benchmarks_web_urldispatcher.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,37 @@ async def run_url_dispatcher_benchmark() -> None:
377377
@benchmark
378378
def _run() -> None:
379379
loop.run_until_complete(run_url_dispatcher_benchmark())
380+
381+
382+
def test_resolve_prefix_resources_many_prefix_many_plain(
383+
loop: asyncio.AbstractEventLoop,
384+
benchmark: BenchmarkFixture,
385+
) -> None:
386+
"""Resolve prefix resource (sub_app) whene 250 PlainResources registered and there are 250 subapps that shares the same sub_app path prefix."""
387+
388+
async def handler(request: web.Request) -> NoReturn:
389+
assert False
390+
391+
app = web.Application()
392+
for count in range(250):
393+
app.router.add_get(f"/api/server/other/{count}/update", handler)
394+
for count in range(250):
395+
subapp = web.Application()
396+
# sub_apps exists for handling deep enough nested route trees
397+
subapp.router.add_get("/deep/enough/sub/path", handler)
398+
app.add_subapp(f"/api/path/to/plugin/{count}", subapp)
399+
app.freeze()
400+
router = app.router
401+
402+
requests = [
403+
_mock_request(method="GET", path="/api/path/to/plugin/249/deep/enough/sub/path")
404+
for customer in range(250)
405+
]
406+
407+
async def run_url_dispatcher_benchmark() -> None:
408+
for request in requests:
409+
await router.resolve(request)
410+
411+
@benchmark
412+
def _run() -> None:
413+
loop.run_until_complete(run_url_dispatcher_benchmark())

0 commit comments

Comments
 (0)