-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_host_static_h6bottleneck.py
137 lines (123 loc) · 6.25 KB
/
config_host_static_h6bottleneck.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
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
# In this file h0 and h1 send to h6 creating a bottleneck.
# we will remove this bottleneck, by using h6 dedicated to h0
# and moving h1 to an other server.
import sys
import random
import subprocess
import time
import iperf3
from multiprocessing import Process
import json
class TrafficGenerator:
def __init__(self):
self.gender=sys.argv[1]
if self.gender.lower() != 'test' and self.gender.lower() != 'train':
print('insert test or train only!')
exit(-2)
self.num_iperf=int(sys.argv[2])
self.hostname=sys.argv[3]
self.list_ip=sys.argv[4].split(',')
self.svr_num = 4 #h6, h7, h8, h9
self.cl_num = int(len(self.list_ip) - self.svr_num)
# NOTE: last addresses in the list are used as iperf3 -s
self.ip_svrs = self.list_ip[-self.svr_num:]
#Grab IP host
proc = subprocess.run(['hostname', '-I'], stdout=subprocess.PIPE, universal_newlines=True)
list_addresses = proc.stdout.split(' ')
self.my_addr = list(filter(lambda el: el.startswith('192.168'), list_addresses))[0]
self.num_load = 10 # from 10 Mbps to 100 Mbps
self.first_port=6969
random.seed(19951018+int(self.hostname[1:]))
def run_server(self, port):
#run one thread for each port
server = iperf3.Server()
server.bind_address=self.my_addr
server.port=port
# with open('file', 'a') as sys.stdout:
print(f'iperf3 -s from {self.my_addr}, port {port}')
while True:
test = server.run()
# with open('file', 'a') as sys.stdout:
print(test)
def generate_traffic(self):
if self.my_addr in self.ip_svrs: #one of the server
processes = []
if self.hostname == 'h6':
#run num port needed for each server
for i in range(2): #One thread for each port!
proc_port = self.first_port + i # 6969= h0, 6970=h1
proc = Process(target=self.run_server, args=(proc_port, ) )
processes.append(proc)
proc.start()
for i in range(2):
processes[i].join()
elif self.hostname == 'h7':
for i in range(2): #One thread for each port!
proc_port = 6973 + i # 6973= h4, 6974=h5
proc = Process(target=self.run_server, args=(proc_port, ) )
processes.append(proc)
proc.start()
for i in range(2):
processes[i].join()
elif self.hostname == 'h8':
proc_port = 6971 # 6971= h2
proc = Process(target=self.run_server, args=(proc_port, ) )
processes.append(proc)
proc.start()
processes[0].join()
elif self.hostname == 'h9':
proc_port = 6972 # 6972= h3
proc = Process(target=self.run_server, args=(proc_port, ) )
processes.append(proc)
proc.start()
processes[0].join()
else:
for j in range(self.num_load): # 20 test at [10, 20, 30, ..., 100] Mbps
for i in range(self.num_iperf):
print(f'iteration {i}/{self.num_iperf}, {(j+1)*10}Mbps')
# generate a random iperf3 request: f(t, bw, svr)
client = iperf3.Client()
client.port=self.first_port + int(self.hostname[1:])
client.bandwidth = 10 * (j + 1) * 1024 * 1024 #Mbps
client.duration = 120 # seconds
#find which server open the selected port
if self.hostname == 'h0' or self.hostname == 'h1':
client.server_hostname = self.ip_svrs[0] #h6
elif self.hostname == 'h4' or self.hostname == 'h5':
client.server_hostname = self.ip_svrs[1] #h7
elif self.hostname == 'h2':
client.server_hostname = self.ip_svrs[2] #h8
elif self.hostname == 'h3':
client.server_hostname = self.ip_svrs[3] #h9
while True:
print(f'iperf to {client.server_hostname}, bw: {client.bandwidth}bps, time: {client.duration}s')
test = client.run()
if test.error == None:
json_test = test.json
Mbps = test.sent_Mbps
mean_rtt=json_test['end']['streams'][0]['sender']['mean_rtt']
print(f'Mbps: {Mbps}, mean_rtt: {mean_rtt}')
if self.gender.lower() == 'test' and self.hostname == 'h0':
#save measurement on file.
Mbps = int((client.bandwidth/1024)/1024)
filename = 'iperf3_total.txt'
with open(filename, "a") as file1:
# Writing data to a file
file1.write(json.dumps(json_test)+'\n')
break
else:
print(test.error)
time.sleep(10)
continue
del client
time.sleep(random.randint(1, 5))
if __name__ == '__main__':
if len(sys.argv) < 5:
print('usage: python3 config_host.py <train/test> <num_iperf> <hostnameID> <ip1>,<ipN>')
exit(-1)
tf = TrafficGenerator()
tf.generate_traffic()
#git clone https://github.com/Enrico-git/NGI-support.git
#sudo rm -rf /NGI-support-main/ ; sudo mv NGI-support/ /NGI-support-main
#python3 /NGI-support-main/config_host_static.py train 3 h0 192.168.0.2,192.168.0.4,192.168.0.9,192.168.0.11,192.168.0.15,192.168.0.17,192.168.0.30,192.168.0.32,192.168.0.37,192.168.0.39
#python3 /NGI-support-main/config_host_static.py test 8 h0 192.168.0.2,192.168.0.4,192.168.0.9,192.168.0.11,192.168.0.15,192.168.0.17,192.168.0.30,192.168.0.32,192.168.0.37,192.168.0.39