Skip to content

Commit ec49e00

Browse files
committed
benchmark(async): add aiohttp
1 parent d989dc4 commit ec49e00

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

benchmark/benchmark.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import asyncio
22
import time
33
from concurrent.futures import ThreadPoolExecutor, as_completed
4-
from io import BytesIO
54
from importlib.metadata import version
5+
from io import BytesIO
66

7+
import aiohttp
8+
import curl_cffi.requests
9+
import httpx
710
import pandas as pd
11+
import pycurl
812
import requests
9-
import httpx
1013
import tls_client
11-
import pycurl
14+
1215
import primp
13-
import curl_cffi.requests
1416

1517

1618
class PycurlSession:
@@ -48,6 +50,7 @@ def text(self):
4850
("primp", primp.Client),
4951
]
5052
AsyncPACKAGES = [
53+
("aiohttp", aiohttp.ClientSession),
5154
("httpx", httpx.AsyncClient),
5255
("curl_cffi", curl_cffi.requests.AsyncSession),
5356
("primp", primp.AsyncClient),
@@ -80,8 +83,13 @@ def session_get_test(session_class, requests_number):
8083

8184
async def async_session_get_test(session_class, requests_number):
8285
async def aget(s, url):
83-
resp = await s.get(url)
84-
return resp.text
86+
if session_class.__module__ == "aiohttp.client":
87+
async with s.get(url) as resp:
88+
text = await resp.text()
89+
return text
90+
else:
91+
resp = await s.get(url)
92+
return resp.text
8593

8694
async with session_class() as s:
8795
tasks = [aget(s, url) for _ in range(requests_number)]
@@ -150,10 +158,7 @@ async def aget(s, url):
150158
)
151159
pivot_df.reset_index(inplace=True)
152160
pivot_df.columns = [" ".join(col).strip() for col in pivot_df.columns.values]
153-
pivot_df = pivot_df[
154-
["name", "session"]
155-
+ [col for col in pivot_df.columns if col not in ["name", "session"]]
156-
]
161+
pivot_df = pivot_df[["name", "session"] + [col for col in pivot_df.columns if col not in ["name", "session"]]]
157162
print(pivot_df)
158163

159164
for session in [False, True, "Async"]:
@@ -169,9 +174,7 @@ async def aget(s, url):
169174
for threads_number in threads_numbers:
170175
for response_size in ["5k", "50k", "200k"]:
171176
url = f"http://127.0.0.1:8000/{response_size}"
172-
print(
173-
f"\nThreads={threads_number}, session=True, {response_size=}, {requests_number=}"
174-
)
177+
print(f"\nThreads={threads_number}, session=True, {response_size=}, {requests_number=}")
175178
for name, session_class in PACKAGES:
176179
start = time.perf_counter()
177180
cpu_start = time.process_time()
@@ -209,10 +212,7 @@ async def aget(s, url):
209212
)
210213
pivot_df.reset_index(inplace=True)
211214
pivot_df.columns = [" ".join(col).strip() for col in pivot_df.columns.values]
212-
pivot_df = pivot_df[
213-
["name", "threads"]
214-
+ [col for col in pivot_df.columns if col not in ["name", "threads"]]
215-
]
215+
pivot_df = pivot_df[["name", "threads"] + [col for col in pivot_df.columns if col not in ["name", "threads"]]]
216216
unique_threads = pivot_df["threads"].unique()
217217
for thread in unique_threads:
218218
thread_df = pivot_df[pivot_df["threads"] == thread]

benchmark/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ matplotlib
22
pandas
33
starlette
44
uvicorn
5+
aiohttp
56
requests
67
httpx
78
tls-client

0 commit comments

Comments
 (0)