-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIPerformaceAsync.py
More file actions
38 lines (29 loc) · 925 Bytes
/
Copy pathAPIPerformaceAsync.py
File metadata and controls
38 lines (29 loc) · 925 Bytes
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
import requests
import time
import threading
api_list = [
'https://api.example.com/endpoint1',
'https://api.example.com/endpoint2',
'https://api.example.com/endpoint3',
]
num_tests = 10
def test_api(api):
response_times = []
for i in range(num_tests):
start_time = time.time()
response = requests.get(api)
end_time = time.time()
response_time = (end_time - start_time) * 1000
response_times.append(response_time)
avg_response_time = sum(response_times) / len(response_times)
print(f'API: {api}')
print(f'Average response time: {avg_response_time:.2f} ms\n')
# Loop through the API list and test each API using a separate thread
threads = []
for api in api_list:
thread = threading.Thread(target=test_api, args=(api,))
thread.start()
threads.append(thread)
# Wait for all threads to complete
for thread in threads:
thread.join()