1
1
import asyncio
2
2
import time
3
3
from concurrent .futures import ThreadPoolExecutor , as_completed
4
- from io import BytesIO
5
4
from importlib .metadata import version
5
+ from io import BytesIO
6
6
7
+ import aiohttp
8
+ import curl_cffi .requests
9
+ import httpx
7
10
import pandas as pd
11
+ import pycurl
8
12
import requests
9
- import httpx
10
13
import tls_client
11
- import pycurl
14
+
12
15
import primp
13
- import curl_cffi .requests
14
16
15
17
16
18
class PycurlSession :
@@ -48,6 +50,7 @@ def text(self):
48
50
("primp" , primp .Client ),
49
51
]
50
52
AsyncPACKAGES = [
53
+ ("aiohttp" , aiohttp .ClientSession ),
51
54
("httpx" , httpx .AsyncClient ),
52
55
("curl_cffi" , curl_cffi .requests .AsyncSession ),
53
56
("primp" , primp .AsyncClient ),
@@ -80,8 +83,13 @@ def session_get_test(session_class, requests_number):
80
83
81
84
async def async_session_get_test (session_class , requests_number ):
82
85
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
85
93
86
94
async with session_class () as s :
87
95
tasks = [aget (s , url ) for _ in range (requests_number )]
@@ -150,10 +158,7 @@ async def aget(s, url):
150
158
)
151
159
pivot_df .reset_index (inplace = True )
152
160
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" ]]]
157
162
print (pivot_df )
158
163
159
164
for session in [False , True , "Async" ]:
@@ -169,9 +174,7 @@ async def aget(s, url):
169
174
for threads_number in threads_numbers :
170
175
for response_size in ["5k" , "50k" , "200k" ]:
171
176
url = f"http://127.0.0.1:8000/{ response_size } "
172
- print (
173
- f"\n Threads={ threads_number } , session=True, { response_size = } , { requests_number = } "
174
- )
177
+ print (f"\n Threads={ threads_number } , session=True, { response_size = } , { requests_number = } " )
175
178
for name , session_class in PACKAGES :
176
179
start = time .perf_counter ()
177
180
cpu_start = time .process_time ()
@@ -209,10 +212,7 @@ async def aget(s, url):
209
212
)
210
213
pivot_df .reset_index (inplace = True )
211
214
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" ]]]
216
216
unique_threads = pivot_df ["threads" ].unique ()
217
217
for thread in unique_threads :
218
218
thread_df = pivot_df [pivot_df ["threads" ] == thread ]
0 commit comments