Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 304 benchmark for FileResponse #10114

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions tests/test_benchmarks_web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import asyncio
import pathlib

from multidict import CIMultiDict
from pytest_codspeed import BenchmarkFixture

import aiohttp
Fixed Show fixed Hide fixed
from aiohttp import web
from aiohttp.pytest_plugin import AiohttpClient

Expand All @@ -24,15 +26,15 @@
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("/")
await client.close()

@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(
Expand All @@ -53,12 +55,52 @@
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("/")
await client.close()

@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
Loading