forked from aio-libs/aiohttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecated BaseRequest.has_body, replaced with 2 new attributes (aio-…
…libs#2005) (aio-libs#2169) * Deprecated BaseRequest.has_body, replaced with 2 new attributes * Test obsolete BaseRequest.has_body attr
- Loading branch information
1 parent
bdd862b
commit 22c0fed
Showing
5 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
- Deprecated BaseRequest.has_body in favour of BaseRequest.can_read_body | ||
- Added BaseRequest.body_exists attribute that stays static for the lifetime | ||
of the request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import asyncio | ||
import atexit | ||
import csv | ||
import math | ||
import os | ||
import signal | ||
|
||
PORT = 8888 | ||
|
||
server = os.fork() | ||
if server == 0: | ||
loop = asyncio.get_event_loop() | ||
coro = asyncio.start_server(lambda *_: None, port=PORT) | ||
loop.run_until_complete(coro) | ||
loop.run_forever() | ||
else: | ||
atexit.register(os.kill, server, signal.SIGTERM) | ||
|
||
|
||
async def write_joined_bytearray(writer, chunks): | ||
body = bytearray(chunks[0]) | ||
for c in chunks[1:]: | ||
body += c | ||
writer.write(body) | ||
|
||
async def write_joined_list(writer, chunks): | ||
body = b''.join(chunks) | ||
writer.write(body) | ||
|
||
async def write_separately(writer, chunks): | ||
for c in chunks: | ||
writer.write(c) | ||
|
||
|
||
def fm_size(s, _fms=('', 'K', 'M', 'G')): | ||
i = 0 | ||
while s > 1024: | ||
s /= 1024 | ||
i += 1 | ||
return '{:.1f}{}B'.format(s, _fms[i]) | ||
|
||
writes = { | ||
'Join the chunks in a bytearray then write': write_joined_bytearray, | ||
'Join the chunks with a list then write': write_joined_list, | ||
'Write the chunks separately': write_separately, | ||
} | ||
|
||
bodies = ( | ||
[], | ||
[2 ** 7], | ||
[2 ** 17], | ||
[2 ** 27], | ||
[2 ** 30], | ||
[1000 * 2 ** 0 for _ in range(1)], | ||
[ 100 * 2 ** 0 for _ in range(10)], | ||
[ 10 * 2 ** 0 for _ in range(100)], | ||
[ 2 ** 0 for _ in range(1000)], | ||
[1000 * 2 ** 10 for _ in range(1)], | ||
[ 100 * 2 ** 10 for _ in range(10)], | ||
[ 10 * 2 ** 10 for _ in range(100)], | ||
[ 2 ** 10 for _ in range(1000)], | ||
[1000 * 2 ** 20 for _ in range(1)], | ||
[ 100 * 2 ** 20 for _ in range(10)], | ||
[ 10 * 2 ** 20 for _ in range(100)], | ||
[ 2 ** 20 for _ in range(1000)], | ||
) | ||
|
||
jobs = [( | ||
# always start with a 256B headers chunk | ||
'{} in {} chunks'.format(fm_size(sum(j) if j else 0), len(j)), | ||
[b'0' * s for s in [256] + list(j)], | ||
) for j in bodies] | ||
|
||
async def time(loop, fn, *args): | ||
spent = [] | ||
while len(spent) < 10000 and (not spent or sum(spent) < .2): | ||
s = loop.time() | ||
await fn(*args) | ||
e = loop.time() | ||
spent.append(e - s) | ||
mean = sum(spent) / len(spent) | ||
sd = sum((x - mean) ** 2 for x in spent) / len(spent) | ||
return len(spent), mean, math.sqrt(sd) | ||
|
||
async def main(loop): | ||
_, writer = await asyncio.open_connection(port=PORT) | ||
res = [] | ||
for t, c in jobs: | ||
print(t) | ||
for k, v in writes.items(): | ||
print('{:<42}: '.format(k), end='') | ||
coro = time(loop, v, writer, c) | ||
try: | ||
it, mean, sd = await asyncio.wait_for(coro, 2) | ||
except asyncio.TimeoutError: | ||
print('timed out') | ||
else: | ||
print('{:.6f}ms per loop (stdev: {:.3f}ms, {} iterations)'.format(mean * 1000, sd * 1000, it)) | ||
res.append([t, k, mean, sd, it]) | ||
print('--') | ||
with open('bench.csv', 'w', newline='') as f: | ||
w = csv.writer(f) | ||
w.writerow(['Job', 'Writer', 'Mean', 'St dev', 'Iterations']) | ||
for r in res: | ||
w.writerow(r) | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main(loop)) |