-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLatencyThroughput.py
44 lines (37 loc) · 1.37 KB
/
LatencyThroughput.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
import requests
import time
API_ENDPOINT = "https://api.example.com"
# def measure_latency():
# latencies = []
# for i in range(10):
# start_time = time.time()
# response = requests.get(API_ENDPOINT)
# end_time = time.time()
# latency = end_time - start_time
# latencies.append(latency)
# print(f"Request {i+1}: {latency:.4f} seconds")
# average_latency = sum(latencies) / len(latencies)
# print(f"Average latency: {average_latency:.4f} seconds")
def measure_latency():
latencies = []
with requests.Session() as session:
for i in range(10):
start_time = time.time()
response = session.get(API_ENDPOINT)
end_time = time.time()
latency = end_time - start_time
latencies.append(latency)
print(f"Request {i+1}: {latency:.4f} seconds")
average_latency = sum(latencies) / len(latencies)
print(f"Average latency: {average_latency:.4f} seconds")
def measure_throughput():
with requests.Session() as session:
start_time = time.time()
for i in range(10):
response = session.get(API_ENDPOINT)
end_time = time.time()
total_time = end_time - start_time
throughput = 10 / total_time
print(f"Throughput: {throughput:.2f} requests/second")
measure_latency()
measure_throughput()