-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_tester.py
171 lines (142 loc) · 5.28 KB
/
node_tester.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
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
import os
import threading
import subprocess
import time
import re
import riddler_interface as interface
class client(threading.Thread):
def __init__(self, controller, dest_node, run_info, args):
super(client, self).__init__(None)
self.controller = controller
self.dest_node = dest_node
self.run_info = run_info
self.args = args
self.running = False
self.ping_p = None
self.timer = threading.Timer(run_info['test_time']*2, self.kill_client)
def run(self):
h = self.dest_node['host']
t = str(self.run_info['test_time'])
l = str(self.run_info['iperf_len'])
r = str(self.run_info['rate'])
c = str(self.run_info['gen_size'])
p = os.path.join(self.args.udp_path, "udp_client.py")
cmd = [p, h, l, r, t, "1", c]
print(" Starting client: {}".format(cmd))
self.timer.start()
try:
self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
except OSError as e:
self.report_error(e.message + ":" + " ".join(cmd))
return
self.running = True
self.p.wait()
self.running = False
(stdout, stderr) = self.p.communicate()
if stderr:
self.report_error("udp_client.py error: {}".format(stderr))
return
elif not stdout:
self.report_error("No output from command {0}".format(" ".join(cmd)))
return
result = {"role": "src"}
for line in stdout.splitlines():
key,val = line.split(": ")
result[key] = float(val)
if result:
self.report_result(result)
else:
self.report_error("Missing result")
# Brutally kill a running subprocesses
def kill_client(self):
# Make sure even have a running subprocess
if not self.running:
return
try:
# Ask politely first
print(" Terminating client (pid {0})".format(self.p.pid))
self.p.terminate()
# Ask again, if necessary
if not self.p.poll():
self.p.terminate()
# No more patience, kill the damn thing
if not self.p.poll():
self.p.kill()
except OSError as e:
print(" Killing client failed: {0}".format(e))
# We are done
self.running = False
# Send back a result to the controller
def report_result(self, result):
print(" Reporting result")
obj = interface.node(interface.RUN_RESULT, result=result)
self.controller.report(obj)
# Send back an error to the controller
def report_error(self, error):
print(" Reporting error")
obj = interface.node(interface.RUN_ERROR, error=error)
self.controller.report(obj)
class server(threading.Thread):
def __init__(self, controller, args, run_info):
super(server, self).__init__(None)
self.controller = controller
self.args = args
self.protocol = run_info['protocol']
self.tcp_window = run_info['tcp_window']
self.iperf_len = run_info['iperf_len']
self.run_info = run_info
self.running = False
self.start()
def run(self):
l = str(self.iperf_len)
c = str(self.run_info['gen_size'])
p = os.path.join(self.args.udp_path, "udp_server.py")
self.cmd = [p, l, c, "1"]
print(" Starting server: {}".format(self.cmd))
try:
self.p = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
except OSError as e:
obj = interface.node(interface.RUN_ERROR, error=e.message + ":" + " ".join(self.cmd))
self.controller.report(obj)
return
self.running = True
self.p.wait()
self.running = False
(stdout,stderr) = self.p.communicate()
if stderr:
obj = interface.node(interface.RUN_ERROR, error=stderr)
self.controller.report(obj)
return
elif not stdout:
obj = interface.node(interface.RUN_ERROR, error="no server result")
self.controller.report(obj)
return
result = {"role": "dst"}
for line in stdout.splitlines():
key,val = line.split(": ")
result[key] = float(val)
if result:
obj = interface.node(interface.RUN_RESULT, result=result)
self.controller.report(obj)
else:
obj = interface.node(interface.RUN_ERROR, error="empty server result")
self.controller.report(obj)
# Kill a running iperf server
def kill(self):
# Check if process is running at all
if not self.running:
return
try:
# Politely ask server to quit
print(" Terminating server (pid {0})".format(self.p.pid))
self.p.terminate()
# Ask again if necessary
if not self.p.poll():
self.p.terminate()
# No more patience, kill the damn thing
if not self.p.poll():
self.p.kill()
except OSError as e:
print(" Killing server failed: {0}".format(e))
# We are done here
self.running = False