![]() |
python read iperf log and parse throughput - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: python read iperf log and parse throughput (/thread-38044.html) |
python read iperf log and parse throughput - jacklee26 - Aug-26-2022 HI i have a file which is iperf3's log file, I wish to get the Throughput value from the file. I have written python code and would like to ask for any improvement on the code. MY code might not be good, I literally know it. Any better way to accomplish it? basically, I want is read the iperf lof file, and get the throughput value, such as 3.39 Gbits, and get the 3.39 value and print our and write into a new file. #!/usr/bin/python result="" last_line="" lastResult_line="" TPT="" with open('iperf3.txt', 'r') as f: last_line = f.readlines() #lastResult_line="".join(last_line[-3:]) #lastResult_line=lastResult_line.strip() last_line=last_line[-1].strip() print("="*30) #print(lastResult_line) #print(last_line) TPT= last_line.split(' ') TPT=TPT[0].split(' ')[-2] value= int(round(float(TPT))) print(f"Orginal Throuhgput value: {TPT} Gbps") print(f"after Round Value: {value}.0 Gbps") if value< 100: print("Iperf result: pass") result="Iperf Result: pass" else: print("Iperf result: Fail") result="Iperf Result: Fail" with open("testresult.txt", "a+") as f: # here, position is already at the end f.write(f"####Test4:{result}, Throughput:{value}.0 Mbps ####\n") f.write(f"{'='*20}Iperf3 Result{'='*20}\n") f.write(f"{last_line}\n") #f.write(f"{lastResult_line}\n") f.write(f"{'='*50}\n") f.write(f"Iperf Throughput:{TPT}GBytes,rounded=>{value}.0 Gbps. Result PASS\n")iperf3.txt: ----------------------------------------------------------- Server listening on 5201 ----------------------------------------------------------- Accepted connection from 127.0.0.1, port 39756 [ 5] local 127.0.0.1 port 5201 connected to 127.0.0.1 port 39758 [ ID] Interval Transfer Bitrate [ 5] 0.00-1.00 sec 389 MBytes 3.26 Gbits/sec [ 5] 1.00-2.00 sec 401 MBytes 3.37 Gbits/sec [ 5] 2.00-3.00 sec 405 MBytes 3.40 Gbits/sec [ 5] 3.00-4.00 sec 372 MBytes 3.12 Gbits/sec [ 5] 4.00-5.00 sec 419 MBytes 3.51 Gbits/sec [ 5] 5.00-6.00 sec 401 MBytes 3.36 Gbits/sec [ 5] 6.00-7.00 sec 422 MBytes 3.54 Gbits/sec [ 5] 7.00-8.00 sec 411 MBytes 3.45 Gbits/sec [ 5] 8.00-9.00 sec 412 MBytes 3.45 Gbits/sec [ 5] 9.00-10.00 sec 410 MBytes 3.44 Gbits/sec [ 5] 10.00-10.01 sec 896 KBytes 1.47 Gbits/sec - - - - - - - - - - - - - - - - - - - - - - - - - [ ID] Interval Transfer Bitrate [ 5] 0.00-10.01 sec 3.95 GBytes 3.39 Gbits/sec receiver RE: python read iperf log and parse throughput - Gribouillis - Aug-26-2022 You could try this from collections import deque with open('iperf3.txt', 'r') as f: last_line = deque(f, maxlen=1)[-1].strip() ... RE: python read iperf log and parse throughput - Yoriz - Aug-26-2022 Note: no error checking added def file_last_line(file_path): line = "" with open(file_path, "r") as file: for line in file: pass return line.strip() def throughput_text(throughput_line): throughput = throughput_line.split(" ")[-3] rounded_throughput = round(float(throughput)) state = "pass" if rounded_throughput < 100 else "Fail" return ( f"####Test4:Iperf Result: {state}, Throughput:{rounded_throughput}.0 Mbps ####\n" f"{'='*20}Iperf3 Result{'='*20}\n" f"{throughput_line}\n" f"{'='*50}\n" f"Iperf Throughput:{throughput}GBytes,rounded=>{rounded_throughput}.0 Gbps. Result PASS\n" ) def main(): last_line = file_last_line(iperf3.txt") text = throughput_text(last_line) with open(testresult.txt", "a+") as file: file.write(text) if __name__ == "__main__": main() RE: python read iperf log and parse throughput - jacklee26 - Aug-27-2022 (Aug-26-2022, 03:43 PM)Yoriz Wrote: Note: no error checking added hi what if my iperf3.txt is like this: please refer attachment [ 5] local 127.0.0.1 port 39598 connected to 127.0.0.1 port 5201 [ ID] Interval Transfer Bitrate Retr Cwnd [ 5] 0.00-1.00 sec 541 MBytes 4.53 Gbits/sec 0 3.12 MBytes [ 5] 1.00-2.00 sec 631 MBytes 5.31 Gbits/sec 0 3.12 MBytes [ 5] 2.00-3.00 sec 578 MBytes 4.83 Gbits/sec 0 3.12 MBytes [ 5] 3.00-4.00 sec 561 MBytes 4.71 Gbits/sec 0 3.12 MBytes [ 5] 4.00-5.00 sec 512 MBytes 4.30 Gbits/sec 0 3.12 MBytes [ 5] 5.00-6.00 sec 571 MBytes 4.80 Gbits/sec 0 3.12 MBytes [ 5] 6.00-7.00 sec 581 MBytes 4.88 Gbits/sec 0 3.12 MBytes [ 5] 7.00-8.00 sec 639 MBytes 5.36 Gbits/sec 0 3.12 MBytes [ 5] 8.00-9.00 sec 651 MBytes 5.45 Gbits/sec 0 3.12 MBytes [ 5] 9.00-10.00 sec 620 MBytes 5.21 Gbits/sec 0 3.12 MBytes - - - - - - - - - - - - - - - - - - - - - - - - - [ ID] Interval Transfer Bitrate Retr [ 5] 0.00-10.00 sec 5.75 GBytes 4.94 Gbits/sec 0 sender [ 5] 0.00-10.00 sec 5.75 GBytes 4.94 Gbits/sec receiver RE: python read iperf log and parse throughput - Yoriz - Aug-27-2022 If the input files vary, you need to find out what all the variations are before you can figure out if a solution to extract the correct value can be found. |