-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiohttp-client.py
70 lines (53 loc) · 2.14 KB
/
aiohttp-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
#!/usr/bin/env python3
import asyncio
import aiohttp
import time
import os
def print_report(total_count, total_time_ms):
print("Total time for {} requests = {} ms.".format(
total_count, total_time_ms))
print("Avg time per request = {} ms.\n".format(
total_time_ms / total_count))
print("-----------------------------------------\n")
async def async_put_api(session):
resp = await session.put("http://localhost/hello", data="Hello world!")
assert resp.status == 200
return
# Test case 1: Runs each request in sequence.
async def test_case_seq_reqs(total_count):
print("\nSequential Requests:\nParams: total_count {}".format(total_count))
client_session = aiohttp.ClientSession()
total_time_ms = 0
for i in range(total_count):
start_time = time.perf_counter()
await async_put_api(client_session)
end_time = time.perf_counter()
elapsed_time_ms = int(round((end_time - start_time) * 1000))
total_time_ms += elapsed_time_ms
print_report(total_count, total_time_ms)
await client_session.close()
# Test case 2: Runs all requests in parallel and runs slowest.
async def test_case_parallel(total_count, max_conn):
print("\nParallel Requests:\nParams: total_count {}, max_conn {}".format(
total_count, max_conn))
connector = aiohttp.TCPConnector(limit=max_conn, limit_per_host=max_conn)
client_session = aiohttp.ClientSession(connector=connector)
task_list = []
for i in range(total_count):
task = asyncio.ensure_future(async_put_api(client_session))
task_list.append(task)
# Launch the operations.
start_time = time.perf_counter()
await asyncio.gather(*task_list)
end_time = time.perf_counter()
elapsed_time_ms = int(round((end_time - start_time) * 1000))
print_report(total_count, elapsed_time_ms)
await client_session.close()
async def main():
total_count = 100
# Sequential requests.
await test_case_seq_reqs(total_count=total_count)
# Parallel requests - all
await test_case_parallel(total_count=total_count, max_conn=100)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())