Benchmarks
Relevant source files
The following files were used as context for generating this wiki page:
Benchmarks in Uvicorn are designed to measure the performance and efficiency of the server's protocol implementations under various network conditions and request types. These benchmarks primarily focus on the core HTTP/1.1 and WebSocket protocol handling, utilizing a specialized testing infrastructure that bypasses the actual network stack to focus on processing overhead.
The benchmarking suite is integrated into the project's testing framework via pytest-benchmark and pytest-codspeed. It allows developers to track performance regressions across different protocol implementations like h11 and httptools.
Sources: pyproject.toml:67, tests/benchmarks/test_http.py:27
Benchmarking Architecture
The benchmark system uses a "Mock Protocol" approach. Instead of opening real TCP sockets, the tests instantiate protocol classes directly and feed them raw byte strings. This isolates the performance of the parsing and state management logic from OS-level networking overhead.
Protocol Flow for Benchmarks
The following diagram illustrates how data flows through a benchmark test case:
The diagram shows the lifecycle of a benchmark execution, from setup to verification.
Sources: tests/benchmarks/test_http.py:38-100, tests/protocols/test_http.py:204-215
Key Components
| Component | Description |
|---|---|
MockLoop | A simulated event loop that allows manual execution of tasks via run_one(). |
MockTransport | A simulated network transport that captures outgoing data in a buffer. |
get_connected_protocol | A utility function to wire together the mock loop, transport, and the protocol under test. |
pytest.mark.benchmark | Marker used to identify tests that should be handled by the benchmarking engine. |
Sources: tests/benchmarks/test_http.py:27, tests/protocols/test_http.py:126-200
HTTP Benchmarks
The HTTP benchmarks evaluate how the server handles different types of requests and response patterns. These include simple GET/POST operations, pipelined requests, and fragmented body delivery.
Benchmarked Scenarios
| Test Case | Purpose | Source Payload |
|---|---|---|
test_bench_simple_get | Measures overhead of a basic GET request. | SIMPLE_GET_REQUEST |
test_bench_large_post | Evaluates performance with large (100KB+) payloads. | LARGE_POST_REQUEST |
test_bench_pipelined_requests | Tests handling of multiple requests sent in a single batch. | SIMPLE_GET_REQUEST * 3 |
test_bench_fragmented_body | Measures efficiency of reassembling bodies delivered in multiple chunks. | FRAGMENTED_BODY_CHUNKS |
test_bench_chunked_response | Tests performance of sending responses with transfer-encoding: chunked. | _chunked_config |
Sources: tests/benchmarks/test_http.py:44-93, tests/protocols/test_http.py:38-100
HTTP Request Lifecycle in Benchmarks
This sequence shows the interaction between the mock test environment, the protocol implementation, and the ASGI app.
Sources: tests/benchmarks/test_http.py:44-48, uvicorn/protocols/http/httptools_impl.py:246-300
WebSocket Benchmarks
WebSocket benchmarks focus on the handshake process and the efficiency of sending data frames. The benchmarks test multiple implementations including wsproto and websockets-sansio.
Handshake and Data Transfer
The benchmarks measure the time taken to complete the HTTP Upgrade to WebSocket and the overhead of sending text messages.
- Handshake: Uses the
WS_UPGRADEpayload to transition the protocol. - Text Transfer: Uses an application that sends "Hello, world!" immediately upon acceptance.
Sources: tests/benchmarks/test_ws.py:53-64, tests/benchmarks/ws.py:8-12
WebSocket Benchmark Configurations
| App Type | Behavior | Config Reference |
|---|---|---|
_ws_accept_close_app | Accepts connection and immediately closes. | _ws_accept_close_config |
_ws_send_text_app | Accepts, sends a text frame, then closes. | _ws_send_text_config |
Sources: tests/benchmarks/test_ws.py:37-49
Environment and Dependencies
The benchmarking suite depends on specific optional extras and development tools to run effectively across all supported protocols.
Required Packages
- httptools: Used for high-performance C-based HTTP parsing benchmarks.
- uvloop: Used for event loop performance comparisons (on non-Windows platforms).
- wsproto / websockets: Required for WebSocket implementation benchmarks.
- pytest-benchmark: The core engine for executing and recording metrics.
Sources: pyproject.toml:35-50, pyproject.toml:67
Implementation Matrix
Benchmarks are typically parameterized to run against different implementation classes:
@pytest.fixture(params=["wsproto", "websockets-sansio"])
def ws_cls(request):
# Logic to return implementation class
...
Sources: tests/benchmarks/test_ws.py:20-34
Summary
Uvicorn's benchmarks provide a rigorous way to measure the performance of its ASGI server implementations. By using mock transports and loops, the suite provides a high-fidelity environment for testing protocol logic in isolation. This ensures that changes to the parser (such as httptools or h11) or the internal state machine do not introduce performance regressions for common web traffic patterns.