Skip to content

Commit 05a591d

Browse files
Create APIPerformaceAsync.py
1 parent 4177532 commit 05a591d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

APIPerformaceAsync.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import requests
2+
import time
3+
import threading
4+
5+
api_list = [
6+
'https://api.example.com/endpoint1',
7+
'https://api.example.com/endpoint2',
8+
'https://api.example.com/endpoint3',
9+
]
10+
11+
num_tests = 10
12+
13+
def test_api(api):
14+
response_times = []
15+
16+
for i in range(num_tests):
17+
start_time = time.time()
18+
response = requests.get(api)
19+
end_time = time.time()
20+
21+
response_time = (end_time - start_time) * 1000
22+
response_times.append(response_time)
23+
24+
avg_response_time = sum(response_times) / len(response_times)
25+
26+
print(f'API: {api}')
27+
print(f'Average response time: {avg_response_time:.2f} ms\n')
28+
29+
# Loop through the API list and test each API using a separate thread
30+
threads = []
31+
for api in api_list:
32+
thread = threading.Thread(target=test_api, args=(api,))
33+
thread.start()
34+
threads.append(thread)
35+
36+
# Wait for all threads to complete
37+
for thread in threads:
38+
thread.join()

0 commit comments

Comments
 (0)