2019-02-28 14:56:41 +00:00
|
|
|
from random import choice, seed
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2019-02-28 14:56:41 +00:00
|
|
|
from pytest import mark
|
|
|
|
|
|
|
|
import sanic.router
|
|
|
|
|
2021-02-08 10:18:29 +00:00
|
|
|
from sanic.request import Request
|
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2019-02-28 14:56:41 +00:00
|
|
|
seed("Pack my box with five dozen liquor jugs.")
|
|
|
|
|
|
|
|
# Disable Caching for testing purpose
|
|
|
|
sanic.router.ROUTER_CACHE_SIZE = 0
|
|
|
|
|
|
|
|
|
|
|
|
class TestSanicRouteResolution:
|
|
|
|
@mark.asyncio
|
|
|
|
async def test_resolve_route_no_arg_string_path(
|
|
|
|
self, sanic_router, route_generator, benchmark
|
|
|
|
):
|
|
|
|
simple_routes = route_generator.generate_random_direct_route(
|
|
|
|
max_route_depth=4
|
|
|
|
)
|
|
|
|
router, simple_routes = sanic_router(route_details=simple_routes)
|
|
|
|
route_to_call = choice(simple_routes)
|
2021-03-21 07:47:21 +00:00
|
|
|
request = Request(
|
2023-10-25 02:28:52 +01:00
|
|
|
f"/{route_to_call[-1]}".encode(),
|
2021-03-21 07:47:21 +00:00
|
|
|
{"host": "localhost"},
|
|
|
|
"v1",
|
|
|
|
route_to_call[0],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
2019-02-28 14:56:41 +00:00
|
|
|
|
|
|
|
result = benchmark.pedantic(
|
2021-02-08 10:18:29 +00:00
|
|
|
router.get,
|
|
|
|
(
|
2021-03-21 07:47:21 +00:00
|
|
|
request.path,
|
|
|
|
request.method,
|
|
|
|
request.headers.get("host"),
|
2021-02-08 10:18:29 +00:00
|
|
|
),
|
2019-02-28 14:56:41 +00:00
|
|
|
iterations=1000,
|
|
|
|
rounds=1000,
|
|
|
|
)
|
2021-03-01 13:30:52 +00:00
|
|
|
assert await result[1](None) == 1
|
2019-02-28 14:56:41 +00:00
|
|
|
|
|
|
|
@mark.asyncio
|
|
|
|
async def test_resolve_route_with_typed_args(
|
|
|
|
self, sanic_router, route_generator, benchmark
|
|
|
|
):
|
|
|
|
typed_routes = route_generator.add_typed_parameters(
|
|
|
|
route_generator.generate_random_direct_route(max_route_depth=4),
|
|
|
|
max_route_depth=8,
|
|
|
|
)
|
|
|
|
router, typed_routes = sanic_router(route_details=typed_routes)
|
|
|
|
route_to_call = choice(typed_routes)
|
|
|
|
url = route_generator.generate_url_for_template(
|
|
|
|
template=route_to_call[-1]
|
|
|
|
)
|
|
|
|
|
2023-10-25 02:28:52 +01:00
|
|
|
print(f"{route_to_call[-1]} -> {url}")
|
2021-03-21 07:47:21 +00:00
|
|
|
request = Request(
|
2023-10-25 02:28:52 +01:00
|
|
|
f"/{url}".encode(),
|
2021-03-21 07:47:21 +00:00
|
|
|
{"host": "localhost"},
|
|
|
|
"v1",
|
|
|
|
route_to_call[0],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
2019-02-28 14:56:41 +00:00
|
|
|
|
|
|
|
result = benchmark.pedantic(
|
2021-02-08 10:18:29 +00:00
|
|
|
router.get,
|
|
|
|
(
|
2021-03-21 07:47:21 +00:00
|
|
|
request.path,
|
|
|
|
request.method,
|
|
|
|
request.headers.get("host"),
|
2021-02-08 10:18:29 +00:00
|
|
|
),
|
2019-02-28 14:56:41 +00:00
|
|
|
iterations=1000,
|
|
|
|
rounds=1000,
|
|
|
|
)
|
2021-03-01 13:30:52 +00:00
|
|
|
assert await result[1](None) == 1
|