网络性能测试工具-iPerf3使用文档


下载安装

yum 安装

yum install -y ipef3

源码安装

下载最新版本源码包,下载地址:http://downloads.es.net/pub/iperf/

本文以安装 iperf-3.6.tar.gz 版本为例。

yum install wget gcc make -y

wget https://downloads.es.net/pub/iperf/iperf-3.6.tar.gz

tar -zxvf iperf-3.6.tar.gz

cd iperf-3.6

./configure --prefix=/usr/local/iperf

make && make install

ln -s /usr/local/iperf/bin/iperf3 /usr/bin/iperf3

iperf介绍及详细使用

介绍

Iperf 是一个网络性能测试工具。Iperf可以测试最大TCP和UDP带宽性能,具有多种参数和UDP特性,可以根据需要调整,可以报告带宽、延迟抖动和数据包丢失。

iperf只支持千兆流量的测试,iperf3才支持万兆流量测试

此外,iperf 分为两种版本:

  • unix/linux版,更新比较快,版本最新
  • windows版,更新慢

windows版的iperf叫jperf,或者xjperf。jperf是在iperf基础上开发了更好的UI和新的功能。


测试场景

TCP方面

  1. 测试网络带宽。

  2. 支持多线程,在客户端与服务端支持多重连接。

  3. 报告MSS/MTU值的大小。

  4. 支持TCP窗口值自定义并可通过套接字缓冲。

UDP方面

  1. 可以设置指定带宽的UDP数据流。

  2. 可以测试网络抖动值、丢包数。

  3. 支持多播测试。

  4. 支持多线程,在客户端与服务端支持多重连接。

其他

  1. 在适当的地方,选项中可以使用K(kio-)和M(mega-)

  2. 可以指定运行的总时间,设置可以设置传输的数据总量

  3. 在报告中,为数据选用最合适的单位

  4. 服务器支持多重连接,而不是等待一个单线程测试

  5. 在指定时间间隔重复显示网络带宽,波动和丢包情况

  6. 服务器端可做后台程序运行

  7. 服务器端可作为window服务运行

  8. 使用典型数据流测试链接层压缩对于可用带宽的影响

  9. 支持传输指定文件,可用定性和定量测试


实例

TCP测试

服务端
  • -s 表示服务器端;
  • -p 定义监听端口号;
  • -i 设置每次报告之间的时间间隔,单位为秒,如果设置为非零值,就会按照此时间间隔输出测试报告,默认值为零

例如:

iperf3 -s -p 12345 -i 1
客户端
  • -c 表示服务器的IP地址;
  • -p 表示服务器的端口号;
  • -i 设置每次报告之间的时间间隔,单位为秒,如果设置为非零值,就会按照此时间间隔输出测试报告,默认值为零;
  • -t 设置传输的总时间,iperf在指定的时间内,重复的发送指定长度的数据包,默认是10秒钟;
  • -w 设置套接字缓冲区为指定大小
    • 对于TCP方式,此设置为TCP窗口大小,416k为tcp最大值;
    • 对于UDP方式,此设置为接受UDP数据包的缓冲区大小,限制可以接受数据包的最大值;
  • -P 表示线程数,默认为1;
  • -d debug日志输出;

例如:

# 假定服务端IP是192.168.7.151
iperf3 -c 192.168.7.151 -p 12345 -i 1 -w 416k -P 10 -t 30

参考输出结果如下:

[ ID] Interval           Transfer     Bitrate
[  5]   0.00-10.00  sec  5.50 GBytes  4.73 Gbits/sec                  receiver
[  8]   0.00-10.00  sec  5.45 GBytes  4.68 Gbits/sec                  receiver
[SUM]   0.00-10.00  sec  11.0 GBytes  9.41 Gbits/sec                  receiver

UDP测试

服务端
iperf3 -s -p 12345 -i 1
客户端
iperf3 -c 192.168.7.151 -p 12345 -i 1 -u -t 30 -b 10G
  • -b 10G 表示在 udp 测试时,设置测试发送的带宽,单位:bit/秒,不设置时默认为:1Mbit/秒,1G 的线路你可以使用 900M 进行测试。

可能遇到的问题

1. 带宽压测上不去

倘若在大带宽打流测试中达不到预期带宽 ( 例如: 10G网络 ) ,此时不妨检查网卡 MTU 值。

因为在缺省的网卡设定中,MTU 是 1500 ,也就是一个IP包大小是 1500 ,而对于 10G 网络来说,包太小,意味着相同数据量,包的数量多,每个包的头部数据将消耗大量的网络带宽,可以将 MTU 设置成 9000 ( 参考命令: ifconfig eno5 mtu 9000 ) 。


iperf的参数详解

GENERAL OPTIONS

(通用参数,不管iperf3跑在client还是Server模式下都适用的参数)

Command line option(命令行参数) Description(描述说明)
-p, –port n The server port for the server to listen on and the client to connect to. This should be the same in both client and server. Default is 5201.



1、服务端:服务端监听的端口,默认5201端口,同时监听TCP/UDP

2、客户端:客户端去连接的端口,默认5201,如果同时有-u参数,则用UDP发起连接,如果没有,则默认使用TCP连接



使用方式:

iperf3 -s -p 6666, 在6666端口监听

iperf3 -c 192.168.0.1 -p 6666, 去连接服务端的6666端口
–cport n Option to specify the client-side port. (new in iPerf 3.1)

本参数提供了一个指定客户端本地端口的选项,区分于-p指定的是服务端的远端端口

使用方式:

iperf3 -s –cport 8888, 在服务端指定客户端本地端口只能是8888? 从测试结果看,这个选项在服务端不起任何作用,但执行命令不会报错

iperf3 -c 192.168.0.1 –cport 8888,指定客户端选择本地端口为8888
-f, –format [kmKM] A letter specifying the format to print bandwidth numbers in. Supported formats are

<br> 'k' = Kbits/sec 'K' = KBytes/sec 'm' = Mbits/sec 'M' = MBytes/sec<br>

The adaptive formats choose between kilo- and mega- as appropriate.

指定打印测试结果的带宽(bitrate比特率这一列)时用什么格式显示:k表示:千比特/秒, K表示:千字节/秒, m表示:兆比特/秒, K表示:兆字节/秒。

客户端和服务端可以用不同的参数,各自按照各自指定的方式打印出带宽测试结果

使用方式:

iperf3 -s -f M

iperf3 -c 192.168.0.1 -f k
-i, –interval n Sets the interval time in seconds between periodic bandwidth, jitter, and loss reports. If non-zero, a report is made every interval seconds of the bandwidth since the last report. If zero, no periodic reports are printed. Default is zero.

以秒为单位,设置带宽(还有抖动,丢包等)测试报告打印输出的周期。不使用这个选项时,默认周期是1秒,可以使用0和任何0.1秒到60秒之间的参数,如果是0表示不打印周期性的报告,只在整个测试完成时,输出一次报告结果。

(上面英文解释说默认值是0这个是错误的,默认值实际上是1),客户端和服务端可以使用不同的参数值。

使用方式:

iperf3 -s -i 0

iperf3 -c 192.168.0.1 -i 3.6
-F, –file name client-side: read from the file and write to the network, instead of using random data;
server-side: read from the network and write to the file, instead of throwing the data away.

客户端:从文件中读取数据,并发送到网络里,如果不加这个选项默认系统会产生随机数据发送到网络里
服务端:把网络上收到的数据写入文件中,默认系统会丢弃收到的数据

文件格式未知,在google上也找不到什么说明,待后续更新()
使用方式:
iperf3 -s -F ./log.txt

iperf3 -c 192.168.0.1 -F ./input.txt
-A, –affinity n/n,m-F Set the CPU affinity, if possible (Linux and FreeBSD only). On both the client and server you can set the local affinity by using the n form of this argument (where n is a CPU number). In addition, on the client side you can override the server’s affinity for just that one test, using the n,m form of argument. Note that when using this feature, a process will only be bound to a single CPU (as opposed to a set containing potentialy multiple CPUs).

如果系统支持则可以设置CPU亲和性(只支持Linux和FreeBSD),说白了就是在设定将iperf3进程跑在哪个CPU上。
客户端和服务端都可以设置CPU亲和性,n表示CPU编号(**Linux下如何查看CPU编号及其它CPU相关的信息**)。
此外在客户端可以通过”-A n,m”的形式,覆盖server端的CPU亲和性设置(就是覆盖运行server端时,指定运行在哪个CPU上的设置),指定本次运行时iperf3的服务端跑在m号CPU上,但只对这一次运行生效。
注意如果用了这个参数,意味着iperf3只会在指定的CPU上运行(默认不加这个参数时,操作系统会自行调度CPU,负荷重(加多线程?)有可能会把iperf3同时运行在多个CPU上)
使用方式:

iperf3 -s -A 2,指定服务端运行在2号CPU上

iperf3 -c 192.168.0.1 -A 0,指定客户端运行在0号CPU上
或者
iperf3 -c 192.168.0.1 -A 0,3,指定本次运行客户端运行在0号CPU上而服务端运行在2号CPU上
-B, –bind host Bind to host, one of this machine’s addresses. For the client this sets the outbound interface. For a server this sets the incoming interface. This is only useful on multihomed hosts, which have multiple network interfaces.

绑定到主机,主机的一个地址。对客户端来说,这个设置数据发送从哪个接口发送。

对服务端来说,这是设置数据从哪个接口接收。这个命令只在多宿主主机(二个或者二个以上网络接口)中有用。

这个选项对接口的定义很不清楚,没有讲清楚 IP接口和物理网卡接口选择时,-B与主机路由的选择关系,

可以参照另外一个文章有清楚的描述:**iPerf3 -B (–bind host)绑定发送端网卡/发送IP端地址**

以及Linux侧路由配置:Linux上添加路由,删除路由,修改路由配置(路由表项基本知识)
-V, –verbose give more detailed output

输入更多的详细信息
-J, –json output in JSON format

运行结束时,用JSON的格式输出测试结果,默认打印到屏幕上,可以结合下面”–logfile”或者linux本身的重定向命令”>”将输出报告打印到指定文件里
–logfile file send output to a log file. (new in iPerf 3.1)

把测试结果存到指定的log文件
-d, –debug emit debugging output. Primarily (perhaps exclusively) of use to developers.

打印出更多的调试用的log,主要是给开发者用的,如果不是碰到需要调试的问题,这个参数基本没什么用。
-v, –version Show version information and quit.

只要加了-v这个参数,iperf3会显示版本号,然后自动退出
-h, –help Show a help synopsis and quit.

只要加了-h这个参数,iperf3会显示帮助信息(包括命令使用格式,各个参数的简明的使用说明),然后自动退出

SERVER SPECIFIC OPTIONS(服务端专用参数)

Command line option(命令行参数) Description(具体描述)
-s, –server Run iPerf in server mode. (This will only allow one iperf connection at a time)


本次iperf3运行在服务端模式(-c和-s是互斥的,二者必居其一),括号里的这句话:这只允许一次一个iperf connection,是指iperf服务端同一时间只接受一个TCP或者UDP连接。

意味着同一时间只有一个客户端可以给这个iperf3服务端进程打流。(但可以在通过-p参数,指定在同一个主机的同一个IP地址的不同的端口同时开多个iperf3服务端进程)

使用方式:

iperf3 -s
-D, –daemon Run the server in background as a daemon.

以daemon的形式在后台运行(不会向控制台窗口输出,可以通过ps -aux | grep iperf3看到对应的后台进程号)

和用iperf3 -s &命令(注意命令最后的&号)类似,都可以让iperf3在后台运行,但iperf3 -s &命令的话,控制台终端还是可以得到iperf3的打印输出的。

但用&的后台运行,在你关闭终端时,程序也会关闭,而用-D的,则在终端关闭后,iperf3 -s会继续在后台运行

下面文章可以详细了解Linux前后台程序,如何让程序在后台运行,如何切换前后台,以及Daemon进程:

如何在Linux前后台切换程序, 用&把程序运行在后台和以Daemon方式运行的程序(控制台退出后程序继续保持运行)

使用方式:

iperf3 -s -D
-I, –pidfilefile write a file with the process ID, most useful when running as a daemon. (new in iPerf 3.1)

这介需要iperf3.1以上版本才支持,提供了一个将本次运行的iperf3进程的PID写入到指定文件里的功能,正常的情况下也没啥用,大概率在将程序运行成Daemon程序,

并用需要通过PID去监控,KILL或者其它控制这个进程的自动化功能时用到(原文件是这么说的,但我觉得这个指供的功能和ps -aux | grep iperf3得到的信息是一样的,

对于工作在Linux下的人来说并没有什么实际意义,估计是给其它平台的工作者用的)

使用方式:

iperf3 -s -I /home/user/iperf.pid

注意:

1)当前用户一定要有/home/user目录的写权限

2)文件可以事先用touch命令创建好,也可以不用创建

3)iperf3当前进各结束后/home/user/iperf.pid文件会被主动删除

4)/home/user/iperf.pid一定要使用全路径,不能使用默认的当前路径

(使用当前路径分导致各种奇怪的iperf启动失败,或者启动成功了,但指定目录找到不iperf.pid文件),原因不明

CLIENT SPECIFIC OPTIONS(客户端专用参数)

Command line option(命令行参数) Description(具体描述)
-c, –client host Run iPerf in client mode, connecting to an iPerf server running on host.

本次iperf3运行在客户端状态,host指向的是iperf服务端的ip地址

使用方式:

iperf3 -c 192.168.0.10, 本机做为客户端连接到192.168.0.10(服务端运行在这人ip地址上),并做打流量测试
–sctp Use SCTP rather than TCP (Linux, FreeBSD and Solaris). (new in iPerf 3.1)

使用sctp协议(不加这人选项的话默认使用tcp),iperf3.1及更新的版本开始支持

使用方式:

iperf3 -c 192.168.0.10 –sctp
-u, –udp Use UDP rather than TCP. See also the -b option.

使用udp协议(不加这人选项的话默认使用tcp),使用这个选项的话,还需要查看-b选项,因为UDP是面向数据报的协议,所以一般要用-b指定打多大带宽的数据(不指定-b的话,默认值见对-b参数的解释)

使用方式:

iperf3 -c 192.168.0.10 -u
-b, –bandwidth n[KM] Set target bandwidth to n bits/sec (default 1 Mbit/sec for UDP, unlimited for TCP). If there are multiple streams (-P flag), the bandwidth limit is applied separately to each stream. You can also add a ’/’ and a number to the bandwidth specifier. This is called “burst mode”. It will send the given number of packets without pausing, even if that temporarily exceeds the specified bandwidth limit.

设置本次测试的发送带宽成n 比特/秒(对UDP来说默认是1M 比特/秒,对TCP来说默认是不限制),如果同时还设置了多线程(-P选项),当前这个-b的带宽指定的是每个线程的发送带宽。

我们还可以在带宽后面加上/和一个数字。我们称这个为“突发脉冲模式”。加了这个“/数字”后,iperf3客户端会一次连续发送指定的数量的数据包,中间不做任何间隔(不加这个 “/数字”时,iperf3会根据每个报文的长度,算出为了达到指定的带宽,每秒需要发送几个报文,然后每个数据报文会被以平均的时间间隔进行发送),即使中间会有偶尔的超出带宽限制

使用方式:

iperf3 -c 192.168.0.10 -u -b 100M,报文平均的落在时间线上

或者

iperf3 -c 192.168.0.10 -u -b 100M/40,报文以40个为一组,连续发送40个报文,每一组平均的落在时间线上,每一组发送时可能超出-b指定的带宽,但iperf3保证整个打流期间的平均带宽不超过指定值。

详见:**iperf3 -b参数详细图文分析**
-t, –time n The time in seconds to transmit for. iPerf normally works by repeatedly sending an array of len bytes for time seconds. Default is 10 seconds. See also the -l, -k and -n options.

定义本次发送报文测试要持续的时间,单位为秒iPerf通常工作的模式是:持续-t定义的时间(秒)不停的重复发送一组长度为-l定义的数据包。默认值是10秒。参照-l, -k和-n选项
使用方式:
iperf3 -c 127.0.0.1 -u -t 11
注意这个参数不能和-n或者-k同时使用,上述例子表示连续发送11秒钟时间后停止测试
-n, –num n[KM] The number of buffers to transmit. Normally, iPerf sends for 10 seconds. The -n option overrides this and sends an array of len bytes num times, no matter how long that takes. See also the -l, -k and -t options.

定义本次发送报文要发送的总的数据长度,单位是字节。默认情况下iperf持续发送10秒时间。加了-n选项后,就不覆盖了10秒这个默认设置,而是发送-n指定长度的数据,发送完后停止。参照-l,-k和-t选项
使用方式:
iperf3 -c 127.0.0.1 -u -n 40M
注意这个参数不能和-t或者-k同时使用,长度后面不带单位的话默认单位是字节,也可以用K/M表示千/兆字节,上述例子表示总共要发送40M字节的报文
-k, –blockcount n[KM] The number of blocks (packets) to transmit. (instead of -t or -n) See also the -t, -l and -n options.

定义本次发送报文要发送的报文个数。发送-k指定个数的报文,发送完后停止。参照-l,-n和-t选项
使用方式:
iperf3 -c 127.0.0.1 -u -k 37
注意这个参数不能和-t或者-n同时使用,上面例子表示总共要发送37个报文
-l, –length n[KM] The length of buffers to read or write. iPerf works by writing an array of len bytes a number of times. Default is 128 KB for TCP, 8 KB for UDP. See also the -n, -k and -t options.

每次发报的报文长度。iperf发送一组长度为-l指定的数据报文,每个数据报文的长度都是-l指定的长度。默认TCP是128KB, UDP是8KB。参照-n, -k和-t选项。

使用方式:

iperf3 -c 192.168.3.15 -u -l 2K

注意: 这个参数可以和-n, -k和-t参数一起使用。
-P, –parallel n The number of simultaneous connections to make to the server. Default is 1.

到iperf3服务器端并行的连接数量,默认值是1。(未知并行这个参数其实不是个多线程?这个需要看原代码。如果不是多纯种那么很奇怪为什么多线程在某些测试过程中能测出更大的带宽?)

使用方式:

iperf3 -c 192.168.3.15 -u -P 3

表示开3个连接,这里可以查看链接文章:如何查看LINUX里的某个进程的线程
-R, –reverse Run in reverse mode (server sends, client receives).

工作在反向连接模式(服务器端发送,客户端接收)

使用方式:

iperf3 -c 192.168.3.15 -u -R
-w, –window n[KM] Sets the socket buffer sizes to the specified value. For TCP, this sets the TCP window size. (this gets sent to the server and used on that side too)

设置socket缓冲的大小。对TCP来说,这个用于设置 TCP 窗口接收的大小(这个选项会被发送到服务器端,同样的参数会在服务器端生效)

iperf3 -c 192.168.3.15 -w 100k

对于UDP来说,通常不需要设置这个参数,这个数设置得太小了,会导致发送端发送速度慢,

对于TCP来说,这个参数的设置值的计算和选择方法详见:TCP滑动窗口协议与窗口大小的计算
-M, –set-mss n Attempt to set the TCP maximum segment size (MSS). The MSS is usually the MTU - 40 bytes for the TCP/IP header. For ethernet, the MSS is 1460 bytes (1500 byte MTU).

尝试去设置TCP的MSS最大分片长度。MSS通常是MTU减去40字节的TCP/IP首部。对以达网来说MSS是1460(MTU是1500字节)

使用方式:

iperf3 -c 192.168.3.15 -M 100

MSS设置得很小,会导致TCP分成很多个小包进行发送,过小会导致带宽测试结果偏小。

详见:**TCP的MSS的计算与详解**
-N, –no-delay Set the TCP no delay option, disabling Nagle’s algorithm. Normally this is only disabled for interactive applications like telnet.

设置TCP的不延时选项,关闭Nagle’s算法,通常这个选项只用于交互式人机对话的应用程序中,比如Telnet

使用方式:

iperf3 -c 192.168.3.26 -N
-4, –version4 only use IPv4.

只使用IPv4
-6, –version4 only use IPv6.

只使用IPv6, 注意这个要配合IPv6的地址一起使用
-S, –tos n The type-of-service for outgoing packets. (Many routers ignore the TOS field.) You may specify the value in hex with a ‘0x’ prefix, in octal with a ‘0’ prefix,

or in decimal. For example, ‘0x10’ hex = ‘020’ octal = ‘16’ decimal. The TOS numbers specified in RFC 1349 are:

IPTOS_LOWDELAY minimize delay 0x10

IPTOS_THROUGHPUT maximize throughput 0x08

IPTOS_RELIABILITY maximize reliability 0x04

IPTOS_LOWCOST minimize cost 0x02

设置被发送出去的报文的TOS字段。(目前很多路由器是不处理TOS字段的),可以用0x打头输入16进制数,0打头输入8进制数,或者不需要前缀则默认是10进制数。

比如: 16进制的”0x10”表示8进制的”020”表示十进制的16。

TOS定义如下:

IPTOS_LOWDELAY 最小延时 0x10

IPTOS_THROUGHPUT 最大带宽 0x08

IPTOS_RELIABILITY 可靠性最好 0x04

IPTOS_LOWCOST 最便宜 0x02

使用方式:

iperf3 -c 192.168.3.15 -S 0x10
-L, –flowlabel n Set the IPv6 flow label (currently only supported on Linux).

设置IPv6的流标签(目前只有Linux版本的iperf3能支持)
-Z, –zerocopy Use a “zero copy” method of sending data, such as sendfile(2), instead of the usual write(2). This uses much less CPU.

使用“零拷贝”办法来发送数据,比如使用sendfile(2)代替write(2).这会减少CPU的占用率

使用方式:

iperf3 -c 192.168.3.15 -u -Z
-O, –omit n Omit the first n seconds of the test, to skip past the TCP TCP slowstart period.

忽略最前面n秒的测试结果,通常用来跳过TCP慢启动过程

使用方式:

iperf3 -c 192.168.3.15 -O 20 -t 40

忽略最前面的20秒,然后再打40秒的流(见-t参数)

详见:TCP慢启动过程详解
-T, –title str Prefix every output line with this string.

每行打印输出内容前加一个前缀

使用方式:

iperf3 -c 192.168.3.15 -T 这是前缀

在每行输入前面加了“这是前缀”这4个字,不知道这个功能有什么用
-C, –linux-congestion algo Set the congestion control algorithm (Linux only for iPerf 3.0, Linux and FreeBSD for iPerf 3.1).

设置TCP拥塞控制算法(在LINUX上3.0以上支持,在LINUX和FREEBSD上3.1以下版本支持)

文章作者: NesteaLin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 NesteaLin !
  目录