Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
wreq-python/bench/async_clients.py at main · 0x676e67/wreq-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
0x676e67
wreq-python
Repository navigation
Code
Issues
6
(6)
Pull requests
4
(4)
Discussions
Actions
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
wreq-python
/
bench
/
async_clients.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
208 lines (178 loc) · 7.02 KB
main
Breadcrumbs
wreq-python
/
bench
/
async_clients.py
Copy path
Top
File metadata and controls
Code
Blame
208 lines (178 loc) · 7.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""HTTPS adapters for the original Python asyncio benchmark clients."""
from
contextlib
import
asynccontextmanager
import
ssl
from
urllib
.
parse
import
urlsplit
if
__package__
:
from
.
workloads
import
prepare_chunks
from
.
registry
import
(
CAPABILITIES
as
ALL_CAPABILITIES
,
SPECS
,
adapter_clients
,
POOL_LIMIT
,
RESPONSE_CHUNK_BYTES
,
)
else
:
from
workloads
import
prepare_chunks
from
registry
import
(
CAPABILITIES
as
ALL_CAPABILITIES
,
SPECS
,
adapter_clients
,
POOL_LIMIT
,
RESPONSE_CHUNK_BYTES
,
)
CLIENTS
=
adapter_clients
(
"async"
)
PACKAGE
=
{
client
:
SPECS
[
client
].
package
for
client
in
CLIENTS
}
CAPABILITIES
=
{
client
:
ALL_CAPABILITIES
[
client
]
for
client
in
CLIENTS
}
RESPONSE_READ
=
{
client
:
SPECS
[
client
].
response_read
for
client
in
CLIENTS
}
def
validate_target
(
url
):
target
=
urlsplit
(
url
)
if
(
target
.
scheme
!=
"https"
or
target
.
hostname
not
in
{
"127.0.0.1"
,
"localhost"
,
"::1"
}
or
target
.
username
is
not
None
):
raise
ValueError
(
"Unverified benchmark TLS is restricted to HTTPS loopback"
)
def
tls_context
():
context
=
ssl
.
SSLContext
(
ssl
.
PROTOCOL_TLS_CLIENT
)
context
.
check_hostname
=
False
context
.
verify_mode
=
ssl
.
CERT_NONE
context
.
minimum_version
=
context
.
maximum_version
=
ssl
.
TLSVersion
.
TLSv1_3
context
.
set_alpn_protocols
([
"http/1.1"
])
return
context
def
validation
(
status
,
actual
,
protocol
,
total
,
expected
):
if
status
!=
200
or
actual
!=
protocol
or
total
!=
expected
:
raise
RuntimeError
(
f"Expected 200/
{
protocol
}
/
{
expected
}
bytes, received "
f"
{
status
}
/
{
actual
}
/
{
total
}
bytes"
)
return
{
"status"
:
status
,
"protocol"
:
actual
,
"response_bytes"
:
total
}
async
def
parts
(
chunks
):
for
chunk
in
chunks
:
yield
chunk
@
asynccontextmanager
async
def
operations
(
client_id
,
protocol
,
url
,
body
,
body_kind
):
validate_target
(
url
)
capability
=
CAPABILITIES
[
client_id
]
if
(
protocol
not
in
capability
[
"protocols"
]
or
body_kind
not
in
capability
[
"body_kinds"
]
):
raise
ValueError
(
f"Unsupported benchmark:
{
client_id
}
/
{
protocol
}
/
{
body_kind
}
"
)
# Construct chunks outside timed batches; each request gets a fresh iterator.
chunks
=
prepare_chunks
(
body
,
body_kind
)
if
client_id
==
"httpx"
:
import
httpx
async
with
httpx
.
AsyncClient
(
verify
=
tls_context
(),
trust_env
=
False
,
http1
=
protocol
==
"h1"
,
http2
=
protocol
==
"h2"
,
timeout
=
None
,
limits
=
httpx
.
Limits
(
max_connections
=
None
,
max_keepalive_connections
=
None
),
)
as
client
:
async
def
post
():
async
with
client
.
stream
(
"POST"
,
url
,
content
=
parts
(
chunks
)
if
chunks
else
body
)
as
response
:
total
=
0
async
for
chunk
in
response
.
aiter_raw
():
total
+=
len
(
chunk
)
actual
=
{
"HTTP/1.1"
:
"h1"
,
"HTTP/2"
:
"h2"
}.
get
(
response
.
http_version
)
return
validation
(
response
.
status_code
,
actual
,
protocol
,
total
,
len
(
body
)
)
yield
post
elif
client_id
==
"aiohttp"
:
import
aiohttp
async
with
aiohttp
.
ClientSession
(
connector
=
aiohttp
.
TCPConnector
(
ssl
=
tls_context
(),
limit
=
0
),
trust_env
=
False
,
auto_decompress
=
False
,
timeout
=
aiohttp
.
ClientTimeout
(
total
=
None
),
)
as
client
:
async
def
post
():
async
with
client
.
post
(
url
,
data
=
parts
(
chunks
)
if
chunks
else
body
,
allow_redirects
=
False
)
as
response
:
total
=
0
async
for
chunk
in
response
.
content
.
iter_any
():
total
+=
len
(
chunk
)
actual
=
"h1"
if
response
.
version
==
aiohttp
.
HttpVersion11
else
None
return
validation
(
response
.
status
,
actual
,
protocol
,
total
,
len
(
body
)
)
yield
post
elif
client_id
==
"niquests"
:
import
niquests
from
niquests
.
packages
.
urllib3
import
disable_warnings
from
niquests
.
packages
.
urllib3
.
exceptions
import
InsecureRequestWarning
disable_warnings
(
InsecureRequestWarning
)
async
with
niquests
.
AsyncSession
(
verify
=
False
,
retries
=
0
,
disable_http1
=
protocol
==
"h2"
,
disable_http2
=
protocol
==
"h1"
,
disable_http3
=
True
,
pool_maxsize
=
POOL_LIMIT
,
revocation_configuration
=
None
,
tls_configuration
=
niquests
.
TLSConfiguration
(
backend
=
"ssl"
,
min_version
=
ssl
.
TLSVersion
.
TLSv1_3
,
max_version
=
ssl
.
TLSVersion
.
TLSv1_3
,
),
)
as
client
:
client
.
trust_env
=
False
async
def
post
():
async
with
await
client
.
post
(
url
,
data
=
parts
(
chunks
)
if
chunks
else
body
,
stream
=
True
,
allow_redirects
=
False
,
)
as
response
:
total
=
0
async
for
chunk
in
await
response
.
iter_raw
(
RESPONSE_CHUNK_BYTES
):
total
+=
len
(
chunk
)
actual
=
{
11
:
"h1"
,
20
:
"h2"
}.
get
(
response
.
http_version
)
return
validation
(
response
.
status_code
,
actual
,
protocol
,
total
,
len
(
body
)
)
yield
post
elif
client_id
==
"curl_cffi"
:
from
curl_cffi
import
CurlHttpVersion
,
CurlOpt
,
CurlSslVersion
from
curl_cffi
.
requests
import
AsyncSession
version
=
(
CurlHttpVersion
.
V1_1
if
protocol
==
"h1"
else
CurlHttpVersion
.
V2_PRIOR_KNOWLEDGE
)
async
with
AsyncSession
(
max_clients
=
POOL_LIMIT
,
verify
=
False
,
trust_env
=
False
,
allow_redirects
=
False
,
timeout
=
None
,
http_version
=
version
,
curl_options
=
{
CurlOpt
.
PROXY
:
""
,
# libcurl encodes the maximum TLS version in the high 16 bits.
CurlOpt
.
SSLVERSION
:
int
(
CurlSslVersion
.
TLSv1_3
)
|
(
int
(
CurlSslVersion
.
TLSv1_3
)
<<
16
),
},
)
as
client
:
async
def
post
():
async
with
client
.
stream
(
"POST"
,
url
,
content
=
parts
(
chunks
)
if
chunks
else
body
)
as
response
:
total
=
0
async
for
chunk
in
response
.
aiter_content
():
total
+=
len
(
chunk
)
actual
=
{
CurlHttpVersion
.
V1_1
:
"h1"
,
CurlHttpVersion
.
V2_0
:
"h2"
,
}.
get
(
response
.
http_version
)
return
validation
(
response
.
status_code
,
actual
,
protocol
,
total
,
len
(
body
)
)
yield
post
You can’t perform that action at this time.