Skip to content

Instantly share code, notes, and snippets.

@imbolc
Created March 1, 2020 10:47
Show Gist options
  • Save imbolc/15cab07811c32e7d50cc12f380f7f62f to your computer and use it in GitHub Desktop.
Save imbolc/15cab07811c32e7d50cc12f380f7f62f to your computer and use it in GitHub Desktop.
Httpx vs aiohttp benchmark
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.responses import PlainTextResponse
import httpx
import aiohttp
HOST, PORT = "localhost", 8000
URL = f"http://{HOST}:{PORT}/"
async def index(request):
return PlainTextResponse("world")
async def aiohttp_single(request):
async with aiohttp.ClientSession() as client:
async with client.get(URL) as r:
return _response(await r.text())
async def aiohttp_session(request):
async with aiohttp_session.get(URL) as r:
return _response(await r.text())
async def httpx_single(request):
async with httpx.AsyncClient() as client:
r = await client.get(URL)
return _response(r.text)
async def httpx_session(request):
r = await httpx_session.get(URL)
return _response(r.text)
async def httpx_single_http2(request):
async with httpx.AsyncClient(http2=True) as client:
r = await client.get(URL)
return _response(r.text)
async def httpx_session_http2(request):
r = await httpx_session_http2.get(URL)
return _response(r.text)
def _response(name):
return PlainTextResponse("Hello, " + name)
routes = [
Route("/", endpoint=index),
Route("/aiohttp/single", endpoint=aiohttp_single),
Route("/aiohttp/session", endpoint=aiohttp_session),
Route("/httpx/single", endpoint=httpx_single),
Route("/httpx/session", endpoint=httpx_session),
Route("/httpx/single/http2", endpoint=httpx_single_http2),
Route("/httpx/session/http2", endpoint=httpx_session_http2),
]
async def on_startup():
global aiohttp_session, httpx_session, httpx_session_http2
aiohttp_session = aiohttp.ClientSession()
httpx_session = httpx.AsyncClient()
httpx_session_http2 = httpx.AsyncClient(http2=True)
app = Starlette(debug=True, routes=routes, on_startup=[on_startup])
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host=HOST, port=PORT)
imbolc@p1:~$ wrk http://localhost:8000/aiohttp/single
Running 10s test @ http://localhost:8000/aiohttp/single
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 10.31ms 7.72ms 99.45ms 95.58%
Req/Sec 533.61 84.57 610.00 84.50%
10628 requests in 10.01s, 1.48MB read
Requests/sec: 1062.20
Transfer/sec: 151.45KB
imbolc@p1:~$ wrk http://localhost:8000/httpx/single
Running 10s test @ http://localhost:8000/httpx/single
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 70.46ms 17.56ms 151.83ms 82.35%
Req/Sec 70.98 21.31 101.00 78.00%
1418 requests in 10.01s, 202.18KB read
Requests/sec: 141.65
Transfer/sec: 20.20KB
imbolc@p1:~$ wrk http://localhost:8000/httpx/single/http2
Running 10s test @ http://localhost:8000/httpx/single/http2
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 70.13ms 17.16ms 141.46ms 81.04%
Req/Sec 71.26 20.15 101.00 48.99%
1420 requests in 10.01s, 202.46KB read
Requests/sec: 141.87
Transfer/sec: 20.23KB
imbolc@p1:~$ wrk http://localhost:8000/aiohttp/session
Running 10s test @ http://localhost:8000/aiohttp/session
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 5.43ms 1.51ms 26.04ms 94.24%
Req/Sec 0.94k 83.98 1.08k 75.00%
18634 requests in 10.01s, 2.59MB read
Requests/sec: 1862.19
Transfer/sec: 265.51KB
imbolc@p1:~$ wrk http://localhost:8000/httpx/session
Running 10s test @ http://localhost:8000/httpx/session
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 8.61ms 1.84ms 30.88ms 95.70%
Req/Sec 587.36 53.99 650.00 88.50%
11699 requests in 10.01s, 1.63MB read
Requests/sec: 1168.60
Transfer/sec: 166.62KB
imbolc@p1:~$ wrk http://localhost:8000/httpx/session/http2
Running 10s test @ http://localhost:8000/httpx/session/http2
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 8.39ms 1.60ms 39.17ms 96.12%
Req/Sec 601.91 42.89 656.00 81.50%
11981 requests in 10.00s, 1.67MB read
Requests/sec: 1197.51
Transfer/sec: 170.74KB
@baby5
Copy link

baby5 commented Jan 19, 2024

httpx.AsyncClient(trust_env=False)

Using this to make httpx and aiohttp do same thing about system proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment