0. iperf原理解析
iperf工具可以用来测量TCP或者是UDP的网络吞吐量,即bandwidth(带宽)测试。
iPerf的主要目标是帮助调整特定路径上的TCP连接。 众所周知,TCP最基本的调整问题是调整TCP窗口大小,它控制在任何一点在网络中可以有多少数据。如果它太小,发送者将会在一段时间内处于空闲状态,从而影响发送TCP的性能。TCP窗口大小的理论值是:瓶颈带宽与往返延时的乘积,即:
bottleneck bandwidth * round trip time
例如瓶颈链路是45 Mbit/sec,使用ping命令测量到的往返时延是42ms。那么TCP窗口的理论值是
45 Mbit/sec * 42 ms = (45e6) * (42e-3) = 1890000 bits= 230 KByte
在实际测试中,可以以计算得到的TCP窗口为基准,在这个值(如上面为230KByte)的基础上,升高或者降低TCP窗口大小,可以得到一个性能的提升。
带宽测试一般来说采用UDP模式测试,因为在UDP模式下能测出极限带宽、路径时延、丢包率,这些测试项会在带宽测试报告中打印出来。在进行测试时,先以链路理论带宽作为数据发送速率进行测试,例如,从客户端到服务器之间的链路的理论带宽为1000Mbps,先用 -b 1000M进行测试,然后根据测试结果(包括实际带宽,时延抖动和丢包率),再以实际带宽作为数据发送速率进行测试,会发现时延抖动和丢包率比第一次好很多,重复测试几次,就能得出稳定的实际带宽。
iperf是基于server-client模式工作的,因此,要使用iperf测试带宽,需要建立一个服务端(用于丢弃流量)和一个客户端(用于产生流量)。iperf服务端或者是客户端,都是使用的同一个命令,不过是启动命令的选项不同而已。
1. iperf服务启动方式
先看下服务端和客户端的启动方式:
服务端
iperf -s
客户端
iperf -c
注意:iperf默认测试的是TCP协议的带宽,如果需要测试UDP的带宽,则需要加上-u选项
2. iperf常用选项
iperf选项分为 通用选项、服务端特定选项以及客户端特定选项
通用选项
-f 报告输出格式。 [kmKM] format to report: Kbits, Mbits, KBytes, MBytes
-i 在周期性报告带宽之间暂停n秒。如周期是10s,则-i指定为2,则每隔2秒报告一次带宽测试情况,则共计报告5次
-p 设置服务端监听的端口,默认是5001
-u 使用UDP协议测试
-w n<K/M> 指定TCP窗口大小
-m 输出MTU大小
-M 设置MTU大小
-o 结果输出至文件
服务端选项
-s iperf服务器模式
-d 以后台模式运行服务端
-U 运行一个单一线程的UDP模式
客户端选项
-b , –bandwidth n[KM] 指定客户端通过UDP协议发送数据的带宽(bit/s)。默认是1Mbit/s
-c 以客户端模式运行iperf,并且连接至服务端主机ServerIP。 eg: iperf -c
-d 双向测试
-t 指定iperf带宽测试时间,默认是10s。 eg: iperf -c -t 20
-P 指定客户端并发线程数,默认只运行一个线程。 eg,指定3个线程 : iperf -c -P 3
-T 指定TTL值
3.实例
master 为服务端
minion 为客户端
注意:必须先运行iperf server
2秒返回一次带宽测试报告
[root@master ~]# iperf -s -i 2
————————————————————
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
————————————————————
[ 4] local 192.168.159.135 port 5001 connected with 192.168.159.136 port 57065
[ ID] Interval Transfer Bandwidth
[ 4] 0.0- 2.0 sec 907 MBytes 3.80 Gbits/sec
[ 4] 2.0- 4.0 sec 870 MBytes 3.65 Gbits/sec
[ 4] 4.0- 6.0 sec 864 MBytes 3.62 Gbits/sec
[ 4] 6.0- 8.0 sec 949 MBytes 3.98 Gbits/sec
[ 4] 8.0-10.0 sec 813 MBytes 3.41 Gbits/sec
[ 4] 0.0-10.0 sec 4.31 GBytes 3.69 Gbits/sec
[root@minion ~]# iperf -c 192.168.159.135 -i 2
————————————————————
Client connecting to 192.168.159.135, TCP port 5001
TCP window size: 19.3 KByte (default)
————————————————————
[ 3] local 192.168.159.136 port 57065 connected with 192.168.159.135 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0- 2.0 sec 912 MBytes 3.82 Gbits/sec
[ 3] 2.0- 4.0 sec 872 MBytes 3.66 Gbits/sec
[ 3] 4.0- 6.0 sec 863 MBytes 3.62 Gbits/sec
[ 3] 6.0- 8.0 sec 949 MBytes 3.98 Gbits/sec
[ 3] 8.0-10.0 sec 814 MBytes 3.41 Gbits/sec
[ 3] 0.0-10.0 sec 4.31 GBytes 3.70 Gbits/sec
上面两个测试很简单,但需要注意的是,以上测试都是客户端到服务端的上行带宽测试。如果还需要进行下行测试,我们可以直接使用双向测试,测试如下:
双向测试
[root@master ~]# iperf -s
————————————————————
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
————————————————————
[ 4] local 192.168.159.135 port 5001 connected with 192.168.159.136 port 56617
————————————————————
Client connecting to 192.168.159.136, TCP port 5001
TCP window size: 19.3 KByte (default)
————————————————————
[ 5] local 192.168.159.135 port 48055 connected with 192.168.159.136 port 5001
[ ID] Interval Transfer Bandwidth
[ 4] 0.0- 2.0 sec 528 MBytes 2.20 Gbits/sec
[ 5] 0.0- 2.0 sec 425 MBytes 1.78 Gbits/sec
[root@minion ~]# iperf -c 192.168.159.135 -t 2 -d
————————————————————
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
————————————————————
————————————————————
Client connecting to 192.168.159.135, TCP port 5001
TCP window size: 174 KByte (default)
————————————————————
[ 5] local 192.168.159.136 port 56617 connected with 192.168.159.135 port 5001
[ 4] local 192.168.159.136 port 5001 connected with 192.168.159.135 port 48055
[ ID] Interval Transfer Bandwidth
[ 5] 0.0- 2.0 sec 528 MBytes 2.21 Gbits/sec
[ 4] 0.0- 2.0 sec 425 MBytes 1.77 Gbits/sec
注意看下面两行:
[ 5] local 192.168.159.136 port 56617 connected with 192.168.159.135 port 5001
[ 4] local 192.168.159.136 port 5001 connected with 192.168.159.135 port 48055
[5]标志开头的是上行测试,因为server端端口才是5001。相应的,[4]标志开头的是下行测试。后面的带宽测试打印的时候,都是以[5]、[4]标记来区分是上行还是下行的。
UDP测试
该测试中理论带宽10000Mbits/s(内网,万兆交换机,万兆网卡)。因此测试时我先按照理论带宽10000Mbits/sec来测试,然后根据测试报告,反复测试,得到极限带宽为815 Mbits/sec 。
测试报告如下:
[root@master ~]# iperf -s -u
————————————————————
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size: 122 KByte (default)
————————————————————
[ 3] local 10.65.14.43 port 5001 connected with 10.65.14.42 port 5586
[ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
[ 3] 0.0-60.0 sec 4.11 GBytes 588 Mbits/sec 0.000 ms 1158071/4158943 (28%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order
[ 4] local 10.65.14.43 port 5001 connected with 10.65.14.42 port 39851
[ 4] 0.0-60.0 sec 5.33 GBytes 763 Mbits/sec 0.014 ms 264719/4158695 (6.4%)
[ 4] 0.0-60.0 sec 1 datagrams received out-of-order
[ 3] local 10.65.14.43 port 5001 connected with 10.65.14.42 port 56885
[ 3] 0.0-60.0 sec 5.48 GBytes 784 Mbits/sec 0.017 ms 49/3999977 (0.0012%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order
[ 4] local 10.65.14.43 port 5001 connected with 10.65.14.42 port 30343
[ 4] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec 0.013 ms 727/4157413 (0.017%)
[ 4] 0.0-60.0 sec 1 datagrams received out-of-order
[ 3] local 10.65.14.43 port 5001 connected with 10.65.14.42 port 17417
[ 3] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec 0.013 ms 2061/4158792 (0.05%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order
[root@minion ~]# iperf -c 10.65.14.43 -u -m -t 60 -i 10 -b 10000M
————————————————————
Client connecting to 10.65.14.43, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 122 KByte (default)
————————————————————
[ 3] local 10.65.14.42 port 5586 connected with 10.65.14.43 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 972 MBytes 815 Mbits/sec
[ 3] 10.0-20.0 sec 972 MBytes 815 Mbits/sec
[ 3] 20.0-30.0 sec 972 MBytes 815 Mbits/sec
[ 3] 30.0-40.0 sec 972 MBytes 815 Mbits/sec
[ 3] 40.0-50.0 sec 972 MBytes 815 Mbits/sec
[ 3] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec
[ 3] Sent 4158944 datagrams
[ 3] Server Report:
[ 3] 0.0-60.0 sec 4.11 GBytes 588 Mbits/sec 0.000 ms 1158071/4158943 (28%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order
[root@minion ~]# iperf -c 10.65.14.43 -u -m -t 60 -i 10 -b 1000M
————————————————————
Client connecting to 10.65.14.43, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 122 KByte (default)
————————————————————
[ 3] local 10.65.14.42 port 39851 connected with 10.65.14.43 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 972 MBytes 815 Mbits/sec
[ 3] 10.0-20.0 sec 972 MBytes 815 Mbits/sec
[ 3] 20.0-30.0 sec 972 MBytes 815 Mbits/sec
[ 3] 30.0-40.0 sec 972 MBytes 815 Mbits/sec
[ 3] 40.0-50.0 sec 972 MBytes 815 Mbits/sec
[ 3] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec
[ 3] Sent 4158696 datagrams
[ 3] Server Report:
[ 3] 0.0-60.0 sec 5.33 GBytes 763 Mbits/sec 0.013 ms 264719/4158695 (6.4%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order
[root@minion ~]# iperf -c 10.65.14.43 -u -m -t 60 -i 10 -b 763M
————————————————————
Client connecting to 10.65.14.43, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 122 KByte (default)
————————————————————
[ 3] local 10.65.14.42 port 56885 connected with 10.65.14.43 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 935 MBytes 784 Mbits/sec
[ 3] 10.0-20.0 sec 935 MBytes 784 Mbits/sec
[ 3] 20.0-30.0 sec 935 MBytes 784 Mbits/sec
[ 3] 30.0-40.0 sec 935 MBytes 784 Mbits/sec
[ 3] 40.0-50.0 sec 935 MBytes 784 Mbits/sec
[ 3] 50.0-60.0 sec 935 MBytes 784 Mbits/sec
[ 3] 0.0-60.0 sec 5.48 GBytes 784 Mbits/sec
[ 3] Sent 3999978 datagrams
[ 3] Server Report:
[ 3] 0.0-60.0 sec 5.48 GBytes 784 Mbits/sec 0.017 ms 49/3999977 (0.0012%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order
[root@minion ~]# iperf -c 10.65.14.43 -u -m -t 60 -i 10 -b 784M
————————————————————
Client connecting to 10.65.14.43, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 122 KByte (default)
————————————————————
[ 3] local 10.65.14.42 port 30343 connected with 10.65.14.43 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 972 MBytes 815 Mbits/sec
[ 3] 10.0-20.0 sec 971 MBytes 815 Mbits/sec
[ 3] 20.0-30.0 sec 971 MBytes 815 Mbits/sec
[ 3] 30.0-40.0 sec 971 MBytes 815 Mbits/sec
[ 3] 40.0-50.0 sec 971 MBytes 815 Mbits/sec
[ 3] 50.0-60.0 sec 971 MBytes 815 Mbits/sec
[ 3] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec
[ 3] Sent 4157414 datagrams
[ 3] Server Report:
[ 3] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec 0.013 ms 727/4157413 (0.017%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order
[root@minion ~]# iperf -c 10.65.14.43 -u -m -t 60 -i 10 -b 815M
————————————————————
Client connecting to 10.65.14.43, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 122 KByte (default)
————————————————————
[ 3] local 10.65.14.42 port 17417 connected with 10.65.14.43 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 972 MBytes 815 Mbits/sec
[ 3] 10.0-20.0 sec 972 MBytes 815 Mbits/sec
[ 3] 20.0-30.0 sec 972 MBytes 815 Mbits/sec
[ 3] 30.0-40.0 sec 972 MBytes 815 Mbits/sec
[ 3] 40.0-50.0 sec 972 MBytes 815 Mbits/sec
[ 3] 50.0-60.0 sec 972 MBytes 815 Mbits/sec
[ 3] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec
[ 3] Sent 4158793 datagrams
[ 3] Server Report:
[ 3] 0.0-60.0 sec 5.69 GBytes 815 Mbits/sec 0.013 ms 2061/4158792 (0.05%)
[ 3] 0.0-60.0 sec 1 datagrams received out-of-order