forked from hundeboll/riddler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_tester.py
More file actions
189 lines (159 loc) · 6.51 KB
/
node_tester.py
File metadata and controls
189 lines (159 loc) · 6.51 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
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):
super(client, self).__init__(None)
self.controller = controller
self.dest_node = dest_node
self.run_info = run_info
self.running = False
self.timer = threading.Timer(run_info['test_time']*2, self.kill_client)
self.end = threading.Event()
# Run the specified test in a separate thread
def run(self):
h = self.dest_node['host']
t = str(self.run_info['test_time'])
p = str(self.dest_node['port'])
w = str(self.run_info['tcp_window'])
# Craft the iperf command depending on the given protocol
if self.run_info['protocol'] == 'tcp':
cmd = ["iperf", "-c", h, "-t", t, "-yc", "-p", p, "-w", w]
elif self.run_info['protocol'] == 'udp':
r = str(self.run_info['rate']*1024)
cmd = ["iperf", "-c", h, "-u", "-b", r, "-t", t, "-p", p, "-yC"]
# Start a little watchdog to make sure we don't hang here forever
self.timer.start()
# Start the client in a separate process and wait for it to finish
print(" Starting {0} client".format(self.run_info['protocol']))
self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.running = True
self.p.wait()
self.running = False
# Read the output from iperf
(stdout, stderr) = self.p.communicate()
# Check and parse the output
if stderr:
self.report_error("Iperf client error: {0}".format(stderr))
return
elif not stdout:
self.report_error("No output from command {0}".format(" ".join(cmd)))
return
elif self.run_info['protocol'] == 'tcp':
result = self.parse_tcp_output(stdout)
elif self.run_info['protocol'] == 'udp':
result = self.parse_udp_output(stdout)
# Send back our result
if result:
self.report_result(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
# Screen scrape the output from a iperf TCP client
def parse_tcp_output(self, output):
# Now convert and format the results
try:
# Remove trailing newlines and get the comma separated values
output = output.strip()
vals = output.split(",")
return {
'dest': self.dest_node['name'],
'transfered': int(vals[7])/8/1024, # kB
'throughput': int(vals[8])/1024, # kbit/s
}
except IndexError as e:
print(" Failed to parse result: {0}".format(e))
self.report_error(output)
return None
# Screen scrape the output from a iperf UDP client
def parse_udp_output(self, output):
t = self.run_info['test_time']
# Convert and format the results
try:
# Select 2nd line and get the comma separated values
output = output.split()[1]
vals = output.split(",")
return {
'dest': self.dest_node['name'],
'transfered': int(vals[7])/8/1024, # kB
'throughput': int(vals[7])*8/1024/t, # kbit/s
'jitter': float(vals[9]), # seconds
'lost': int(vals[10]), # packets
'total': int(vals[11]), # packets
'ratio': float(vals[12]), # percent
}
except IndexError as e:
print(" Failed to parse result: {0}".format(e))
self.report_error(output)
return None
# 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, args, run_info):
super(server, self).__init__(None)
self.args = args
self.protocol = run_info['protocol']
self.tcp_window = run_info['tcp_window']
self.running = False
self.end = threading.Event()
self.start()
# Run a iperf server in a separate thread
def run(self):
h = self.args.mesh_host
p = str(self.args.mesh_port)
w = str(self.tcp_window)
# Craft the iperf command based on the protocol
if self.protocol == "tcp":
self.cmd = ["iperf", "-s", "-B", h, "-p", p, "-w", w]
elif self.protocol == "udp":
self.cmd = ["iperf", "-s", "-u", "-B", h, "-p", p]
# Start the iperf server in a separate process and wait for it be killed
print(" Starting {0} server".format(self.protocol))
self.p = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.running = True
self.p.wait()
# 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