Skip to content

Commit

Permalink
[PR #9939/509fddfd backport][3.11] Fix test_resolve_gitapi_subapps be…
Browse files Browse the repository at this point in the history
…nchmark (#9949)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
  • Loading branch information
patchback[bot] and asvetlov authored Nov 17, 2024
1 parent b8367c9 commit ce5225c
Showing 1 changed file with 120 additions and 28 deletions.
148 changes: 120 additions & 28 deletions tests/test_benchmarks_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ce5225c

Please sign in to comment.