From bf3db1df84c50ebe1ded9fd98adb3096a6612e2f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Dec 2024 09:20:35 -0600 Subject: [PATCH 1/2] Add 304 benchmark for FileResponse related issue #10112 --- tests/test_benchmarks_web_fileresponse.py | 50 +++++++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/tests/test_benchmarks_web_fileresponse.py b/tests/test_benchmarks_web_fileresponse.py index 7cdbda2efbb..66ec0bcbaff 100644 --- a/tests/test_benchmarks_web_fileresponse.py +++ b/tests/test_benchmarks_web_fileresponse.py @@ -3,8 +3,10 @@ import asyncio import pathlib +from multidict import CIMultiDict from pytest_codspeed import BenchmarkFixture +import aiohttp from aiohttp import web from aiohttp.pytest_plugin import AiohttpClient @@ -24,7 +26,7 @@ async def handler(request: web.Request) -> web.FileResponse: app = web.Application() app.router.add_route("GET", "/", handler) - async def run_file_resonse_benchmark() -> None: + async def run_file_response_benchmark() -> None: client = await aiohttp_client(app) for _ in range(response_count): await client.get("/") @@ -32,7 +34,7 @@ async def run_file_resonse_benchmark() -> None: @benchmark def _run() -> None: - loop.run_until_complete(run_file_resonse_benchmark()) + loop.run_until_complete(run_file_response_benchmark()) def test_simple_web_file_sendfile_fallback_response( @@ -53,7 +55,7 @@ async def handler(request: web.Request) -> web.FileResponse: app = web.Application() app.router.add_route("GET", "/", handler) - async def run_file_resonse_benchmark() -> None: + async def run_file_response_benchmark() -> None: client = await aiohttp_client(app) for _ in range(response_count): await client.get("/") @@ -61,4 +63,44 @@ async def run_file_resonse_benchmark() -> None: @benchmark def _run() -> None: - loop.run_until_complete(run_file_resonse_benchmark()) + loop.run_until_complete(run_file_response_benchmark()) + + +def test_simple_web_file_response_not_modified( + loop: asyncio.AbstractEventLoop, + aiohttp_client: AiohttpClient, + benchmark: BenchmarkFixture, +) -> None: + """Benchmark creating 100 simple web.FileResponse.""" + response_count = 100 + filepath = pathlib.Path(__file__).parent / "sample.txt" + + async def handler(request: web.Request) -> web.FileResponse: + return web.FileResponse(path=filepath) + + app = web.Application() + app.router.add_route("GET", "/", handler) + + async def make_last_modified_header() -> CIMultiDict: + client = await aiohttp_client(app) + resp = await client.get("/") + last_modified = resp.headers["Last-Modified"] + headers = CIMultiDict({"If-Modified-Since": last_modified}) + return headers + + async def run_file_response_benchmark( + headers: CIMultiDict, + ) -> aiohttp.ClientResponse: + client = await aiohttp_client(app) + for _ in range(response_count): + resp = await client.get("/", headers=headers) + + await client.close() + return resp + + headers = loop.run_until_complete(make_last_modified_header()) + + @benchmark + def _run() -> None: + resp = loop.run_until_complete(run_file_response_benchmark(headers)) + assert resp.status == 304 From 979453d1d68ffed39536daf6e15d76fa9439eb2a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Dec 2024 09:24:25 -0600 Subject: [PATCH 2/2] nits --- tests/test_benchmarks_web_fileresponse.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/test_benchmarks_web_fileresponse.py b/tests/test_benchmarks_web_fileresponse.py index 66ec0bcbaff..01aa7448c86 100644 --- a/tests/test_benchmarks_web_fileresponse.py +++ b/tests/test_benchmarks_web_fileresponse.py @@ -6,8 +6,7 @@ from multidict import CIMultiDict from pytest_codspeed import BenchmarkFixture -import aiohttp -from aiohttp import web +from aiohttp import ClientResponse, web from aiohttp.pytest_plugin import AiohttpClient @@ -71,7 +70,7 @@ def test_simple_web_file_response_not_modified( aiohttp_client: AiohttpClient, benchmark: BenchmarkFixture, ) -> None: - """Benchmark creating 100 simple web.FileResponse.""" + """Benchmark web.FileResponse that return a 304.""" response_count = 100 filepath = pathlib.Path(__file__).parent / "sample.txt" @@ -81,7 +80,7 @@ async def handler(request: web.Request) -> web.FileResponse: app = web.Application() app.router.add_route("GET", "/", handler) - async def make_last_modified_header() -> CIMultiDict: + async def make_last_modified_header() -> CIMultiDict[str]: client = await aiohttp_client(app) resp = await client.get("/") last_modified = resp.headers["Last-Modified"] @@ -89,14 +88,14 @@ async def make_last_modified_header() -> CIMultiDict: return headers async def run_file_response_benchmark( - headers: CIMultiDict, - ) -> aiohttp.ClientResponse: + headers: CIMultiDict[str], + ) -> ClientResponse: client = await aiohttp_client(app) for _ in range(response_count): resp = await client.get("/", headers=headers) await client.close() - return resp + return resp # type: ignore[possibly-undefined] headers = loop.run_until_complete(make_last_modified_header())