Created
January 27, 2023 20:20
-
-
Save leemurus/1817183a821dfa9fff514cf4199638b6 to your computer and use it in GitHub Desktop.
httpx vs aiohttp
This file contains 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
import aiohttp | |
import asyncio | |
import time | |
import httpx | |
def print_performance(func): | |
async def wrapper(*args, **kwargs): | |
begin = time.monotonic() | |
result = await func(*args, **kwargs) | |
duration = time.monotonic() - begin | |
print(f'Time execution of {func.__name__}: {duration * 1000:.2f} ms') | |
return result | |
return wrapper | |
@print_performance | |
async def execute_get_httpx_requests(url: str, number: int): | |
tasks = [] | |
async with httpx.AsyncClient() as client: | |
for _ in range(number): | |
tasks.append(client.get(url)) | |
await asyncio.gather(*tasks) | |
@print_performance | |
async def execute_get_aiohttp_requests(url: str, number: int): | |
tasks = [] | |
async with aiohttp.ClientSession() as session: | |
for _ in range(number): | |
tasks.append(session.get(url)) | |
await asyncio.gather(*tasks) | |
@print_performance | |
async def execute_post_httpx_requests(url: str, number: int): | |
tasks = [] | |
async with httpx.AsyncClient() as client: | |
for _ in range(number): | |
tasks.append(client.post(url)) | |
await asyncio.gather(*tasks) | |
@print_performance | |
async def execute_post_aiohttp_requests(url: str, number: int): | |
tasks = [] | |
async with aiohttp.ClientSession() as session: | |
for _ in range(number): | |
tasks.append(session.post(url)) | |
await asyncio.gather(*tasks) | |
async def main(): | |
get_method_url = 'http://localhost:8000/ping' | |
post_method_url = 'http://localhost:8000/pong' | |
exc_number = 100 | |
await execute_get_httpx_requests(get_method_url, exc_number) | |
await execute_post_httpx_requests(post_method_url, exc_number) | |
await execute_get_aiohttp_requests(get_method_url, exc_number) | |
await execute_post_aiohttp_requests(post_method_url, exc_number) | |
if __name__ == '__main__': | |
asyncio.run(main()) |
This file contains 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
from fastapi import FastAPI | |
import uvicorn | |
app = FastAPI() | |
@app.get('/ping') | |
def ping(): | |
return 'pong' | |
@app.post('/post') | |
def pong(): | |
return 'ping' | |
if __name__ == '__main__': | |
uvicorn.run( | |
app=app, | |
host='localhost', | |
port=8000, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Time execution of execute_get_httpx_requests: 698.56 ms
Time execution of execute_post_httpx_requests: 358.55 ms
Time execution of execute_get_aiohttp_requests: 135.74 ms
Time execution of execute_post_aiohttp_requests: 108.69 ms