Cleaned iperf up, added normalization to Mbits

This commit is contained in:
madrigal 2025-09-16 14:25:53 -04:00
parent b423d49a2b
commit 49b6b7672b

View File

@ -129,7 +129,7 @@ def get_current_location(dictionary={}):
# Ping base station # Ping base station
def ping_basestation( def ping_basestation(
address="10.46.0.1", dictionary={} address="10.45.0.1", dictionary={}
): # raspberry pi address 192.168.0.29, host1 is 0.30 ): # raspberry pi address 192.168.0.29, host1 is 0.30
try: try:
result = subprocess.run( result = subprocess.run(
@ -157,13 +157,13 @@ def collect_iperf(
duration=10, duration=10,
is_client=True, is_client=True,
): ):
if not is_client: if is_client:
commands = [["iperf3", "-s"]]
else:
commands = [ commands = [
["iperf3", "-c", server_ip, "-t", str(duration)], ["iperf3", "-c", server_ip, "-t", str(duration)],
["iperf3", "-c", server_ip, "-t", str(duration), "-R"], ["iperf3", "-c", server_ip, "-t", str(duration), "-R"],
] ]
else:
commands = [["iperf3", "-s"]]
for command in commands: for command in commands:
try: try:
@ -191,27 +191,33 @@ def collect_iperf(
except: except:
end_distance = None end_distance = None
# Look for final sender and receiver bitrates (usually in summary lines)
matches = re.findall( matches = re.findall(
r"\[\s*\d+\]\s+\d+\.\d+\-\d+\.\d+\s+sec\s+[\d.]+\s+\w+Bytes\s+([\d.]+)\s+Mbits/sec\s+(\S+)?", r"\[\s*\d+\]\s+\d+\.\d+\-\d+\.\d+\s+sec\s+[\d.]+\s+\w+Bytes\s+([\d.]+)\s+(Kbits/sec|Mbits/sec)",
output, output,
) )
if len(matches) >= 1: def normalize(value: float, unit: str) -> float:
# Take the last throughput entry """Convert all rates to Mbits/sec."""
last_entry = matches[-1] if unit.lower().startswith("kbit"):
sender_bitrate = float(last_entry[0]) return value / 1000.0
return value
# Sometimes iperf omits receiver or sender lines depending on direction if len(matches) >= 1:
# Try to find both separately: # Normalize the last entries
last_value, last_unit = matches[-1]
sender_bitrate = normalize(float(last_value), last_unit)
# Try to differentiate sender vs receiver
receiver_match = re.search(r"receiver", output, re.IGNORECASE) receiver_match = re.search(r"receiver", output, re.IGNORECASE)
sender_match = re.search(r"sender", output, re.IGNORECASE) sender_match = re.search(r"sender", output, re.IGNORECASE)
if receiver_match and sender_match and len(matches) >= 2: if receiver_match and sender_match and len(matches) >= 2:
receiver_bitrate = float(matches[-1][0]) recv_value, recv_unit = matches[-1]
sender_bitrate = float(matches[-2][0]) send_value, send_unit = matches[-2]
receiver_bitrate = normalize(float(recv_value), recv_unit)
sender_bitrate = normalize(float(send_value), send_unit)
else: else:
receiver_bitrate = sender_bitrate # fallback: assume same receiver_bitrate = sender_bitrate
else: else:
bitrates = re.findall(r"(\d+\.\d+) Mbits/sec", output) bitrates = re.findall(r"(\d+\.\d+) Mbits/sec", output)
@ -227,10 +233,6 @@ def collect_iperf(
"iperf_full": output, "iperf_full": output,
"sender_bitrate": sender_bitrate, "sender_bitrate": sender_bitrate,
"receiver_bitrate": receiver_bitrate, "receiver_bitrate": receiver_bitrate,
"note": (
"avgs are calculated with the assumption "
"that all value are in Mbits/sec"
),
"start_distance": start_distance, "start_distance": start_distance,
"end_distance": end_distance, "end_distance": end_distance,
"type": test_type, "type": test_type,