-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests-threading.py
69 lines (52 loc) · 1.98 KB
/
requests-threading.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
#!/usr/bin/env python3
import requests
import threading
import time
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")
def thread_func_put_api(session):
resp = session.put("http://localhost/hello", data="Hello World!!")
assert resp.status_code == 200
return
# Test case 1: Runs each request in sequence.
def test_case_seq_reqs(total_count):
print("\nSequential Requests:\nParams: total_count {}".format(total_count))
client_session = requests.Session()
start_time = time.perf_counter()
for i in range(total_count):
thread_func_put_api(client_session)
end_time = time.perf_counter()
total_time_ms = int(round((end_time - start_time) * 1000))
print_report(total_count, total_time_ms)
# Test case 2: Runs all requests in parallel, 1 request per thread.
def test_case_parallel(total_count):
print("\nParallel Requests:\nParams: total_count {}, threads_count {}".format(
total_count, total_count))
client_session = requests.Session()
# Start threads to upload objects.
request_threads = []
start_time = time.perf_counter()
for i in range(total_count):
t = threading.Thread(
target=thread_func_put_api, args=(client_session,))
request_threads.append(t)
t.start()
# Wait for threads to complete.
total_time_ms = 0
for i in range(total_count):
request_threads[i].join()
end_time = time.perf_counter()
total_time_ms = int(round((end_time - start_time) * 1000))
print_report(total_count, total_time_ms)
def main():
total_count = 100
# Sequential requests.
test_case_seq_reqs(total_count=total_count)
# Parallel requests with threads - all
test_case_parallel(total_count=total_count)
if __name__ == '__main__':
main()