Skip to content

Commit 07bf925

Browse files
[PR #9929/73691e49 backport][3.11] Add couple benchmarks for dynamic routes (#9931)
Co-authored-by: Andrew Svetlov <[email protected]>
1 parent 163405f commit 07bf925

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

tests/test_benchmarks_web_urldispatcher.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def _run() -> None:
173173
loop.run_until_complete(run_url_dispatcher_benchmark())
174174

175175

176-
def test_resolve_dynamic_resource_url_with_many_routes(
176+
def test_resolve_dynamic_resource_url_with_many_static_routes(
177177
loop: asyncio.AbstractEventLoop,
178178
benchmark: BenchmarkFixture,
179179
) -> None:
@@ -203,6 +203,68 @@ def _run() -> None:
203203
loop.run_until_complete(run_url_dispatcher_benchmark())
204204

205205

206+
def test_resolve_dynamic_resource_url_with_many_dynamic_routes(
207+
loop: asyncio.AbstractEventLoop,
208+
benchmark: BenchmarkFixture,
209+
) -> None:
210+
"""Resolve different a DynamicResource when there are 250 DynamicResources registered."""
211+
212+
async def handler(request: web.Request) -> NoReturn:
213+
assert False
214+
215+
app = web.Application()
216+
for count in range(250):
217+
app.router.add_route(
218+
"GET", f"/api/server/other/{{customer}}/update{count}", handler
219+
)
220+
app.router.add_route("GET", "/api/server/dispatch/{customer}/update", handler)
221+
app.freeze()
222+
router = app.router
223+
224+
requests = [
225+
_mock_request(method="GET", path=f"/api/server/dispatch/{customer}/update")
226+
for customer in range(250)
227+
]
228+
229+
async def run_url_dispatcher_benchmark() -> None:
230+
for request in requests:
231+
await router.resolve(request)
232+
233+
@benchmark
234+
def _run() -> None:
235+
loop.run_until_complete(run_url_dispatcher_benchmark())
236+
237+
238+
def test_resolve_dynamic_resource_url_with_many_dynamic_routes_with_common_prefix(
239+
loop: asyncio.AbstractEventLoop,
240+
benchmark: BenchmarkFixture,
241+
) -> None:
242+
"""Resolve different a DynamicResource when there are 250 DynamicResources registered with the same common prefix."""
243+
244+
async def handler(request: web.Request) -> NoReturn:
245+
assert False
246+
247+
app = web.Application()
248+
for count in range(250):
249+
app.router.add_route("GET", f"/api/{{customer}}/show_{count}", handler)
250+
app.router.add_route("GET", "/api/{customer}/update", handler)
251+
app.freeze()
252+
router = app.router
253+
254+
requests = [
255+
_mock_request(method="GET", path=f"/api/{customer}/update")
256+
for customer in range(250)
257+
]
258+
259+
async def run_url_dispatcher_benchmark() -> None:
260+
for request in requests:
261+
await router.resolve(request)
262+
263+
@benchmark
264+
def _run() -> None:
265+
loop.run_until_complete(run_url_dispatcher_benchmark())
266+
267+
206268
def test_resolve_gitapi(
207269
loop: asyncio.AbstractEventLoop,
208270
benchmark: BenchmarkFixture,

0 commit comments

Comments
 (0)