cowmesh-network-test/cowmesh_pi_iperf_test.py

129 lines
4.0 KiB
Python
Raw Normal View History

2023-04-20 11:00:28 +00:00
"""
2023-04-28 12:04:41 +00:00
similar to cowmesh_iperf_test but instead of running iperf router-to-router,
it runs iperf between computers which are connected to the network
(pi & nuc etc.)
2023-04-20 11:00:28 +00:00
"""
2023-04-27 08:46:42 +00:00
import datetime
2023-04-20 11:00:28 +00:00
import os
import re
import time
import asyncio, asyncssh, sys
import paramiko
import subprocess
import json
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
SECRETS_PATH = os.path.join(PROJECT_PATH, "secrets.json")
with open(SECRETS_PATH, 'r') as f:
SECRETS = json.loads(f.read())
2023-04-28 12:04:41 +00:00
base_node = "janastunuc" # nuc in jaaga
2023-04-20 11:00:28 +00:00
nodes = [
2023-04-28 12:04:41 +00:00
"janastunuc", # nuc in jaaga
"solipi", # pi in guard
"writer", # pi in new-gazebo
2023-04-20 11:00:28 +00:00
]
results = {}
2023-04-28 12:04:41 +00:00
class CowmeshPiIperfTester:
2023-04-20 11:00:28 +00:00
2023-04-28 12:04:41 +00:00
def __init__(self, log=None, debug=False, time=10):
2023-04-27 08:46:42 +00:00
if log:
self.log = log
self.debug = debug
2023-04-28 12:04:41 +00:00
self.time = time
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
async def log(self, msg):
print(msg)
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
async def debug_log(self, msg):
if self.debug:
await self.log(msg)
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
async def test_between_two_nodes(self, node_a, node_b):
await self.log("++ running test from {} to {}".format(node_a, node_b))
2023-04-28 12:04:41 +00:00
user_a = SECRETS["HOST_INFO"][node_a]["user"]
password_a = SECRETS["HOST_INFO"][node_a]["password"]
2023-04-20 11:00:28 +00:00
myconn = paramiko.SSHClient()
myconn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
2023-04-28 12:04:41 +00:00
ip_a = SECRETS["HOST_INFO"][node_a]["ip"]
myconn.connect(ip_a, username=user_a, password=password_a)
2023-04-27 08:46:42 +00:00
2023-04-28 12:04:41 +00:00
ip_b = SECRETS["HOST_INFO"][node_b]["ip"]
2023-04-20 11:00:28 +00:00
2023-04-28 12:04:41 +00:00
remote_cmd = 'iperf -c {ip_b} -p 5001 -t {time}'.format(ip_b=ip_b, time=self.time)
2023-04-20 11:00:28 +00:00
(stdin, stdout, stderr) = myconn.exec_command(remote_cmd)
2023-04-27 08:46:42 +00:00
output = str(stdout.read())
await self.debug_log("output: {}".format(output))
await self.debug_log("errors: {}".format(stderr.read()))
2023-04-20 11:00:28 +00:00
myconn.close()
2023-04-27 08:46:42 +00:00
match = re.search("(\S+) Mbits", output)
if match:
to_return = match.group(1)
else:
to_return = None
return to_return
async def start_iperf_servers(self):
for node in nodes:
print("++ starting iperf server on {}".format(node))
2023-04-28 12:04:41 +00:00
user = SECRETS["HOST_INFO"][node]["user"]
password = SECRETS["HOST_INFO"][node]["password"]
2023-04-27 08:46:42 +00:00
myconn = paramiko.SSHClient()
myconn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
2023-04-28 12:04:41 +00:00
myconn.connect(node, username=user, password=password)
2023-04-27 08:46:42 +00:00
remote_cmd = 'iperf -s &'
(stdin, stdout, stderr) = myconn.exec_command(remote_cmd)
await self.debug_log("{}".format(stdout.read()))
await self.debug_log("{}".format(type(myconn)))
await self.debug_log("Options available to deal with the connections are many like\n{}".format(dir(myconn)))
myconn.close()
async def run_test(self):
await self.start_iperf_servers()
for node_a in nodes:
for node_b in nodes:
if node_a == node_b:
await self.debug_log("skip self")
continue
r = await self.test_between_two_nodes(node_a, node_b)
result_key = "{} -> {}".format(node_a, node_b)
results[result_key] = r
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
async def output_results(self):
results_str = ""
now = datetime.datetime.now()
date = now.date()
time = now.time()
2023-04-28 12:04:41 +00:00
results_str += "**** computer-to-computer iperf results on {date:%m-%d-%Y} at {time:%H:%M}:\n\n".format(date=date, time=time)
2023-04-27 08:46:42 +00:00
for test_name, result in results.items():
results_str += "{}: {} mbps\n".format(test_name, result)
await self.log(results_str)
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
if __name__ == "__main__":
try:
2023-04-28 12:04:41 +00:00
tester = CowmeshPiIperfTester()
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
async def main_fun():
await tester.run_test()
await tester.output_results()
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
asyncio.get_event_loop().run_until_complete(main_fun())
2023-04-20 11:00:28 +00:00
2023-04-27 08:46:42 +00:00
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc))