1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
"""codspeed benchmarks for the web file responses."""
import asyncio
import pathlib
from multidict import CIMultiDict
from pytest_codspeed import BenchmarkFixture
from aiohttp import ClientResponse, web
from aiohttp.pytest_plugin import AiohttpClient
def test_simple_web_file_response(
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 run_file_response_benchmark() -> None:
client = await aiohttp_client(app)
for _ in range(response_count):
await client.get("/")
await client.close()
@benchmark
def _run() -> None:
loop.run_until_complete(run_file_response_benchmark())
def test_simple_web_file_sendfile_fallback_response(
loop: asyncio.AbstractEventLoop,
aiohttp_client: AiohttpClient,
benchmark: BenchmarkFixture,
) -> None:
"""Benchmark creating 100 simple web.FileResponse without sendfile."""
response_count = 100
filepath = pathlib.Path(__file__).parent / "sample.txt"
async def handler(request: web.Request) -> web.FileResponse:
transport = request.transport
assert transport is not None
transport._sendfile_compatible = False # type: ignore[attr-defined]
return web.FileResponse(path=filepath)
app = web.Application()
app.router.add_route("GET", "/", handler)
async def run_file_response_benchmark() -> None:
client = await aiohttp_client(app)
for _ in range(response_count):
await client.get("/")
await client.close()
@benchmark
def _run() -> None:
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
|