Python Forum
python read iperf log and parse throughput
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python read iperf log and parse throughput
#1
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
Reply
#2
You could try this
from collections import deque

with open('iperf3.txt', 'r') as f:
    last_line = deque(f, maxlen=1)[-1].strip()
...
Reply
#3
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()
jacklee26 likes this post
Reply
#4
(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

Attached Files

.txt   iper3.txt (Size: 1.18 KB / Downloads: 92)
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 205 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  parse/read from file seperated by dots giovanne 5 1,127 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  [split] Parse Nested JSON String in Python mmm07 4 1,547 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Read directory listing of files and parse out the highest number? cubangt 5 2,375 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  How to parse a live feed in Python? Daring_T 2 4,147 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  Parse a REST API call using Python GKT 1 1,913 May-07-2020, 04:15 AM
Last Post: buran
  print the result of iperf log jacklee26 3 3,031 Feb-23-2020, 04:51 AM
Last Post: Larz60+
  Read csv file, parse data, and store in a dictionary markellefultz20 4 4,601 Nov-26-2019, 03:33 PM
Last Post: DeaD_EyE
  Python Library to parse RAML 1.0 sameekb 1 3,231 Jan-21-2019, 04:55 PM
Last Post: Larz60+
  How to parse the data in python sandy 10 5,250 Jan-15-2019, 05:50 PM
Last Post: sandy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020