-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_host_static_subprocess.py
129 lines (115 loc) · 6.2 KB
/
config_host_static_subprocess.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
import sys
import random
import subprocess
import time
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
print(f'iperf3 -s from {self.my_addr}, port {port}')
while (True):
subprocess.run(['iperf3', '-s', '-p', f'{port}'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
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)
port=self.first_port + int(self.hostname[1:])
bandwidth = 10 * (j + 1) #bps
#find which server open the selected port
if self.hostname == 'h0' or self.hostname == 'h1':
server_hostname = self.ip_svrs[0] #h6
elif self.hostname == 'h4' or self.hostname == 'h5':
server_hostname = self.ip_svrs[1] #h7
elif self.hostname == 'h2':
server_hostname = self.ip_svrs[2] #h8
elif self.hostname == 'h3':
server_hostname = self.ip_svrs[3] #h9
duration = 15
if j >= 5:
duration = 60
while True:
print(f'iperf to {server_hostname}, bw: {bandwidth}Mbps, time: {duration}s, port: {port}')
test = subprocess.run(['iperf3', '-c', server_hostname, '-b', f'{bandwidth}M', '-t', f'{duration}', '-p', f'{port}', '-J'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
if test.returncode == 0:
json_test = json.loads(test.stdout)
Mbps = json_test['end']['streams'][0]['receiver']['bits_per_second']
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((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)
time.sleep(10)
continue
time.sleep(random.randint(1, 5))
if __name__ == '__main__':
if len(sys.argv) < 5:
print('usage: python3 config_host_static.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_static2.py train 2 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_static2.py test 15 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