-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_test.2.2.bak.py
More file actions
199 lines (168 loc) · 7.34 KB
/
Copy pathperformance_test.2.2.bak.py
File metadata and controls
199 lines (168 loc) · 7.34 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
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# =====================================================
# Author : Marion Renaldo Rotensulu
# Version : v2.2
# Description : Custom Performance test with OS detection, user-level logging, UDP BW default, RTT parsing
# Log File : /var/log/iperf_tests/iperf_summary.log (or ~/iperf_logs if not root)
# Last Updated : 2025-06-13
# Changelog :
# - v1.0 Basic summary output
# - v1.1 Status check & debug output
# - v1.2 Syslog-style one-line logging
# - v1.3 Live terminal output + debug mode
# - v1.4 Added raw output capture in /tmp before parsing
# - v1.5 Fixed regex parsing for UDP/TCP final summary lines (receiver block)
# - v1.6 Fixed UDP regex from correct receiver line
# - v1.7 Regex fix to correctly parse integer/float UDP bandwidth
# - v2.0 Logging fallback to user dir if not root, macOS RTT parsing added
# - v2.1 OS auto-detection + required fallback, reverse parsing for download mode
# - v2.2 Default UDP bandwidth set to 1000M if not defined
# =====================================================
import subprocess
import datetime
import os
import argparse
import re
import socket
import platform
import sys
# --- Argument Parser ---
parser = argparse.ArgumentParser(description="Network Performance Test with iperf3 and ping.")
parser.add_argument("--duration", type=int, choices=[60, 300, 600], default=60,
help="Test duration in seconds")
parser.add_argument("--server", type=str, required=True, help="IP address of iperf3 server")
parser.add_argument("--port", type=int, default=5201, help="iperf3 server port (default: 5201)")
parser.add_argument("--debug", action="store_true", help="Enable debug output")
parser.add_argument("--udp-bandwidth", type=str, default="1000M", help="UDP test bandwidth (default: 1000M)")
parser.add_argument("--direction", type=str, choices=["upload", "download"], default="upload",
help="Test direction (default: upload)")
parser.add_argument("--os-mode", type=str, choices=["Linux", "MacOS"], required=False,
help="Override OS detection")
args = parser.parse_args()
# --- Auto-detect OS ---
auto_os = platform.system()
if auto_os == "Darwin":
detected_os = "MacOS"
elif auto_os == "Linux":
detected_os = "Linux"
else:
detected_os = None
if args.os_mode:
os_mode = args.os_mode
elif detected_os:
os_mode = detected_os
print(f"[INFO] OS auto-detected as {os_mode}")
else:
print("[ERROR] Could not detect OS. Please provide --os-mode MacOS|Linux.")
sys.exit(1)
# --- Define Paths ---
duration = args.duration
server_ip = args.server
port = args.port
debug_mode = args.debug
direction = args.direction
udp_bw = args.udp_bandwidth or "1000M" # fallback if empty string
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
user_home = os.path.expanduser("~")
is_root = (os.geteuid() == 0)
log_dir = "/var/log/iperf_tests" if is_root else os.path.join(user_home, "iperf_logs")
tmp_dir = "/tmp"
os.makedirs(log_dir, exist_ok=True)
summary_log_file = os.path.join(log_dir, "iperf_summary.log")
ping_tmp = os.path.join(tmp_dir, f"ping_{timestamp}.log")
udp_tmp = os.path.join(tmp_dir, f"iperf3_udp_{timestamp}.log")
tcp_tmp = os.path.join(tmp_dir, f"iperf3_tcp_{timestamp}.log")
# --- Helper Functions ---
def run_and_save(cmd, tmpfile):
try:
with open(tmpfile, "w") as f:
subprocess.run(cmd, stdout=f, stderr=subprocess.STDOUT, text=True, timeout=duration + 30)
with open(tmpfile, "r") as f:
return f.read()
except Exception as e:
return f"ERROR: {e}"
def check_mtu(ip):
mtu_test = subprocess.run(["ping", "-c", "1", "-s", "1472", "-M", "do", ip], capture_output=True, text=True)
if "frag needed" in mtu_test.stderr or "Message too long" in mtu_test.stderr:
print("[WARNING] MTU issue detected. Consider adjusting MTU if UDP loss is high.")
# --- Reachability Check ---
ping_status = False
try:
socket.gethostbyname(server_ip)
ping_check = subprocess.run(["ping", "-c", "3", server_ip], capture_output=True, text=True)
if "0% packet loss" in ping_check.stdout or "1% packet loss" in ping_check.stdout:
ping_status = True
except Exception:
ping_status = False
# --- Initialize Results ---
status = "FAIL"
latency_avg = "-"
packet_loss = "-"
udp_bw_result = udp_jitter = udp_ploss = "-"
tcp_bw_result = "-"
if ping_status:
status = "OK"
check_mtu(server_ip)
print("[INFO] Running latency test (ping)...")
ping_output = run_and_save(["ping", "-c", str(duration // 2), server_ip], ping_tmp)
if os_mode == "Linux":
match = re.search(r"rtt min/avg/max/mdev = [\d\.]+/([\d\.]+)/[\d\.]+/[\d\.]+", ping_output)
elif os_mode == "MacOS":
match = re.search(r"(?:rtt|round-trip).* = [\d\.]+/([\d\.]+)/[\d\.]+/[\d\.]+", ping_output)
else:
match = None
if match:
latency_avg = match.group(1) + "ms"
ploss_match = re.search(r"(\d+)% packet loss", ping_output)
if ploss_match:
packet_loss = ploss_match.group(1) + "%"
print("[INFO] Running UDP test (jitter, loss, bandwidth)...")
udp_args = ["iperf3", "-c", server_ip, "-p", str(port), "-u", "-t", str(duration), "-b", udp_bw]
if direction == "download":
udp_args.append("-R")
udp_output = run_and_save(udp_args, udp_tmp)
udp_line = ""
for line in udp_output.splitlines():
if direction == "download" and "sender" in line and "bits/sec" in line:
udp_line = line.strip()
elif direction == "upload" and "receiver" in line and "bits/sec" in line:
udp_line = line.strip()
if udp_line:
udp_bw_match = re.search(r"(\d+(?:\.\d+)? \wbits/sec)", udp_line)
if udp_bw_match:
udp_bw_result = udp_bw_match.group(1)
udp_jitter_match = re.search(r"(\d+\.\d+)\s+ms\s+\d+/", udp_line)
if udp_jitter_match:
udp_jitter = udp_jitter_match.group(1) + "ms"
udp_loss_match = re.search(r"\(([\d\.]+)%\)", udp_line)
if udp_loss_match:
udp_ploss = udp_loss_match.group(1) + "%"
print("[INFO] Running TCP test (bandwidth)...")
tcp_args = ["iperf3", "-c", server_ip, "-p", str(port), "-t", str(duration)]
if direction == "download":
tcp_args.append("-R")
tcp_output = run_and_save(tcp_args, tcp_tmp)
tcp_line = ""
for line in tcp_output.splitlines():
if "receiver" in line and "bits/sec" in line:
tcp_line = line.strip()
if tcp_line:
tcp_bw_match = re.search(r"(\d+(?:\.\d+)? \wbits/sec)", tcp_line)
if tcp_bw_match:
tcp_bw_result = tcp_bw_match.group(1)
if debug_mode:
print("\n[DEBUG] Ping Output:\n", ping_output)
print("\n[DEBUG] UDP Output:\n", udp_output)
print("\n[DEBUG] UDP Parsed Line:\n", udp_line)
print("\n[DEBUG] TCP Output:\n", tcp_output)
print("\n[DEBUG] TCP Parsed Line:\n", tcp_line)
else:
print(f"[ERROR] Server {server_ip} unreachable. Test aborted.")
# --- Final Summary ---
summary_line = (f"{timestamp} STATUS={status} SERVER={server_ip}:{port} DURATION={duration}s "
f"LATENCY={latency_avg} PING_LOSS={packet_loss} "
f"UDP_BW={udp_bw_result} UDP_JITTER={udp_jitter} UDP_LOSS={udp_ploss} "
f"TCP_BW={tcp_bw_result}")
print("\n[RESULT] " + summary_line)
with open(summary_log_file, "a") as f:
f.write(summary_line + "\n")