Last active
June 2, 2017 17:43
-
-
Save samuelcolvin/00f01793c118bf9aafae886ffbc81a58 to your computer and use it in GitHub Desktop.
demonstrating the performance of `asyncio.wait`
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
import asyncio | |
from statistics import mean, stdev | |
from time import time | |
from aiohttp import ClientSession | |
request_count = 1500 | |
url = 'https://requestb.in/1fz8k451' | |
async def main(loop, use_first_complete=False): | |
print(f'using FIRST_COMPLETED: {use_first_complete}') | |
start_time = time() | |
async with ClientSession(loop=loop) as session: | |
async def run(v, _create_time): | |
_start_time = time() | |
async with session.get(url) as r: | |
assert r.status == 200 | |
wait, request = _start_time - _create_time, time() - _start_time | |
# print(f'{v:>5} wait: {wait:0.3f}s, request: {request:0.3f}s') | |
return wait, request | |
pending_tasks = {loop.create_task(run(i, time())) for i in range(request_count)} | |
if use_first_complete: | |
complete_tasks = set() | |
while pending_tasks: | |
done, pending_tasks = await asyncio.wait(pending_tasks, loop=loop, return_when=asyncio.FIRST_COMPLETED) | |
complete_tasks.update(done) | |
else: | |
complete_tasks, pending_tasks = await asyncio.wait(pending_tasks, loop=loop) | |
assert not pending_tasks | |
time_taken = time() - start_time | |
print(f'total time taken {time_taken:0.2f}') | |
results = [t.result() for t in complete_tasks] | |
wait_times, request_times = zip(*results) | |
print(f'wait times: mean {mean(wait_times):0.3f}s, stdev {stdev(wait_times):0.3f}s') | |
print(f'request times: mean {mean(request_times):0.3f}s, stdev {stdev(request_times):0.3f}s') | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main(loop, use_first_complete=False)) |
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
➤ python perf.py | |
using FIRST_COMPLETED: False | |
total time taken 4.07 | |
wait times: mean 0.136s, stdev 0.059s | |
request times: mean 1.824s, stdev 0.947s | |
➤ python perf.py | |
using FIRST_COMPLETED: False | |
total time taken 5.05 | |
wait times: mean 0.135s, stdev 0.057s | |
request times: mean 2.172s, stdev 1.125s | |
➤ python perf.py | |
using FIRST_COMPLETED: False | |
total time taken 5.98 | |
wait times: mean 0.142s, stdev 0.060s | |
request times: mean 2.450s, stdev 1.334s | |
➤ python perf.py | |
using FIRST_COMPLETED: True | |
total time taken 47.46 | |
wait times: mean 0.132s, stdev 0.058s | |
request times: mean 10.103s, stdev 8.111s | |
➤ python perf.py | |
using FIRST_COMPLETED: True | |
total time taken 27.69 | |
wait times: mean 0.138s, stdev 0.057s | |
request times: mean 8.536s, stdev 7.914s | |
➤ python perf.py | |
using FIRST_COMPLETED: True | |
total time taken 18.18 | |
wait times: mean 0.139s, stdev 0.059s | |
request times: mean 5.934s, stdev 5.327s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment