forked from lexiforest/curl_cffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
163 lines (134 loc) · 4.22 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import asyncio
import queue
import threading
import time
from io import BytesIO
import aiohttp
import httpx
import pandas as pd
import pycurl
import requests
import tls_client
import curl_cffi
import curl_cffi.requests
# import uvloop
# uvloop.install()
results = []
class FakePycurlSession:
def __init__(self):
self.c = pycurl.Curl()
def get(self, url):
buffer = BytesIO()
self.c.setopt(pycurl.URL, url)
self.c.setopt(pycurl.WRITEDATA, buffer)
self.c.perform()
def __del__(self):
self.c.close()
class FakeCurlCffiSession:
def __init__(self):
self.c = curl_cffi.Curl()
def get(self, url):
buffer = BytesIO()
self.c.setopt(curl_cffi.CurlOpt.URL, url)
self.c.setopt(curl_cffi.CurlOpt.WRITEDATA, buffer)
self.c.perform()
def __del__(self):
self.c.close()
for size in ["1k", "20k", "200k"]:
stats = {}
url = "http://localhost:8000/" + size
for name, SessionClass in [
("requests", requests.Session),
("httpx_sync", httpx.Client),
("tls_client", tls_client.Session),
("curl_cffi_sync", curl_cffi.requests.Session),
("curl_cffi_raw", FakeCurlCffiSession),
("pycurl", FakePycurlSession),
]:
s = SessionClass()
start = time.time()
for _ in range(1000):
s.get(url)
dur = time.time() - start
stats[name] = dur
results.append({"name": name, "size": size, "duration": dur})
print(f"One worker, {size}: {stats}")
df = pd.DataFrame(results)
df.to_csv("single_worker.csv", index=False, float_format="%.4f")
results = []
def worker(q, done, SessionClass):
s = SessionClass()
while not done.is_set():
try:
url = q.get_nowait()
except Exception:
continue
s.get(url)
q.task_done()
async def aiohttp_worker(q, done, s):
while not done.is_set():
url = await q.get()
async with s.get(url) as response:
await response.read()
q.task_done()
async def httpx_worker(q, done, s):
while not done.is_set():
url = await q.get()
await s.get(url)
q.task_done()
for size in ["1k", "20k", "200k"]:
url = "http://localhost:8000/" + size
stats = {}
for name, SessionClass in [
("requests", requests.Session),
("httpx_sync", httpx.Client),
("tls_client", tls_client.Session),
("curl_cffi_sync", curl_cffi.requests.Session),
("curl_cffi_raw", FakeCurlCffiSession),
("pycurl", FakePycurlSession),
]:
q = queue.Queue()
for _ in range(1000):
q.put(url)
done = threading.Event()
start = time.time()
threads = []
for _ in range(10):
t = threading.Thread(target=worker, args=(q, done, SessionClass))
threads.append(t)
t.start()
q.join()
done.set()
dur = time.time() - start
stats[name] = dur
results.append({"name": name, "size": size, "duration": dur})
for t in threads:
t.join()
# print(stats)
async def test_asyncs_workers(url, size, stats):
for name, worker, SessionClass in [
("aiohttp", aiohttp_worker, aiohttp.ClientSession),
("httpx_async", httpx_worker, httpx.AsyncClient),
("curl_cffi_async", httpx_worker, curl_cffi.requests.AsyncSession),
]:
q = asyncio.Queue()
for _ in range(1000):
await q.put(url)
done = asyncio.Event()
start = time.time()
workers = []
async with SessionClass() as s:
for _ in range(10):
w = asyncio.create_task(worker(q, done, s))
workers.append(w)
await q.join()
done.set()
dur = time.time() - start
stats[name] = dur
results.append({"name": name, "size": size, "duration": dur})
for w in workers:
w.cancel()
asyncio.run(test_asyncs_workers(url, size, stats))
print(f"10 Workers, {size}: {stats}")
df = pd.DataFrame(results)
df.to_csv("multiple_workers.csv", index=False, float_format="%.4f")