使用python提取iperf数据并绘制吞吐率曲线

iperf跑出来的数据无法方便的绘制曲线,因此使用python中的正则表达式提取出数据并使用matplotlib进行绘图。

首先需要将iperf输出的日志存进一个文件里,文件示例:

------------------------------------------------------------
Server listening on TCP port 9009
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
[  6] local 10.0.0.10 port 9009 connected with 10.0.0.11 port 39328
[ ID] Interval       Transfer     Bandwidth
[  6]  0.0- 1.0 sec  10.2 MBytes  85.3 Mbits/sec
[  6]  1.0- 2.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6]  2.0- 3.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6]  3.0- 4.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6]  4.0- 5.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6]  5.0- 6.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6]  6.0- 7.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6]  7.0- 8.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6]  8.0- 9.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6]  9.0-10.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6] 10.0-11.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6] 11.0-12.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6] 12.0-13.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6] 13.0-14.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6] 14.0-15.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6] 15.0-16.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6] 16.0-17.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6] 17.0-18.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6] 18.0-19.0 sec  11.4 MBytes  95.6 Mbits/sec
[  6] 19.0-20.0 sec  11.4 MBytes  95.7 Mbits/sec
[  6]  0.0-20.6 sec   234 MBytes  95.1 Mbits/sec

然后运行下面的python程序进行数据提取和绘图

import re
import matplotlib.pyplot as plt
import numpy as np

time_list = []
rate_list = []

with open('./iperf-log.txt','r') as f:# iperf-log.txt为iperf日志文件名
    row_data = f.readlines() # 读取iperf日志文件的每一行至一个list中
    for line in row_data:    # 利用正则表达式进行匹配,可根据实际情况更改匹配内容
        time = re.findall(r"-(.*) sec", line)
        rate = re.findall(r"MBytes  (.*) Mbits", line)
        if(len(time)>0):     # 当前行中有吞吐和时间数据时对数据进行存储
            print(time)
            time_list.append(float(time[0]))
            rate_list.append(float(rate[0]))

plt.figure()
plt.plot(time_list, rate_list)
plt.xlabel('Time(sec)')
plt.ylabel('Bandwidth(Mbits/sec)')
plt.grid()
plt.show()

绘图结果
使用python提取iperf数据并绘制吞吐率曲线_第1张图片
有个小瑕疵是日志文件的最后一行会输出总的吞吐率,因此会出现像上图中一样最后出现一个干扰点的情况,实际使用时将最后一行手动删去即可。

你可能感兴趣的:(网络)