-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add benchmarks for the URL dispatcher #9910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
"""codspeed benchmarks for the URL dispatcher.""" | ||
|
||
import asyncio | ||
from typing import NoReturn | ||
from unittest import mock | ||
|
||
from multidict import CIMultiDict, CIMultiDictProxy | ||
from pytest_codspeed import BenchmarkFixture | ||
from yarl import URL | ||
|
||
from aiohttp import web | ||
from aiohttp.http import HttpVersion, RawRequestMessage | ||
|
||
|
||
def _mock_request(method: str, path: str) -> web.Request: | ||
message = RawRequestMessage( | ||
method, | ||
path, | ||
HttpVersion(1, 1), | ||
CIMultiDictProxy(CIMultiDict()), | ||
(), | ||
False, | ||
None, | ||
False, | ||
False, | ||
URL(path), | ||
) | ||
|
||
return web.Request( | ||
message, mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock() | ||
) | ||
|
||
|
||
def test_resolve_root_route( | ||
loop: asyncio.AbstractEventLoop, | ||
benchmark: BenchmarkFixture, | ||
) -> None: | ||
"""Resolve top level PlainResources route 100 times.""" | ||
resolve_count = 100 | ||
|
||
async def handler(request: web.Request) -> NoReturn: | ||
assert False | ||
|
||
app = web.Application() | ||
app.router.add_route("GET", "/", handler) | ||
request = _mock_request(method="GET", path="/") | ||
|
||
async def run_url_dispatcher_benchmark() -> None: | ||
for _ in range(resolve_count): | ||
await app._router.resolve(request) | ||
|
||
@benchmark | ||
def _run() -> None: | ||
loop.run_until_complete(run_url_dispatcher_benchmark()) | ||
|
||
|
||
def test_resolve_single_fixed_url_with_many_routes( | ||
loop: asyncio.AbstractEventLoop, | ||
benchmark: BenchmarkFixture, | ||
) -> None: | ||
"""Resolve PlainResources route 100 times.""" | ||
resolve_count = 100 | ||
|
||
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/dispatch/{count}/update", handler) | ||
request = _mock_request(method="GET", path="/api/server/dispatch/1/update") | ||
|
||
async def run_url_dispatcher_benchmark() -> None: | ||
for _ in range(resolve_count): | ||
await app._router.resolve(request) | ||
|
||
@benchmark | ||
def _run() -> None: | ||
loop.run_until_complete(run_url_dispatcher_benchmark()) | ||
|
||
|
||
def test_resolve_multiple_fixed_url_with_many_routes( | ||
loop: asyncio.AbstractEventLoop, | ||
benchmark: BenchmarkFixture, | ||
) -> None: | ||
"""Resolve 250 different PlainResources routes.""" | ||
|
||
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/dispatch/{count}/update", handler) | ||
|
||
requests = [ | ||
_mock_request(method="GET", path=f"/api/server/dispatch/{count}/update") | ||
for count in range(250) | ||
] | ||
|
||
async def run_url_dispatcher_benchmark() -> None: | ||
for request in requests: | ||
await app._router.resolve(request) | ||
|
||
@benchmark | ||
def _run() -> None: | ||
loop.run_until_complete(run_url_dispatcher_benchmark()) | ||
|
||
|
||
def test_resolve_dynamic_resource_url_with_many_routes( | ||
loop: asyncio.AbstractEventLoop, | ||
benchmark: BenchmarkFixture, | ||
) -> None: | ||
"""Resolve different a DynamicResource when there are 250 PlainResources 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/{count}/update", handler) | ||
app.router.add_route("GET", "/api/server/dispatch/{customer}/update", handler) | ||
|
||
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 app._router.resolve(request) | ||
|
||
@benchmark | ||
def _run() -> None: | ||
loop.run_until_complete(run_url_dispatcher_benchmark()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.