forked from PranavSinghKanwar/Server-Load-Balancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual.py
More file actions
108 lines (93 loc) · 3.47 KB
/
Copy pathvisual.py
File metadata and controls
108 lines (93 loc) · 3.47 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# import requests
# import matplotlib.pyplot as plt
# import time
# # List of websites to check
# websites = [
# "https://example.com",
# "https://facebook.com",
# "https://instagram.com",
# "https://jsonplaceholder.typicode.com",
# "https://api.publicapis.org",
# "https://dog.ceo/api/breeds/list/all",
# ]
# # Function to fetch data from websites and measure time to connect
# def measure_time_to_connect(website):
# try:
# response = requests.get(website)
# return response.elapsed.total_seconds() * 1000 # Convert to milliseconds
# except requests.RequestException:
# return None
# # Function to update and display the plot
# def update_plot():
# # Measure time to connect for each website
# times_to_connect = {website: measure_time_to_connect(website) for website in websites}
# # Filter out websites with None response times
# valid_times_to_connect = {website: time for website, time in times_to_connect.items() if time is not None}
# # Plot the time to connect for each website
# plt.clf() # Clear the previous plot
# plt.bar(valid_times_to_connect.keys(), valid_times_to_connect.values())
# plt.xlabel('Website')
# plt.ylabel('Time to Connect (ms)')
# plt.title('Time to Connect for Each Website')
# plt.xticks(rotation=45, ha='right')
# plt.tight_layout()
# plt.pause(5) # Pause for 5 seconds before updating
# # Continuous loop to update the plot
# while True:
# update_plot()
# import requests
# import matplotlib.pyplot as plt
# import time
# def fetch_server_stats():
# try:
# response = requests.get("http://127.0.0.1:8081/stats")
# if response.status_code == 200:
# return response.json()
# else:
# print("Failed to fetch server stats. Status Code:", response.status_code)
# return {}
# except requests.RequestException as e:
# print("Error fetching server stats:", e)
# return {}
# def visualize_server_stats(server_stats):
# server_addresses = list(server_stats.keys())
# response_times = list(server_stats.values())
# plt.bar(server_addresses, response_times)
# plt.xlabel('Server Address')
# plt.ylabel('Time to Connect (ms)')
# plt.title('Server Response Times')
# plt.xticks(rotation=45)
# plt.tight_layout()
# plt.show()
# if __name__ == "__main__":
# while True:
# server_stats = fetch_server_stats()
# visualize_server_stats(server_stats)
# time.sleep(3) # Update every 3 seconds
import requests
import matplotlib.pyplot as plt
import time
def fetch_server_stats():
try:
response = requests.get("http://127.0.0.1:8081/stats")
if response.status_code == 200:
return response.json()
else:
print("Failed to fetch server stats. Status Code:", response.status_code)
return {}
except requests.RequestException as e:
print("Error fetching server stats:", e)
return {}
def visualize_server_stats(server_stats):
server_addresses = list(server_stats.keys())
response_times = list(server_stats.values())
plt.bar(server_addresses, response_times)
plt.xlabel('Server')
plt.ylabel('Response Time (ms)')
plt.title('Backend Server Response Times')
plt.xticks(rotation=45)
plt.show()
while True:
server_stats = fetch_server_stats()
visualize_server_stats(server_stats)
time.sleep(3) # Update and visualize server stats every 3 seconds