Skip to content

Commit 78473b9

Browse files
[PR #10114/94569554 backport][3.11] Add 304 benchmark for FileResponse (#10115)
Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent ae153ab commit 78473b9

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

tests/test_benchmarks_web_fileresponse.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import asyncio
44
import pathlib
55

6+
from multidict import CIMultiDict
67
from pytest_codspeed import BenchmarkFixture
78

8-
from aiohttp import web
9+
from aiohttp import ClientResponse, web
910
from aiohttp.pytest_plugin import AiohttpClient
1011

1112

@@ -24,15 +25,15 @@ async def handler(request: web.Request) -> web.FileResponse:
2425
app = web.Application()
2526
app.router.add_route("GET", "/", handler)
2627

27-
async def run_file_resonse_benchmark() -> None:
28+
async def run_file_response_benchmark() -> None:
2829
client = await aiohttp_client(app)
2930
for _ in range(response_count):
3031
await client.get("/")
3132
await client.close()
3233

3334
@benchmark
3435
def _run() -> None:
35-
loop.run_until_complete(run_file_resonse_benchmark())
36+
loop.run_until_complete(run_file_response_benchmark())
3637

3738

3839
def test_simple_web_file_sendfile_fallback_response(
@@ -53,12 +54,52 @@ async def handler(request: web.Request) -> web.FileResponse:
5354
app = web.Application()
5455
app.router.add_route("GET", "/", handler)
5556

56-
async def run_file_resonse_benchmark() -> None:
57+
async def run_file_response_benchmark() -> None:
5758
client = await aiohttp_client(app)
5859
for _ in range(response_count):
5960
await client.get("/")
6061
await client.close()
6162

6263
@benchmark
6364
def _run() -> None:
64-
loop.run_until_complete(run_file_resonse_benchmark())
65+
loop.run_until_complete(run_file_response_benchmark())
66+
67+
68+
def test_simple_web_file_response_not_modified(
69+
loop: asyncio.AbstractEventLoop,
70+
aiohttp_client: AiohttpClient,
71+
benchmark: BenchmarkFixture,
72+
) -> None:
73+
"""Benchmark web.FileResponse that return a 304."""
74+
response_count = 100
75+
filepath = pathlib.Path(__file__).parent / "sample.txt"
76+
77+
async def handler(request: web.Request) -> web.FileResponse:
78+
return web.FileResponse(path=filepath)
79+
80+
app = web.Application()
81+
app.router.add_route("GET", "/", handler)
82+
83+
async def make_last_modified_header() -> CIMultiDict[str]:
84+
client = await aiohttp_client(app)
85+
resp = await client.get("/")
86+
last_modified = resp.headers["Last-Modified"]
87+
headers = CIMultiDict({"If-Modified-Since": last_modified})
88+
return headers
89+
90+
async def run_file_response_benchmark(
91+
headers: CIMultiDict[str],
92+
) -> ClientResponse:
93+
client = await aiohttp_client(app)
94+
for _ in range(response_count):
95+
resp = await client.get("/", headers=headers)
96+
97+
await client.close()
98+
return resp # type: ignore[possibly-undefined]
99+
100+
headers = loop.run_until_complete(make_last_modified_header())
101+
102+
@benchmark
103+
def _run() -> None:
104+
resp = loop.run_until_complete(run_file_response_benchmark(headers))
105+
assert resp.status == 304

0 commit comments

Comments
 (0)