-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart_builder.py
More file actions
219 lines (175 loc) · 7.62 KB
/
chart_builder.py
File metadata and controls
219 lines (175 loc) · 7.62 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import time
import random
import threading
import heapq
from collections import deque, defaultdict
from datetime import datetime
class LoadBalancer:
def __init__(self):
self.servers = []
self.health_checks = {}
self.routing_table = {}
self.algorithms = ['round_robin', 'least_connections', 'random', 'weighted', 'ip_hash']
self.algorithm = random.choice(self.algorithms)
self.current_index = 0
self.lock = threading.Lock()
self.request_log = deque(maxlen=10000)
def add_server(self, host, port, weight=1):
server_id = f"{host}:{port}"
server = {
'id': server_id,
'host': host,
'port': port,
'weight': weight,
'connections': 0,
'requests': 0,
'errors': 0,
'latency': [],
'healthy': True,
'last_check': time.time(),
'added_at': time.time()
}
self.servers.append(server)
return server_id
def generate_server_pool(self, count=5):
hosts = ['web1', 'web2', 'web3', 'app1', 'app2', 'api1', 'api2', 'cache1', 'db1']
domains = ['internal', 'cluster.local', 'service.consul', 'backend.svc']
for i in range(count):
host = f"{random.choice(hosts)}-{i}.{random.choice(domains)}"
port = random.choice([80, 443, 3000, 8080, 8443])
weight = random.randint(1, 5)
self.add_server(host, port, weight)
def round_robin(self):
with self.lock:
healthy_servers = [s for s in self.servers if s['healthy']]
if not healthy_servers:
return None
self.current_index = (self.current_index + 1) % len(healthy_servers)
return healthy_servers[self.current_index]
def least_connections(self):
with self.lock:
healthy_servers = [s for s in self.servers if s['healthy']]
if not healthy_servers:
return None
return min(healthy_servers, key=lambda s: s['connections'])
def random_server(self):
with self.lock:
healthy_servers = [s for s in self.servers if s['healthy']]
if not healthy_servers:
return None
return random.choice(healthy_servers)
def weighted_random(self):
with self.lock:
healthy_servers = [s for s in self.servers if s['healthy']]
if not healthy_servers:
return None
weights = [s['weight'] for s in healthy_servers]
total = sum(weights)
r = random.uniform(0, total)
cumsum = 0
for server, weight in zip(healthy_servers, weights):
cumsum += weight
if r <= cumsum:
return server
return healthy_servers[0]
def ip_hash(self, client_ip):
with self.lock:
healthy_servers = [s for s in self.servers if s['healthy']]
if not healthy_servers:
return None
hash_val = hash(client_ip) % len(healthy_servers)
return healthy_servers[hash_val]
def get_server(self, client_ip=None):
if self.algorithm == 'round_robin':
return self.round_robin()
elif self.algorithm == 'least_connections':
return self.least_connections()
elif self.algorithm == 'random':
return self.random_server()
elif self.algorithm == 'weighted':
return self.weighted_random()
elif self.algorithm == 'ip_hash' and client_ip:
return self.ip_hash(client_ip)
else:
return self.round_robin()
def handle_request(self, client_ip=None):
server = self.get_server(client_ip)
if not server:
return {'error': 'no healthy servers'}
server['connections'] += 1
server['requests'] += 1
start_time = time.time()
latency = random.uniform(5, 100)
time.sleep(latency / 1000)
success = random.random() > 0.05
status_code = 200 if success else random.choice([500, 502, 503, 504])
if not success:
server['errors'] += 1
server['connections'] -= 1
server['latency'].append(latency)
if len(server['latency']) > 100:
server['latency'] = server['latency'][-100:]
request_log = {
'timestamp': time.time(),
'client': client_ip or f"192.168.{random.randint(1,254)}.{random.randint(1,254)}",
'server': server['id'],
'latency': latency,
'status': status_code,
'algorithm': self.algorithm
}
self.request_log.append(request_log)
return request_log
def health_check(self):
for server in self.servers:
if time.time() - server['last_check'] > 5:
server['healthy'] = random.random() > 0.1
server['last_check'] = time.time()
def get_server_stats(self, server_id):
for server in self.servers:
if server['id'] == server_id:
return {
'id': server['id'],
'healthy': server['healthy'],
'requests': server['requests'],
'errors': server['errors'],
'error_rate': server['errors'] / max(server['requests'], 1),
'avg_latency': sum(server['latency']) / max(len(server['latency']), 1),
'connections': server['connections']
}
return None
def get_overall_stats(self):
total_requests = sum(s['requests'] for s in self.servers)
total_errors = sum(s['errors'] for s in self.servers)
healthy_count = sum(1 for s in self.servers if s['healthy'])
return {
'algorithm': self.algorithm,
'total_servers': len(self.servers),
'healthy_servers': healthy_count,
'total_requests': total_requests,
'total_errors': total_errors,
'error_rate': total_errors / max(total_requests, 1),
'requests_logged': len(self.request_log)
}
def simulate_traffic(self, duration=30):
end_time = time.time() + duration
while time.time() < end_time:
self.health_check()
client_ip = f"{random.randint(1,254)}.{random.randint(0,254)}.{random.randint(0,254)}.{random.randint(1,254)}"
for _ in range(random.randint(1, 10)):
self.handle_request(client_ip)
time.sleep(random.uniform(0.01, 0.1))
def change_algorithm(self):
self.algorithm = random.choice([a for a in self.algorithms if a != self.algorithm])
def main():
lb = LoadBalancer()
lb.generate_server_pool(8)
print(f"Starting load balancer with {len(lb.servers)} servers, algorithm: {lb.algorithm}")
for i in range(3):
lb.change_algorithm()
lb.simulate_traffic(5)
stats = lb.get_overall_stats()
print(f"Load balancer: {stats['total_requests']} requests, "
f"error rate: {stats['error_rate']:.2%}")
if __name__ == "__main__":
main()