-
-
Notifications
You must be signed in to change notification settings - Fork 200
Add FastAPI HTTP request benchmark #440
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e2a459e
Add FastAPI HTTP request benchmark
savannahostrowski 3cef6be
Add new line
savannahostrowski 1a172af
Use tabs in MANIFEST
savannahostrowski 6f5bcfe
Remove extraneous pinned deps
savannahostrowski 556e08f
Syntax cleanup
savannahostrowski 3d56992
Restructure to remove server config from timing
savannahostrowski 28148ed
Use polling
savannahostrowski 5b14cad
Cleanup
savannahostrowski fa4e05f
Move setup
savannahostrowski 38b1567
Remove setup to function
savannahostrowski 1cdece8
Merge branch 'main' into fastapi
savannahostrowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
pyperformance/data-files/benchmarks/bm_fastapi/pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [project] | ||
| name = "pyperformance_bm_fastapi" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "pyperf", | ||
| "fastapi", | ||
| "uvicorn", | ||
| "httpx", | ||
| ] | ||
| urls = {repository = "https://github.com/python/pyperformance"} | ||
| dynamic = ["version"] | ||
|
|
||
| [tool.pyperformance] | ||
| name = "fastapi" | ||
| tags = "apps" |
3 changes: 3 additions & 0 deletions
3
pyperformance/data-files/benchmarks/bm_fastapi/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| fastapi==0.121.0 | ||
| httpx==0.28.1 | ||
| uvicorn==0.38.0 |
89 changes: 89 additions & 0 deletions
89
pyperformance/data-files/benchmarks/bm_fastapi/run_benchmark.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """ | ||
| Test the performance of simple HTTP serving with FastAPI. | ||
|
|
||
| This benchmark tests FastAPI's request handling, including: | ||
| - Path parameter extraction and validation | ||
| - Pydantic model serialization | ||
| - JSON response encoding | ||
|
|
||
| The bench serves a REST API endpoint that returns JSON objects, | ||
| simulating a typical web application scenario. | ||
|
|
||
| Author: Savannah Ostrowski | ||
| """ | ||
|
|
||
| import asyncio | ||
| import socket | ||
|
|
||
| import httpx | ||
| import pyperf | ||
| import uvicorn | ||
| from fastapi import FastAPI | ||
| from pydantic import BaseModel | ||
|
|
||
| HOST = "127.0.0.1" | ||
|
|
||
| CONCURRENCY = 150 | ||
|
|
||
| class Item(BaseModel): | ||
| id: int | ||
| name: str | ||
| price: float | ||
| tags: list[str] = [] | ||
|
|
||
| app = FastAPI() | ||
|
|
||
| @app.get("/items/{item_id}", response_model=Item) | ||
| async def get_item(item_id: int): | ||
| return { | ||
| "id": item_id, | ||
| "name": "Sample Item", | ||
| "price": 9.99, | ||
| "tags": ["sample", "item", "fastapi"] | ||
| } | ||
|
|
||
|
|
||
| def bench_fastapi(loops, url): | ||
| async def run_benchmark(): | ||
| async with httpx.AsyncClient() as client: | ||
| t0 = pyperf.perf_counter() | ||
|
|
||
| for i in range(loops): | ||
| tasks = [ | ||
| client.get(f"{url}/items/{i}") | ||
| for _ in range(CONCURRENCY) | ||
| ] | ||
| responses = await asyncio.gather(*tasks) | ||
| for response in responses: | ||
| response.raise_for_status() | ||
| data = response.json() | ||
| assert data["id"] == i | ||
| assert "tags" in data | ||
|
|
||
| return pyperf.perf_counter() - t0 | ||
|
|
||
| return asyncio.run(run_benchmark()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import threading | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| s.bind((HOST, 0)) | ||
| s.listen(1) | ||
| port = s.getsockname()[1] | ||
|
|
||
| config = uvicorn.Config(app, host=HOST, port=port, log_level="error") | ||
| server = uvicorn.Server(config) | ||
|
|
||
| server_thread = threading.Thread(target=server.run, daemon=True) | ||
| server_thread.start() | ||
|
|
||
| while not server.started: | ||
| pass | ||
|
|
||
| url = f"http://{HOST}:{port}" | ||
|
|
||
| runner = pyperf.Runner() | ||
| runner.metadata['description'] = "Test the performance of HTTP requests with FastAPI" | ||
| runner.bench_time_func("fastapi_http", bench_fastapi, url) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.