做网维的或者企业网管的应该对iperf这个局域网测速软件有所了解,如果你不了解,可以参考天下网吧的一些教程和工具:网吧网维必备的局域网测速方案ipref!这款工具原版是英文的纯DOS界面的,但是该款软件是跨平台的开源软件,所以深受大家,特别是做网吧维护的喜爱,不过天下网吧了解到很多网维希望能加上曲线图来展示速度,这样更直观,虽然天下网吧已经提供了带曲线图的iperf工具,使用也很简单,但是自己用代码来实现的话那不是更秀。如果做网维没有自己的想法,那跟咸鱼有什么区别?所以自己写代码来实现吧。
iperf原生支持一个把测试数据写入到txt日志文件的功能,写入到txt文件里的格式如下:
------------------------------------------------------------ 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根据iperf的日志数据画出曲线图的代码.
import re import matplotlib.pyplot as plt import numpy as np time_list = [] rate_list = [] with open('./txwb1.ini','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.title('txwb-iperf-chart') plt.plot(time_list, rate_list) plt.xlabel('Time(sec)') plt.ylabel('Bandwidth(Mbits/sec)') plt.grid() plt.show()