iperf是一个基于Client/Server的网络性能测试工具,可以测试TCP、UDP和SCTP带宽质量,能够提供网络吞吐率信息,以及震动、丢包率,最大段和最大传输单元大小等统计信息,帮助我们测试网络性能,定位网络瓶颈。其中抖动和丢包率适应于UDP测试,二带宽测试适应于TCP和UDP。
iperf的好处是纯粹发包和接收,防止硬盘拖后腿,影响测试结果。对于时延,iperf无能无力。但是iperf能够计算抖动。在iperf测试中需要发送大量的包,因此计算出来的抖动值,就是连续发送时延差值的平均值。
iperf是一个开源软件,其源码在GitHub上,地址:https://github.com/esnet/iperf。
1 下载
下载最新版本源码包,下载地址:http://downloads.es.net/pub/iperf/。本文以 iperf-3.0.3.tar.gz 版本为例。
2 解压
$ mkdir ~/iperf
$ cp iperf-3.0.3.tar.gz ~/iperf
$ cd ~/iperf
$ tar xzf iperf-3.0.3.tar.gz
$ cd iperf-3.0.3/
命令中的“~”表示当前用户目录,比如在Linux主机上面工作的用户是:veryarm,那么命令中的“~”就代表路径:/home/veryarm/目录。
3 配置
$ mkdir ../install
$ export PATH=$PATH:/usr/local/arm-2010q1/bin #交叉编译工具路径加入系统环境变量!
$ ./configure --host=arm-none-linux-gnueabi --prefix=/home/veryarm/iperf/install
其中,
- –host: 指定交叉编译工具,一般为arm-none-linux-gnueabi、arm-linux-gnueabihf、arm-linux等,具体要和目标板用的交叉编译工具对应。
- –prefix: 指定安装目录,编译后的文件会全部放在安装目录中。必须是绝对路径!
4 编译
$ make # 编译,如果是多核CPU,如四核,使用make -j4可提高编译速度
$ make install # 安装
完成后,文件会自动安装到上面指定的 install 目录。
5 去除调试信息并压缩
使用交叉编译工具 strip 去除镜像的调试信息,在不影响使程序使用的情况下,程序大小一般可以减小50%左右。本程序我们仅用到其中2个文件,bin下的iperf3 和 lib下的libiperf.so.0.0.0。
$ cd ../install # 进入安装目录
$ arm-none-linux-gnueabi-strip bin/iperf3 # 去除iperf3镜像调试信息
$ arm-none-linux-gnueabi-strip lib/libiperf.so.0.0.0 # 去除共享库的调试信息
$ rm lib/libiperf.a lib/libiperf.la # 删除两个不用的库文件
$ tar jcf iperf-3.0.3.tar.bz2 bin lib # 压缩bin和lib目录
6 复制
将打包后的 iperf-3.0.3.tar.bz2 压缩包通过U盘、FTP或者NFS等方式拷贝到目标板,并分别解压到/bin和/sbin目录。
$ cp iperf-3.0.3.tar.bz2 ~ # 将复制压缩包到目标板用户目录
$ tar jxf iperf-3.0.3.tar.bz2 bin -C /usr # 解压镜像到/usr/bin
$ tar jxf iperf-3.0.3.tar.bz2 lib -C /usr # 解压库文件到/usr/lib
7 使用
iperf有iperf2版本(命令iperf)和iperf3版本(命令iperf3),现在很多Linux系统和目标板系统默认有iperf2,而这两个版本又不能混着用,也就是说,如果服务器运行iperf2,而目标板上运行 iperf3 是无法测试的,会提示“connect failed: Connection refused”错误。
有鉴于此,本文重点以新版的 iperf3 为例进行测试。
(1)电脑安装iperf
复制第1步下载的源码到电脑上,在Ubuntu(或其他机器)上,使用以下命令安装 iperf3 到电脑上:
$ cd iperf-3.0.3/
$ ./configure
$ make
$ sudo make install
iperf3程序会自动安装到/usr/local/bin目录下,其共享库则安装到/usr/local/lib/目录下。
如果运行 iperf 命令时提示“iperf3: error while loading shared libraries: libiperf.so.0: cannot open shared object file: No such file or directory”错误,制作一个软连接可解决,命令如下:
sudo ln -s /usr/local/lib/libiperf.so.0.0.0 /usr/lib/iperf.so.0
(2)启动服务器iperf
在Ubuntu上,以服务端方式启动:
$ iperf3 -s -i 1
参数说明:
- -s:以服务器方式启动
- -i:以秒为单位显示报告间隔
- -w:指定TCP窗口大小,默认是8KB
(3)启动客户端
在目标板上,以客户端方式连接到服务端,测试10秒钟。
$ iperf3 -c 192.168.1.100 -i 1 -w 448k -t 10
参数说明:
- -c:以client模式启动,随后跟着服务器的IP地址
- -t:测试时间,默认10秒
执行后,服务器端显示:
Accepted connection from 192.168.1.250, port 55010
[ 5] local 192.168.1.100 port 5201 connected to 192.168.1.250 port 55011
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-1.07 sec 2.12 MBytes 16.7 Mbits/sec
[ 5] 1.07-2.06 sec 2.38 MBytes 20.2 Mbits/sec
[ 5] 2.06-3.10 sec 2.38 MBytes 19.1 Mbits/sec
[ 5] 3.10-4.08 sec 2.38 MBytes 20.4 Mbits/sec
[ 5] 4.08-5.07 sec 2.62 MBytes 22.2 Mbits/sec
[ 5] 5.07-6.00 sec 2.38 MBytes 21.4 Mbits/sec
[ 5] 6.00-7.06 sec 2.75 MBytes 21.7 Mbits/sec
[ 5] 7.06-8.10 sec 2.50 MBytes 20.2 Mbits/sec
[ 5] 8.10-9.10 sec 2.50 MBytes 21.0 Mbits/sec
[ 5] 9.10-10.06 sec 2.50 MBytes 21.9 Mbits/sec
[ 5] 10.06-10.10 sec 128 KBytes 26.0 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Retr
[ 5] 0.00-10.10 sec 24.6 MBytes 20.5 Mbits/sec 7 sender
[ 5] 0.00-10.10 sec 24.6 MBytes 20.5 Mbits/sec receive
客户端显示:
Connecting to host 192.168.1.100, port 5201
[ 4] local 192.168.1.250 port 55011 connected to 192.168.1.100 port 5201
[ ID] Interval Transfer Bandwidth Retr Cwnd
[ 4] 0.00-1.03 sec 2.25 MBytes 18.4 Mbits/sec 4 96.2 KBytes
[ 4] 1.03-2.02 sec 2.38 MBytes 20.0 Mbits/sec 0 112 KBytes
[ 4] 2.02-3.06 sec 2.38 MBytes 19.2 Mbits/sec 1 90.5 KBytes
[ 4] 3.06-4.02 sec 2.38 MBytes 20.7 Mbits/sec 0 96.2 KBytes
[ 4] 4.02-5.02 sec 2.62 MBytes 22.1 Mbits/sec 0 106 KBytes
[ 4] 5.02-6.03 sec 2.62 MBytes 21.9 Mbits/sec 1 76.4 KBytes
[ 4] 6.03-7.02 sec 2.50 MBytes 21.0 Mbits/sec 0 100 KBytes
[ 4] 7.02-8.00 sec 2.38 MBytes 20.4 Mbits/sec 0 112 KBytes
[ 4] 8.00-9.06 sec 2.62 MBytes 20.8 Mbits/sec 1 89.1 KBytes
[ 4] 9.06-10.03 sec 2.50 MBytes 21.7 Mbits/sec 0 105 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Retr
[ 4] 0.00-10.03 sec 24.6 MBytes 20.6 Mbits/sec 7 sender
[ 4] 0.00-10.03 sec 24.6 MBytes 20.6 Mbits/sec receiver
iperf Done.
以上信息显示,当前网卡传输速度约为 20Mbit/s,下载速度 2.5 Mbit/s。
8 iperf参数
除了以上参数,我们还可以在iperf中使用下面的参数。
(1)通用参数
- -f:后接[k|m|K|M],分别表示以Kbits,Mbits,Kbytes,Mbytes显示报告,默认以Mbits以单位,例如,
$ iperf3 –c 192.168.22.100 –f K
- -o:将报告和错误信息输出到文件,例如,
iperf3 –c 192.168.1.100 -o iperf.log
- -p:指定服务器端使用的端口或客户端的端口。
$ iperf3 –c 192.168.22.66 –p 1234
$ iperf3 –s –p 1234
- -u:指定使用udp协议
- -M:指定TCP数据包的最大mtu值。
- -N:设定TCP不延迟
(2)服务器端
- -D:以服务方式运行iperf,例如,
$ iperf3 -s –D
- -R:停止iperf服务,针对-D
(3)客户端
- -d:同时进行双向传输测试
- -n:指定传输的字节数。
$ iperf3 -c 192.168.21.66 -n 100000
- -r:单独进行双向传输测试
- -F:指定需要传输的文件
- -T:指定ttl值