iperf是一种命令行工具,用于通过测量服务器可以处理的最大网络吞吐量来诊断网络速度问题。它在遇到网络速度问题时特别有用,通过该工具可以确定哪个服务器无法达到最大吞吐量。
开始之前
- 您需要root权限具有sudo权限的用户帐户。
- update 系统源
安装iperf
- 该iperf软件包在大多数Linux发行版的存储库中
Debian和Ubuntu安装
apt-get update apt-get install iperf
CentOS 安装
- CentOS存储库没有iperf,使用EPEL存储库,它是用于在Redhat系统上安装第三方软件包的存储库
yum install epel-release
yum update
yum install iperf
使用iperf的注意事项
-
必须在测试的两台计算机上同时安装 iperf 。如果在个人计算机上使用基于Unix或 Linux的操作系统,则可以在本地计算机上安装 iperf。
-
但是,如果要测试网络提供商的吞吐量,最好使用另一台服务器作为终点,因为本地ISP可能会施加影响测试结果的网络限制。
- 测试的时候,两台机器都使用同一版本的 iperf 。
Iperf参数说明
主客端参数 -s //以server模式启动,eg:iperf -s -c //host以client模式启动,host是server端地址,eg:iperf -c 222.35.11.23 =============== 通用参数 -f //[kmKM] 分别表示以Kbits, Mbits, KBytes, MBytes显示报告,默认以Mbits为单位,eg:iperf -c 222.35.11.23 -f K -i //sec 以秒为单位显示报告间隔,eg:iperf -c 222.35.11.23 -i 2 -l //缓冲区大小,默认是8KB,eg:iperf -c 222.35.11.23 -l 16 -m //显示tcp最大mtu值 -o //将报告和错误信息输出到文件eg:iperf -c 222.35.11.23 -o ciperflog.txt -p //指定服务器端使用的端口或客户端所连接的端口eg:iperf -s -p 9999;iperf -c 222.35.11.23 -p 9999 -u //使用udp协议 -w //指定TCP窗口大小,默认是8KB -B //绑定一个主机地址或接口(当主机有多个地址或接口时使用该参数) -C //兼容旧版本(当server端和client端版本不一样时使用) -M //设定TCP数据包的最大mtu值 -N //设定TCP不延时 -V //传输ipv6数据包 =============== server专用参数 -D //以服务方式运行iperf,eg:iperf -s -D -R //停止iperf服务,针对-D,eg:iperf -s -R =============== client端专用参数 -d //同时进行双向传输测试 -n //指定传输的字节数,eg:iperf -c 222.35.11.23 -n 100000 -r //单独进行双向传输测试 -t //测试时间,默认10秒,eg:iperf -c 222.35.11.23 -t 5 -F //指定需要传输的文件 -T //指定ttl值
TCP客户端和服务器的测试
iperf需要两个系统,因为一个系统必须充当服务端,另外一个系统充当客户端,客户端连接到需要测试速度的服务端
1.在需要测试的电脑上,以服务器模式启动iperf
iperf -s
可以看到类似于下图的输出
------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 85.3 KByte (default) ------------------------------------------------------------
2.在第二台电脑上,以客户端模式启动iperf连接到第一台电脑,替换198.51.100.5为地台电脑的ip地址
iperf -c 198.51.100.5
------------------------------------------------------------ Client connecting to 198.51.100.5, TCP port 5001 TCP window size: 45.0 KByte (default) ------------------------------------------------------------ [ 3] local 198.51.100.6 port 50616 connected with 198.51.100.5 port 5001 [ ID] Interval Transfer Bandwidth [ 3] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec
3.这时可以在第一步中的服务端终端看到连接和结果
------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 85.3 KByte (default) ------------------------------------------------------------ [ 4] local 198.51.100.5 port 5001 connected with 198.51.100.6 port 50616 [ ID] Interval Transfer Bandwidth [ 4] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec
4.要停止iperf服务进程,请按CTRL+c
UDP客户端和服务器的测试
使用iperf,还可以测试通过UDP连接实现的最大吞吐量
1.需要测试的机器作为服务端,输入下面命令回车等待客户端发送测试包
iperf -s -p 5001 -f m -i 1
2.找另一台机器做客户端,两台机器的用网线连接要测试的网卡端口,输入下面命令回车测试
iperf -u -c 192.168.1.1. -p 5001 -p 1 -f m -i 1 -b 1000M -t 30
双向测试
在某些情况下,可能希望测试两台服务器以获得最大吞吐量。使用iperf提供的内置双向测试功能可以轻松完成此测试。
- 要测试两个连接,从客户端运行一下命令,替换ip为服务端ip地址
-
iperf -c 198.51.100.5 -d
- 结果是iperf将在客户端服务器上启动服务器和客客户端(198.51.100.6)连接。完成此操作后,iperf会将iperf服务器连接到客户端,该连接现在既充当服务器连接又充当客户端连接。
-
------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 85.3 KByte (default) ------------------------------------------------------------ ------------------------------------------------------------ Client connecting to 198.51.100.5, TCP port 5001 TCP window size: 351 KByte (default) ------------------------------------------------------------ [ 3] local 198.51.100.6 port 50618 connected with 198.51.100.5 port 5001 [ 5] local 198.51.100.6 port 5001 connected with 198.51.100.5 port 58650 [ ID] Interval Transfer Bandwidth [ 5] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec [ 3] 0.0-10.2 sec 1.28 GBytes 1.08 Gbits/sec
在服务器是哪个,可以看到:
-
------------------------------------------------------------ Client connecting to 198.51.100.6, TCP port 5001 TCP window size: 153 KByte (default) ------------------------------------------------------------ [ 6] local 198.51.100.5 port 58650 connected with 198.51.100.6 port 5001 [ 6] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec [ 5] 0.0-10.2 sec 1.28 GBytes 1.08 Gbits/sec