Add fragmented body benchmark for chunked body accumulation

Sends 100KB in 390 x 256-byte chunks to exercise the body += path
that triggers O(n^2) allocation with bytes concatenation.
This commit is contained in:
Marcelo Trylesinski 2026-03-15 16:53:46 +01:00
parent 9b22e53e28
commit 8b383969ef
2 changed files with 24 additions and 0 deletions

View File

@ -61,6 +61,20 @@ START_POST_REQUEST = b"\r\n".join(
FINISH_POST_REQUEST = b'{"hello": "world"}'
BODY_CHUNK_SIZE = 256
FRAGMENTED_BODY_SIZE = 100_000
FRAGMENTED_POST_HEADERS = b"\r\n".join(
[
b"POST / HTTP/1.1",
b"Host: example.org",
b"Content-Type: application/octet-stream",
b"Content-Length: " + str(FRAGMENTED_BODY_SIZE).encode(),
b"",
b"",
]
)
FRAGMENTED_BODY_CHUNKS = [b"x" * BODY_CHUNK_SIZE] * (FRAGMENTED_BODY_SIZE // BODY_CHUNK_SIZE)
class MockTransport:
def __init__(self) -> None:

View File

@ -7,6 +7,8 @@ import pytest
from tests.benchmarks.http import (
CONNECTION_CLOSE_REQUEST,
FINISH_POST_REQUEST,
FRAGMENTED_BODY_CHUNKS,
FRAGMENTED_POST_HEADERS,
HTTP10_GET_REQUEST,
LARGE_POST_REQUEST,
SIMPLE_GET_REQUEST,
@ -91,6 +93,14 @@ async def test_bench_connection_close(http_protocol_cls: type[HTTPProtocol]) ->
await protocol.loop.run_one()
async def test_bench_fragmented_body(http_protocol_cls: type[HTTPProtocol]) -> None:
protocol = get_connected_protocol(_plain_text_app, http_protocol_cls)
protocol.data_received(FRAGMENTED_POST_HEADERS)
for chunk in FRAGMENTED_BODY_CHUNKS:
protocol.data_received(chunk)
await protocol.loop.run_one()
async def test_bench_post_body_receive(http_protocol_cls: type[HTTPProtocol]) -> None:
protocol = get_connected_protocol(_body_echo_app, http_protocol_cls)
protocol.data_received(START_POST_REQUEST)