-
Notifications
You must be signed in to change notification settings - Fork 4
/
py_test_client.py
181 lines (160 loc) · 4.72 KB
/
py_test_client.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import asyncio
import timeit
import aiohttp
import httpx
import requests
import torequests
result: dict = {}
def print_msg(name, cost, ok):
qps = round(TOTAL_REQUEST_COUNTS / cost)
msg = f'{name: <25}: {ok} / {TOTAL_REQUEST_COUNTS} = {ok * 100 / (TOTAL_REQUEST_COUNTS)}%, cost {round(cost, 3):0>5}s, {qps: >4} qps, {(round(qps*100/AIOHTTP_QPS) if AIOHTTP_QPS else 100): >3}% standard.'
print(msg)
async def test_aiohttp():
global AIOHTTP_QPS
from aiohttp import ClientSession
async def test(req, url):
async with req.get(url) as resp:
await resp.read()
return resp._body
async with ClientSession() as req:
ok = 0
start = timeit.default_timer()
tasks = [
asyncio.ensure_future(test(req, url))
for _ in range(TOTAL_REQUEST_COUNTS)
]
for task in tasks:
r = await task
if r == b'ok':
ok += 1
name = 'test_aiohttp'
cost = timeit.default_timer() - start
if cost < result.get(name, 999):
result[name] = cost
AIOHTTP_QPS = round(TOTAL_REQUEST_COUNTS / cost)
else:
cost = result[name]
print_msg(name, cost, ok)
async def test_dummy():
from torequests.dummy import Requests
async with Requests() as req:
start = timeit.default_timer()
ok = 0
tasks = [
asyncio.ensure_future(req.get(url))
for _ in range(TOTAL_REQUEST_COUNTS)
]
for task in tasks:
r = await task
if r.content == b'ok':
ok += 1
name = 'test_dummy'
cost = timeit.default_timer() - start
if cost < result.get(name, 999):
result[name] = cost
else:
cost = result[name]
result[name] = cost
print_msg(name, cost, ok)
async def test_aiohttp_dummy():
from torequests.aiohttp_dummy import Requests
async with Requests() as req:
start = timeit.default_timer()
ok = 0
tasks = [
asyncio.ensure_future(req.get(url))
for _ in range(TOTAL_REQUEST_COUNTS)
]
for task in tasks:
r = await task
if r.content == b'ok':
ok += 1
name = 'test_aiohttp_dummy'
cost = timeit.default_timer() - start
if cost < result.get(name, 999):
result[name] = cost
else:
cost = result[name]
result[name] = cost
print_msg(name, cost, ok)
def test_tPool():
from torequests.main import tPool
req = tPool()
start = timeit.default_timer()
ok = 0
tasks = [req.get(url) for _ in range(TOTAL_REQUEST_COUNTS)]
req.x
for task in tasks:
r = task.x
if r.text == 'ok':
ok += 1
name = 'test_tPool'
cost = timeit.default_timer() - start
if cost < result.get(name, 999):
result[name] = cost
else:
cost = result[name]
result[name] = cost
print_msg(name, cost, ok)
async def test_httpx():
from httpx import AsyncClient
start = timeit.default_timer()
ok = 0
async with AsyncClient() as req:
tasks = [
asyncio.create_task(req.get(url))
for _ in range(TOTAL_REQUEST_COUNTS)
]
for task in tasks:
r = await task
if r.text == 'ok':
ok += 1
name = 'test_httpx'
cost = timeit.default_timer() - start
if cost < result.get(name, 999):
result[name] = cost
else:
cost = result[name]
result[name] = cost
print_msg(name, cost, ok)
if __name__ == "__main__":
import platform
import sys
if sys.platform == 'win32': # use IOCP in windows
if sys.version_info.major >= 3 and sys.version_info.minor >= 7:
asyncio.set_event_loop_policy(
asyncio.WindowsProactorEventLoopPolicy())
else:
asyncio.set_event_loop(asyncio.ProactorEventLoop())
else: # try to use uvloop
try:
import uvloop
uvloop.install()
except ImportError:
pass
url = 'http://127.0.0.1:8080'
TOTAL_REQUEST_COUNTS = 2000
# use aiohttp as the standard qps
AIOHTTP_QPS = None
if len(sys.argv) > 1:
n = int(sys.argv[1])
else:
n = 10
for _ in range(n):
asyncio.run(test_aiohttp())
asyncio.run(test_dummy())
asyncio.run(test_aiohttp_dummy())
print('=' * 80)
# show final result
print(platform.platform())
print(sys.version)
print([
f'{mod.__name__}({mod.__version__})'
for mod in [aiohttp, torequests, requests, httpx]
])
print('=' * 80)
asyncio.run(test_aiohttp())
asyncio.run(test_dummy())
asyncio.run(test_aiohttp_dummy())
asyncio.run(test_httpx())
test_tPool()