3
3
import asyncio
4
4
import pathlib
5
5
6
+ from multidict import CIMultiDict
6
7
from pytest_codspeed import BenchmarkFixture
7
8
8
- from aiohttp import web
9
+ from aiohttp import ClientResponse , web
9
10
from aiohttp .pytest_plugin import AiohttpClient
10
11
11
12
@@ -24,15 +25,15 @@ async def handler(request: web.Request) -> web.FileResponse:
24
25
app = web .Application ()
25
26
app .router .add_route ("GET" , "/" , handler )
26
27
27
- async def run_file_resonse_benchmark () -> None :
28
+ async def run_file_response_benchmark () -> None :
28
29
client = await aiohttp_client (app )
29
30
for _ in range (response_count ):
30
31
await client .get ("/" )
31
32
await client .close ()
32
33
33
34
@benchmark
34
35
def _run () -> None :
35
- loop .run_until_complete (run_file_resonse_benchmark ())
36
+ loop .run_until_complete (run_file_response_benchmark ())
36
37
37
38
38
39
def test_simple_web_file_sendfile_fallback_response (
@@ -53,12 +54,52 @@ async def handler(request: web.Request) -> web.FileResponse:
53
54
app = web .Application ()
54
55
app .router .add_route ("GET" , "/" , handler )
55
56
56
- async def run_file_resonse_benchmark () -> None :
57
+ async def run_file_response_benchmark () -> None :
57
58
client = await aiohttp_client (app )
58
59
for _ in range (response_count ):
59
60
await client .get ("/" )
60
61
await client .close ()
61
62
62
63
@benchmark
63
64
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