diff --git a/tests/test_benchmarks_web_fileresponse.py b/tests/test_benchmarks_web_fileresponse.py index 7cdbda2efbb..01aa7448c86 100644 --- a/tests/test_benchmarks_web_fileresponse.py +++ b/tests/test_benchmarks_web_fileresponse.py @@ -3,9 +3,10 @@ import asyncio import pathlib +from multidict import CIMultiDict from pytest_codspeed import BenchmarkFixture -from aiohttp import web +from aiohttp import ClientResponse, web from aiohttp.pytest_plugin import AiohttpClient @@ -24,7 +25,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 +33,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 +54,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 +62,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 web.FileResponse that return a 304.""" + 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[str]: + 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[str], + ) -> ClientResponse: + client = await aiohttp_client(app) + for _ in range(response_count): + resp = await client.get("/", headers=headers) + + await client.close() + return resp # type: ignore[possibly-undefined] + + 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