-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAPI.py
More file actions
55 lines (47 loc) · 1.66 KB
/
API.py
File metadata and controls
55 lines (47 loc) · 1.66 KB
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
import requests
import time
import matplotlib.pyplot as plt
def fetch_api_data(url):
"""Fetch data from a given URL."""
response = requests.get(url)
if response.status_code == 200:
return response.json() # Return the JSON data
else:
return None
def fetch_data_sequentially(urls):
"""Fetch data sequentially from a list of URLs and record the time taken for each."""
results = []
times = []
for url in urls:
start_time = time.time()
data = fetch_api_data(url)
end_time = time.time()
times.append(end_time - start_time)
if data is not None:
results.append(data)
print(f"Data fetched from {url}")
else:
print(f"Failed to fetch data from {url}")
return results, times
def time_function(func, *args, **kwargs):
"""Measure the total execution time of a function."""
start_time = time.time()
result, times = func(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
return result, times, execution_time
def plot_performance(times):
"""Plot the execution time for each API call."""
plt.figure(figsize=(10, 5))
plt.plot(times, marker='o', linestyle='-')
plt.title('API Request Execution Times')
plt.xlabel('Request Index')
plt.ylabel('Execution Time (seconds)')
plt.grid(True)
plt.show()
# Example usage
urls = [f"https://jsonplaceholder.typicode.com/posts/{i}" for i in range(1, 21)]
results, times, total_time = time_function(fetch_data_sequentially, urls)
print("Fetched data from all endpoints.")
print(f"Total time taken: {total_time:.2f} seconds")
plot_performance(times)