当前位置:首页>Linux>linux iperf工具介绍

linux iperf工具介绍

  • 2026-10-11 08:28:15
linux iperf工具介绍

每一个城市的名字,都藏着一座城市的前世今生,从东四省到东三省,春风不语,春风依旧。

一,什么是iperf

#源码https://github.com/esnet/iperf#下载https://iperf.fr/iperf-doc.php

Iperf 是一款专业且开源的网络性能测试工具,主要用于测量两个网络节点之间的最大 TCP 和 UDP 带宽性能。

采用客户端/服务器(C/S)架构,通过在两台设备(如电脑、服务器)上分别运行 iperf 的服务端和客户端,来创建数据流,从而精确地测量它们之间的网络质量。

Iperf 可以报告多种关键网络指标,帮助全面评估网络状况:

  • 带宽测试
    :测量网络链路的最大吞吐量,支持 TCP 和 UDP 两种协议。
  • 数据包丢失
    :在 UDP 测试中,可以精确计算出数据传输过程中的丢包率。
  • 网络延迟与抖动
    :评估网络连接的稳定性和延迟变化情况。
  • 多线程支持
    :支持并行发起多个连接进行测试,能更充分地测试高带宽网络的性能。
  • 版本差异
    :目前主要有 iperf2 和 iperf3 两个版本,两者不完全兼容。iperf3 是在 iperf2 基础上开发的,代码更精简,并增加了一些新功能(如反向模式测试、JSON格式输出),但移除了部分旧功能。
  • 跨平台支持
    :Iperf 支持几乎所有主流操作系统,包括 Linux、Windows、macOS,甚至有 Android 和 iOS 的移动版本。

二,安装和参数

网址:https://iperf.fr/iperf-doc.phpiPerf3 在不同操作系统上的安装指南及 Ubuntu 系统升级安装方法🖥️ WindowsWindows 二进制文件可在以下地址获取:https://files.budman.pwhttps://github.com/ar51an/iperf3-win-builds🍎 macOS通过 HomeBrew 安装:brew install iperf3通过 MacPorts 安装:sudo port install iperf3🤖 AndroidAndroid 版本编译指南:https://github.com/davidBar-On/android-iperf3/🐧 Ubuntu / Debian / Mint打开终端并输入:sudo apt-get install iperf3🐧 Fedora / Red Hat / CentOS / Rocky打开终端并输入:yum install iperf3🐡 FreeBSD打开终端并输入:sudo pkg install benchmarks/iperf3📝 iPerf3 服务器日志脚本脚本名称:iperf3tocsv.py (2.5 KiB) - 作者:Kirth Gersen功能:记录 iPerf3 日志,显示内容包括:"日期, IP, 本地端口, 远程端口, 持续时间, 协议, 流数量, Cookie, 发送量, 发送速率(Mbps), 接收量, 接收速率(Mbps), 总发送量, 总接收量"🛠️ 如何在 Ubuntu/Debian/Mint 上安装比系统自带版本更新的 iPerf?1. 移除旧版本sudo apt remove iperf3 libiperf02. 安装依赖项sudo apt install libsctp13. 获取最新版本从 https://launchpad.net/ubuntu/+source/iperf3 下载适用于 Ubuntu 的最新版本。4. 安装下载的软件包下载 iperf3_3.xx-1_amd64.deb 和 libiperf0_3.xx-1_amd64.deb 包(标准 Ubuntu 版本请使用 amd64 版本)。安装命令:sudo dpkg -i libiperf0_3.xx-1_amd64.deb iperf3_3.xx-1_amd64.deb5. 清理删除已安装的无用安装包:rm libiperf0_3.xx-1_amd64.deb iperf3_3.xx-1_amd64.deb

这里我选择源码安装

https://downloads.es.net/pub/iperf/  手动下载或者wget都行tar -xzvf iperf-3.21.tar.gz cd iperf-3.21./configure &&  make && make install[root@master1 iperf-3.21]# iperf3 -viperf 3.21 (cJSON 1.7.15)Linux master1 3.10.0-1160.36.2.el7.x86_64 #1 SMP Wed Jul 21 11:57:15 UTC 2021 x86_64Optional features available: CPU affinity setting, IPv6 flow label, TCP congestion algorithm setting, sendfile / zerocopy, socket pacing, authentication, bind to device, support IPv4 don't fragment, POSIX threads
参数
[root@localhost iperf-3.21]# iperf3 -hUsage: iperf3 [-s|-c host] [options]       iperf3 [-h|--help] [-v|--version]通用设置(服务器或客户端):-p, --port #			服务器监听或客户端连接的端口号-f, --format [kmgtKMGT]	报告格式:Kbits, Mbits, Gbits, Tbits-i, --interval #		周期性吞吐量报告的间隔秒数-I, --pidfile file		写入 PID 文件-F, --file name			发送/接收指定的文件-A, --affinity n[,m]	设置 CPU 亲和性核心编号为 n(进程使用的核)。(可选客户端仅 m - 服务器用于此测试的核心编号)-B, --bind <host>[%<dev>]	绑定到与地址 <host> 关联的接口 (可选 <dev> 等同于 --bind-dev <dev>)--bind-dev <dev>		使用 SO_BINDTODEVICE 绑定到网络接口-V, --verbose			输出更详细的信息-J, --json				以 JSON 格式输出--json-stream			以换行分隔的 JSON 格式输出--json-stream-full-output	启用 JSON 流时以 JSON 格式输出--logfile f				将输出发送到日志文件--forceflush			强制在每个间隔刷新输出--timestamps [=format]	在每行输出的开头发出时间戳 (可选 "=" 和格式字符串,遵循 strftime(3))--rcv-timeout #			接收数据的空闲超时时间(默认 120000 毫秒)--snd-timeout #			未确认 TCP 数据的超时时间 (毫秒,默认为系统设置)--cntl-ka [=#/#/#]		使用控制连接 TCP keepalive - KEEPIDLE/KEEPINTV/KEEPCNT (每个值都是可选的,默认为系统设置)-d, --debug [=#]		发出调试输出 (可选 "=" 和调试级别: 1-4。默认是 4 - 所有消息)-v, --version			显示版本信息并退出-h, --help				显示此帮助信息并退出服务器专用设置:-s, --server			以服务器模式运行-D, --daemon			将服务器作为守护进程运行-1, --one-off			处理一个客户端连接后退出--server-bitrate-limit #[KMG][/#]	服务器总比特率限制(默认 0 = 无限制)。(可选斜杠和用于平均总数据速率的秒数间隔。默认是 5 秒)--idle-timeout #		服务器空闲 # 秒后重启(以防卡住,默认 - 无超时)--server-max-duration #	        iperf 测试针对服务器运行的最大时间(秒)--rsa-private-key-path	        用于解密身份验证凭据的 RSA 私钥路径--authorized-users-path	        包含用户凭据的配置文件路径--time-skew-threshold	        在身份验证过程中,服务器和客户端之间的时间偏移阈值(秒)--use-pkcs1-padding		自行承担风险使用 pkcs1 填充客户端专用设置:-c, --client <host>[%<dev>]			以客户端模式运行,连接到 <host> (可选 <dev> 等同于 --bind-dev <dev>)-u, --udp							使用 UDP 而不是 TCP--connect-timeout #					控制连接建立的超时时间(毫秒)-b, --bitrate #[KMG][/#]			目标比特率(比特/秒)(0 表示无限制)。(默认 UDP 为 1 Mbit/sec,TCP 为无限制) (可选斜杠和数据包计数以用于突发模式)--pacing-timer #[KMG]				设置服务器用于节流(pacing)的时间,单位为微秒(默认 1000)。 (已弃用 - 用于旧版本服务器的向后兼容性)--fq-rate #[KMG]					在比特/秒中启用基于公平排队的套接字节流(Linux 仅限)-t, --time #						传输持续时间(秒)(默认 10 秒)-n, --bytes #[KMG]					传输直到客户端发送或接收(每个方向)至少此数量的字节(代替 -t 或 -k)-k, --blockcount #[KMG]				传输直到客户端发送或接收(每个方向)至少此数量的数据块(代替 -t 或 -n)-l, --length #[KMG]					读写缓冲区的长度(默认 TCP 为 128 KB,UDP 为动态或 1460)--cport <port>						绑定到特定的客户端端口(TCP 和 UDP,默认:临时端口)-P, --parallel #					运行的并行客户端流数量-R, --reverse						以反向模式运行(服务器发送,客户端接收)--bidir								以双向模式运行。客户端和服务器同时发送和接收数据。-w, --window #[KMG]					设置发送/接收套接字缓冲区大小(间接设置 TCP 窗口大小)-C, --congestion <algo>				设置 TCP 拥塞控制算法(仅限 Linux 和 FreeBSD)-M, --set-mss #						设置 TCP/SCTP 最大段大小(MTU - 40 字节)-N, --no-delay						设置 TCP/SCTP 无延迟,禁用 Nagle 算法-4, --version4						仅使用 IPv4-6, --version6						仅使用 IPv6-S, --tos N							设置 IP 服务类型,0-255。可以使用八进制和十六进制的前缀,例如 52, 064 和 0x34 都指定相同的值。--dscp N 或 --dscp val				设置 IP dscp 值,0-63 或符号。数值可以指定为十进制、八进制和十六进制(见上面的 --tos)。-L, --flowlabel N					设置 IPv6 流标签(仅 Linux 支持)-Z, --zerocopy						使用“零拷贝”方法发送数据--skip-rx-copy						使用 MSG_TRUNC 选项忽略接收的消息-O, --omit N						执行 N 秒的预测试,并省略预测试统计信息-T, --title str						在每行输出前加上此字符串--extra-data str					包含在客户端和服务器 JSON 中的数据字符串--get-server-output					从服务器获取结果--udp-counters-64bit				在 UDP 测试数据包中使用 64 位计数器--gsro								在客户端和服务器上启用 UDP GSO/GRO(仅客户端选项)--repeating-payload					在有效载荷中使用重复模式,而不是随机化有效载荷(像 iperf2 一样)--dont-fragment						设置 IPv4 不分段标志--username							用于身份验证的用户名--rsa-public-key-path				用于加密身份验证凭据的 RSA 公钥路径[KMG] 表示支持 K/M/G 后缀以表示千、兆或吉的选项。iperf3 主页: https://software.es.net/iperf/报告漏洞: https://github.com/esnet/iperf

三,例子

便于测试,服务端我一直开着

iperf3 -s

1、基础测试(最常用),用于测试默认的 TCP 带宽。

服务端命令:[root@master1 iperf-3.21]# iperf3 -s-----------------------------------------------------------Server listening on 5201 (test #1)-----------------------------------------------------------Accepted connection from 172.25.61.120, port 32073[  5] local 172.25.61.52 port 5201 connected to 172.25.61.120 port 32075[ ID] Interval           Transfer     Bitrate[  5]   0.00-1.00   sec  11.0 MBytes  92.2 Mbits/sec                  [  5]   1.00-2.00   sec  11.1 MBytes  93.3 Mbits/sec                  [  5]   2.00-3.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   3.00-4.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   4.00-5.00   sec  11.1 MBytes  93.3 Mbits/sec                  [  5]   5.00-6.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   6.00-7.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   7.00-8.00   sec  11.1 MBytes  93.3 Mbits/sec                  [  5]   8.00-9.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   9.00-10.00  sec  11.1 MBytes  93.3 Mbits/sec                  [  5]  10.00-10.03  sec   256 KBytes  75.2 Mbits/sec                  - - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval           Transfer     Bitrate[  5]   0.00-10.03  sec   112 MBytes  93.7 Mbits/sec                  receiver-----------------------------------------------------------Server listening on 5201 (test #2)-----------------------------------------------------------
输出含义
  • 第一阶段:连接建立

Server listening on 5201 (test #1)Accepted connection from 172.25.61.120, port 32073[  5] local 172.25.61.52 port 5201 connected to 172.25.61.120 port 32075

含义: 服务端正在监听 5201 端口(这是 iperf3 的默认端口)。客户端发起连接后,服务端接受了请求,并建立了 TCP 连接。

  • 第二阶段:实时传输过程

[ ID] Interval           Transfer     Bitrate[  5]   0.00-1.00   sec  11.0 MBytes  92.2 Mbits/sec...[  5]   9.00-10.00  sec  11.1 MBytes  93.3 Mbits/sec

含义: 这是测试过程中的实时汇报,默认每隔 1 秒汇报一次。

稳定性分析: 可以看到每一秒的带宽都在 92.2 ~ 94.4 Mbps 之间波动,非常平稳,没有剧烈的忽高忽低,说明网络链路质量不错。

  • 第三阶段:最终汇总

[ 5]   0.00-10.03  sec   112 MBytes  93.7 Mbits/sec                  receiver

含义: 这是整个 10 秒测试的总结。

receiver: 标记为 receiver 表示这是服务端视角收到的数据。这意味着数据流向是 从客户端上传到服务端。

  • 关键指标
指标
数值
说明
Bitrate (带宽)
93.7 Mbits/sec
网络吞吐量。这个数值通常受限于网卡速率(可能是百兆网卡)或网络限速策略source_group_web_6。
Transfer (流量)
112 MBytes
实际传输的数据量大小source_group_web_7。
Duration (时长)
10.03 sec
测试持续的时间,默认通常为 10 秒source_group_web_8。
Direction (方向)
接收 (Receiver)
数据流向:客户端 -> 服务端(即客户端的上传速度,服务端的下载速度)source_group_web_9。
客户端输入命令
[root@localhost iperf-3.21]# iperf3 -c 172.25.61.52Connecting to host 172.25.61.52, port 5201[  5] local 172.25.61.120 port 32075 connected to 172.25.61.52 port 5201[ ID] Interval           Transfer     Bitrate         Retr  Cwnd[  5]   0.00-1.00   sec  11.4 MBytes  95.3 Mbits/sec    0    170 KBytes       [  5]   1.00-2.00   sec  11.2 MBytes  94.4 Mbits/sec    0    197 KBytes       [  5]   2.00-3.00   sec  11.2 MBytes  94.4 Mbits/sec    0    211 KBytes       [  5]   3.00-4.00   sec  11.1 MBytes  93.4 Mbits/sec    0    245 KBytes       [  5]   4.00-5.00   sec  11.2 MBytes  94.3 Mbits/sec   11    198 KBytes       [  5]   5.00-6.00   sec  11.1 MBytes  93.3 Mbits/sec   12    160 KBytes       [  5]   6.00-7.00   sec  11.2 MBytes  94.4 Mbits/sec   12    136 KBytes       [  5]   7.00-8.00   sec  11.2 MBytes  94.3 Mbits/sec   23    127 KBytes       [  5]   8.00-9.00   sec  11.1 MBytes  93.3 Mbits/sec   24    116 KBytes       [  5]   9.00-10.00  sec  11.1 MBytes  93.3 Mbits/sec   34   94.7 KBytes       - - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval           Transfer     Bitrate         Retr[  5]   0.00-10.00  sec   112 MBytes  94.0 Mbits/sec  116            sender[  5]   0.00-10.03  sec   112 MBytes  93.7 Mbits/sec                  receiveriperf Done.
  • 第一部分:连接建立

[root@localhost iperf-3.21]# iperf3 -c 172.25.61.52Connecting to host 172.25.61.52, port 5201[  5] local 172.25.61.120 port 32075 connected to 172.25.61.52 port 5201

第1行:执行的命令。-c 表示以客户端模式运行,目标服务器 IP 是 172.25.61.52。

第2行:客户端正在尝试连接服务器的 5201 端口(iperf3 的默认端口)。

第3行:连接成功。

local 172.25.61.120 port 32075:这是本机(客户端)的 IP 和随机分配的端口。connected to ... port 5201:已成功连接到服务端。[ 5]:这是连接会话的 ID,后续所有数据都基于这个连接。
  • 第二部分:实时传输详情(核心数据)

这部分每秒输出一行,展示网络实时的表现。

[ ID] Interval           Transfer     Bitrate         Retr  Cwnd[  5]   0.00-1.00   sec  11.4 MBytes  95.3 Mbits/sec    0    170 KBytes...[  5]   9.00-10.00  sec  11.1 MBytes  93.3 Mbits/sec   34   94.7 KBytes

表头含义:

Interval:时间间隔。例如 0.00-1.00 sec 代表测试开始后的第 1 秒内。

Transfer:在该秒内传输的数据总量。

Bitrate:该秒内的平均带宽速度。

Retr (关键):重传次数。TCP 协议中,如果数据包丢失,发送端必须重发。数值越高,说明网络越拥塞或质量越差。

Cwnd (关键):拥塞窗口。这是 TCP 协议允许发送端在未收到确认前能发送的最大数据量。

数据解读:

前 4 秒:Retr 为 0,Bitrate 稳定在 94-95 Mbps。说明网络刚开始很顺畅。

第 5-10 秒:Retr 开始出现并逐渐增加(11, 12, 23, 34...),同时右侧的 Cwnd 数值开始下降(从 198K 降到 94.7K)。

原因:因为测试速度(约 94 Mbps)已经填满了百兆网络管道,导致交换机或路由器的缓冲区溢出,开始丢包。

结果:TCP 协议检测到丢包(Retr 增加),为了缓解拥塞,主动减小了发送窗口(Cwnd 减小),这是一种正常的自我保护机制。

  • 第三部分:最终统计汇总

[ ID] Interval           Transfer     Bitrate         Retr[  5]   0.00-10.00  sec   112 MBytes  94.0 Mbits/sec  116            sender[  5]   0.00-10.03  sec   112 MBytes  93.7 Mbits/sec                  receiver

这是整个 10 秒测试的总结:

sender (发送端视角):

Bitrate 94.0 Mbits/sec:本机发出的平均速度。Retr 116:在 10 秒内,总共发生了 116 次数据包重传。这再次印证了网络处于“满载且轻微拥塞”的状态。

receiver (接收端视角):

Bitrate 93.7 Mbits/sec:服务器实际收到的平均速度。

注意:接收端通常不显示 Retr,因为重传是由发送端统计的。

发送端 94.0 与 接收端 93.7 的差值,就是被网络设备丢弃的数据包量。

  • 第四部分:测试结束

iperf Done.

测试任务完成,程序退出。

总结论

速度达标:网络速度稳定在 94 Mbps 左右。瓶颈确认:这个速度精确地卡在 100Mbps 的物理极限上。说明网线、网卡或交换机中,至少有一个环节是百兆设备,限制了速度(如果是千兆网络,这里应该跑到 940 Mbps 左右)。网络状况:出现了 116 次重传,但这在“跑满带宽”的测速场景下是正常现象。它说明流量太大,把管道堵住了,导致少量丢包。

2、多线程并发测试(跑满带宽),场景:测试高带宽链路(如千兆局域网、云服务器带宽)

  • -P 8
    :开启 48个并行流(根据 CPU 核心数和带宽调整,如万兆可用 -P 16 或更高)。
  • -t 3
    :测试持续 3秒。

客户端输入输出[root@localhost iperf-3.21]# iperf3 -c 172.25.61.52  -P 8 -t 3Connecting to host 172.25.61.52, port 5201[  5] local 172.25.61.120 port 32091 connected to 172.25.61.52 port 5201[  7] local 172.25.61.120 port 32093 connected to 172.25.61.52 port 5201[  9] local 172.25.61.120 port 32095 connected to 172.25.61.52 port 5201[ 11] local 172.25.61.120 port 32097 connected to 172.25.61.52 port 5201[ 13] local 172.25.61.120 port 32099 connected to 172.25.61.52 port 5201[ 15] local 172.25.61.120 port 32101 connected to 172.25.61.52 port 5201[ 17] local 172.25.61.120 port 32103 connected to 172.25.61.52 port 5201[ 19] local 172.25.61.120 port 32105 connected to 172.25.61.52 port 5201[ ID] Interval           Transfer     Bitrate         Retr  Cwnd[  5]   0.00-1.00   sec  1.75 MBytes  14.7 Mbits/sec    0   31.1 KBytes       [  7]   0.00-1.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [  9]   0.00-1.00   sec  1.75 MBytes  14.7 Mbits/sec    0   33.9 KBytes       [ 11]   0.00-1.00   sec  1.50 MBytes  12.6 Mbits/sec    0   25.5 KBytes       [ 13]   0.00-1.00   sec  1.38 MBytes  11.5 Mbits/sec    0   25.5 KBytes       [ 15]   0.00-1.00   sec  1.38 MBytes  11.5 Mbits/sec    0   25.5 KBytes       [ 17]   0.00-1.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 19]   0.00-1.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [SUM]   0.00-1.00   sec  11.9 MBytes  99.5 Mbits/sec    0             - - - - - - - - - - - - - - - - - - - - - - - - -[  5]   1.00-2.00   sec  1.50 MBytes  12.6 Mbits/sec    0   33.9 KBytes       [  7]   1.00-2.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [  9]   1.00-2.00   sec  1.62 MBytes  13.6 Mbits/sec    0   33.9 KBytes       [ 11]   1.00-2.00   sec  1.25 MBytes  10.5 Mbits/sec    0   26.9 KBytes       [ 13]   1.00-2.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 15]   1.00-2.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 17]   1.00-2.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 19]   1.00-2.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [SUM]   1.00-2.00   sec  11.2 MBytes  94.4 Mbits/sec    0             - - - - - - - - - - - - - - - - - - - - - - - - -[  5]   2.00-3.00   sec  1.62 MBytes  13.6 Mbits/sec    0   35.4 KBytes       [  7]   2.00-3.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [  9]   2.00-3.00   sec  1.38 MBytes  11.5 Mbits/sec    0   33.9 KBytes       [ 11]   2.00-3.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 13]   2.00-3.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 15]   2.00-3.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 17]   2.00-3.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [ 19]   2.00-3.00   sec  1.38 MBytes  11.5 Mbits/sec    0   26.9 KBytes       [SUM]   2.00-3.00   sec  11.2 MBytes  94.3 Mbits/sec    0             - - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval           Transfer     Bitrate         Retr[  5]   0.00-3.00   sec  4.88 MBytes  13.6 Mbits/sec    0            sender[  5]   0.00-3.03   sec  4.75 MBytes  13.1 Mbits/sec                  receiver[  7]   0.00-3.00   sec  4.12 MBytes  11.5 Mbits/sec    0            sender[  7]   0.00-3.03   sec  4.00 MBytes  11.1 Mbits/sec                  receiver[  9]   0.00-3.00   sec  4.75 MBytes  13.3 Mbits/sec    0            sender[  9]   0.00-3.03   sec  4.50 MBytes  12.4 Mbits/sec                  receiver[ 11]   0.00-3.00   sec  4.12 MBytes  11.5 Mbits/sec    0            sender[ 11]   0.00-3.03   sec  4.00 MBytes  11.1 Mbits/sec                  receiver[ 13]   0.00-3.00   sec  4.12 MBytes  11.5 Mbits/sec    0            sender[ 13]   0.00-3.03   sec  4.00 MBytes  11.1 Mbits/sec                  receiver[ 15]   0.00-3.00   sec  4.12 MBytes  11.5 Mbits/sec    0            sender[ 15]   0.00-3.03   sec  4.00 MBytes  11.1 Mbits/sec                  receiver[ 17]   0.00-3.00   sec  4.12 MBytes  11.5 Mbits/sec    0            sender[ 17]   0.00-3.03   sec  4.00 MBytes  11.1 Mbits/sec                  receiver[ 19]   0.00-3.00   sec  4.12 MBytes  11.5 Mbits/sec    0            sender[ 19]   0.00-3.03   sec  4.00 MBytes  11.1 Mbits/sec                  receiver[SUM]   0.00-3.00   sec  34.4 MBytes  96.1 Mbits/sec    0             sender[SUM]   0.00-3.03   sec  33.2 MBytes  92.0 Mbits/sec                  receiveriperf Done.

输出解读

总带宽依然被锁死在 100Mbps:

尽管开了 8 个线程,但总速度(SUM行)依然稳定在 94~96 Mbps 左右。这再次铁证如山地证明网络链路中存在百兆物理瓶颈(如百兆交换机或网线)。

多线程“抢跑”成功,消除了重传:

与上次单线程测试不同,这次 Retr(重传)为 0。这说明使用多线程可以更平滑地利用带宽,避免了单线程TCP协议在探测带宽时因“冲得太快”而导致的丢包。

负载均衡非常均匀:

8 个线程(ID 5, 7, 9...19)每个都分到了约 11.5 Mbps 的带宽,大家“有饭一起吃”,非常公平。

详细解读

  • 连接建立阶段

[  5] local ... connected to ...[  7] local ... connected to ......[ 19] local ... connected to ...

这里列出了 8 个不同的连接 ID(5, 7, 9, 11, 13, 15, 17, 19),说明客户端成功向服务端建立了 8 条并行的 TCP 连接。

  • 实时传输阶段(每秒汇总)

[SUM]   0.00-1.00   sec  11.9 MBytes  99.5 Mbits/sec    0[SUM]   1.00-2.00   sec  11.2 MBytes  94.4 Mbits/sec    0[SUM]   2.00-3.00   sec  11.2 MBytes  94.3 Mbits/sec    0

[SUM] 行:这是所有 8 个线程加起来的总和。

Bitrate:第一秒冲到了 99.5 Mbps,后面稳定在 94.4 Mbps。

Retr 为 0:这是多线程的优势。多个线程同时传输,TCP 的拥塞控制机制(Cwnd)在多个连接上分散了,不像单线程那样容易因为一个连接爆发式发送而导致拥塞丢包。

  • 最终统计阶段

[SUM]   0.00-3.00   sec  34.4 MBytes  96.1 Mbits/sec    0             sender[SUM]   0.00-3.03   sec  33.2 MBytes  92.0 Mbits/sec                  receiver

Sender (发送端):平均速度 96.1 Mbps。

Receiver (接收端):平均速度 92.0 Mbps。

差异分析:发送端显示 96.1,接收端显示 92.0,中间差了约 4 Mbps。虽然没有重传(Retr=0),但接收端速度略低,可能是因为网络设备的缓冲区在满载时有少量丢弃,或者仅仅是统计误差,但在百兆网络满载的情况下,92-96 Mbps 都属于该链路的极限物理速度。

3、反向测试(测下载速度)

[root@localhost iperf-3.21]# iperf3 -c 172.25.61.52  -RConnecting to host 172.25.61.52, port 5201Reverse mode, remote host 172.25.61.52 is sending[  5] local 172.25.61.120 port 32109 connected to 172.25.61.52 port 5201[ ID] Interval           Transfer     Bitrate[  5]   0.00-1.00   sec  11.2 MBytes  94.3 Mbits/sec                  [  5]   1.00-2.00   sec  11.1 MBytes  93.3 Mbits/sec                  [  5]   2.00-3.00   sec  11.1 MBytes  93.3 Mbits/sec                  [  5]   3.00-4.00   sec  11.4 MBytes  95.4 Mbits/sec                  [  5]   4.00-5.00   sec  11.1 MBytes  93.3 Mbits/sec                  [  5]   5.00-6.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   6.00-7.00   sec  11.1 MBytes  93.3 Mbits/sec                  [  5]   7.00-8.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   8.00-9.00   sec  11.2 MBytes  94.4 Mbits/sec                  [  5]   9.00-10.00  sec  11.1 MBytes  93.3 Mbits/sec                  - - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval           Transfer     Bitrate         Retr[  5]   0.00-10.02  sec   112 MBytes  94.2 Mbits/sec  110            sender[  5]   0.00-10.00  sec   112 MBytes  93.9 Mbits/sec                  receiveriperf Done.

核心结论

下载速度也是 100Mbps:

测试结果显示平均带宽为 93.9 ~ 94.2 Mbps。这说明下载链路同样被限制在了百兆网络,与上传速度一致。

网络非常稳定:

每秒的速率都在 93.3 ~ 95.4 Mbps 之间微小波动,非常平稳,说明链路质量很好,没有忽快忽慢。

同样存在拥塞:

Retr(重传)次数为 110 次。这说明服务器在向全速发送数据时,也出现了“堵车”现象,导致数据包丢失并重传。

详细解读:

  • 模式确认

Reverse mode, remote host 172.25.61.52 is sending

上述说明了方向:数据流向反转了。现在是服务端(172.25.61.52)在拼命向(172.25.61.120)发送数据。

  • 实时传输过程

[  5]   0.00-1.00   sec  11.2 MBytes  94.3 Mbits/sec...[  5]   9.00-10.00  sec  11.1 MBytes  93.3 Mbits/sec

每一秒都稳定传输约 11.1~11.2 MBytes 的数据。

这个速度(~94 Mbps)是百兆以太网(100BASE-TX)的理论极限值(扣除协议头开销后)。

  • 3. 最终汇总

[  5]   0.00-10.02  sec   112 MBytes  94.2 Mbits/sec  110            sender[  5]   0.00-10.00  sec   112 MBytes  93.9 Mbits/sec                  receiver

sender (服务端):发送速度 94.2 Mbps,发生了 110 次重传。

注意:这里的重传是服务端统计的。说明服务端发得太快,电脑或者中间的交换机来不及接收,导致丢包,服务端只能重发。

receiver (本机):接收速度 93.9 Mbps。

4、UDP 测试(测丢包与抖动)

  • -u
    :使用 UDP 协议。
  • -b 100M
    :指定目标带宽为 100Mbps(UDP 测试必须指定带宽,否则默认只有 1Mbps)。
  • 关注指标
    :查看输出中的 Jitter(抖动)和 Lost/Total Datagrams(丢包率)。
    [root@localhost iperf-3.21]# iperf3 -c 172.25.61.52 -u -b 100M -t 20Connecting to host 172.25.61.52, port 5201[  5] local 172.25.61.120 port 34773 connected to 172.25.61.52 port 5201[ ID] Interval           Transfer     Bitrate         Total Datagrams[  5]   0.00-1.00   sec  11.5 MBytes  96.6 Mbits/sec  8352  [  5]   1.00-2.00   sec  11.4 MBytes  95.9 Mbits/sec  8278  [  5]   2.00-3.00   sec  11.3 MBytes  95.2 Mbits/sec  8219  [  5]   3.00-4.00   sec  11.4 MBytes  95.9 Mbits/sec  8279  [  5]   4.00-5.00   sec  11.4 MBytes  96.0 Mbits/sec  8279  [  5]   5.00-6.00   sec  11.3 MBytes  95.1 Mbits/sec  8217  [  5]   6.00-7.00   sec  11.4 MBytes  95.9 Mbits/sec  8279  [  5]   7.00-8.00   sec  11.4 MBytes  95.4 Mbits/sec  8227  [  5]   8.00-9.00   sec  11.4 MBytes  95.8 Mbits/sec  8271  [  5]   9.00-10.00  sec  11.4 MBytes  95.9 Mbits/sec  8279  [  5]  10.00-11.00  sec  11.3 MBytes  95.3 Mbits/sec  8219  [  5]  11.00-12.00  sec  11.4 MBytes  95.8 Mbits/sec  8279  [  5]  12.00-13.00  sec  11.4 MBytes  95.9 Mbits/sec  8278  [  5]  13.00-14.00  sec  11.3 MBytes  95.1 Mbits/sec  8219  [  5]  14.00-15.00  sec  11.4 MBytes  96.0 Mbits/sec  8279  [  5]  15.00-16.00  sec  11.4 MBytes  95.8 Mbits/sec  8279  [  5]  16.00-17.00  sec  11.3 MBytes  95.3 Mbits/sec  8218  [  5]  17.00-18.00  sec  11.4 MBytes  95.8 Mbits/sec  8279  [  5]  18.00-19.00  sec  11.3 MBytes  95.3 Mbits/sec  8219  [  5]  19.00-20.00  sec  11.4 MBytes  95.8 Mbits/sec  8279  - - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval           Transfer     Bitrate         Jitter    Lost/Total Datagrams[  5]   0.00-20.00  sec   228 MBytes  95.7 Mbits/sec  0.000 ms  0/165228 (0%)  sender[  5]   0.00-20.02  sec   228 MBytes  95.5 Mbits/sec  0.132 ms  130/165226 (0.079%)  receiveriperf Done.

详细日志拆解

  • 实时传输过程

[  5]   0.00-1.00   sec  11.5 MBytes  96.6 Mbits/sec  8352...[  5]  19.00-20.00  sec  11.4 MBytes  95.8 Mbits/sec  8279

每一秒都稳定发送约 8200-8300 个数据包。

带宽始终维持在 95~96 Mbps,没有大起大落,说明链路非常稳定。

  • 最终汇总

[  5]   0.00-20.00  sec   228 MBytes  95.7 Mbits/sec  0.000 ms  0/165228 (0%)  sender[  5]   0.00-20.02  sec   228 MBytes  95.5 Mbits/sec  0.132 ms  130/165226 (0.079%)  receiver

sender (发送端):它只负责发,不知道路上丢了包,所以它显示丢包为 0

receiver (接收端):它统计实际收到的情况,发现有 130 个包没收到,并计算出了 0.132 ms 的抖动。

5、高级参数调优(长肥网络)

[root@localhost iperf-3.21]# iperf3 -c 172.25.61.52 -w 256K -t 3Connecting to host 172.25.61.52, port 5201[  5] local 172.25.61.120 port 32119 connected to 172.25.61.52 port 5201[ ID] Interval           Transfer     Bitrate         Retr  Cwnd[  5]   0.00-1.00   sec  11.5 MBytes  96.4 Mbits/sec    0    180 KBytes       [  5]   1.00-2.00   sec  11.1 MBytes  93.3 Mbits/sec    0    180 KBytes       [  5]   2.00-3.00   sec  11.2 MBytes  94.3 Mbits/sec    0    189 KBytes       - - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval           Transfer     Bitrate         Retr[  5]   0.00-3.00   sec  33.9 MBytes  94.7 Mbits/sec    0            sender[  5]   0.00-3.03   sec  33.8 MBytes  93.4 Mbits/sec                  receiveriperf Done.

详细解读

  • 1. 命令含义

iperf3 -c 172.25.61.52 -w 256K -t 3

-w 256K:设置 TCP 套接字缓冲区大小为 256 KB。

知识点:TCP 窗口大小决定了在收到接收方的“确认信号”之前,发送方可以发送多少数据。公式通常是 带宽 × 延迟。在低延迟的局域网中,通常不需要这么大的窗口;但在高延迟的广域网中,大窗口是跑满带宽的关键。

  • 2. 实时数据传输

[  5]   0.00-1.00   sec  11.5 MBytes  96.4 Mbits/sec    0    180 KBytes...[  5]   2.00-3.00   sec  11.2 MBytes  94.3 Mbits/sec    0    189 KBytes

Bitrate:稳定在 94~96 Mbps。

Cwnd:稳定在 180K 左右。这说明 TCP 协议栈认为当前网络只需要 180KB 的“管道”就足够跑满 100Mbps 了。

3. 最终汇总

[  5]   0.00-3.00   sec  33.9 MBytes  94.7 Mbits/sec    0            sender[  5]   0.00-3.03   sec  33.8 MBytes  93.4 Mbits/sec                  receiver

Sender:发送了 94.7 Mbps,0 重传。

Receiver:接收了 93.4 Mbps。

差异:发送和接收之间有约 1.3 Mbps 的差值,这通常是协议开销(以太网头、IP头、TCP头)以及极少量的链路层丢包造成的。

6、自动化与数据记录

iperf3 -c 172.25.61.52 -J > result.json

关键数据段解读

  • 测试配置 (Start部分)

"reverse": 0:这是正向测试(本机 -> 服务器),测的是上传速度。"duration": 10:测试时长 10 秒。"sndbuf_actual": 32768:实际使用的发送缓冲区大小是 32KB。这解释了为什么之前设置 -w 256K 能消除重传(给了更大的缓冲空间),而默认的 32KB 在百兆链路上显得捉襟见肘。
  • 实时传输细节 (Intervals部分)

这是 JSON 文件中最长的部分,记录了每一秒的数据。我们可以看到明显的波动:0-4秒:bits_per_second: 约 95 Mbpsretransmits: 0snd_cwnd: 从 176KB 增加到 243KB状态:网络通畅,TCP 正在积极扩张窗口以抢占带宽。
4-5秒:retransmits: 12snd_cwnd: 从 243KB 降为 181KB状态:第一次丢包发生,TCP 开始减速。
6-7秒:retransmits: 35 (丢包加剧)snd_cwnd: 暴跌至 99KBrtt: 延迟从 7ms 增加到 12ms状态:网络极度拥堵,TCP 被迫大幅缩减发送窗口。
8-10秒:retransmits: 又出现了 35+35 次丢包snd_cwnd: 在 107KB 和 88KB 之间波动状态:网络处于“拥塞-恢复-再拥塞”的震荡状态。
  • 最终汇总 (End部分)

"sum_sent": 发送了 117.7 MB 数据,速度 94.1 Mbps。"sum_received": 对方只收到了 117.4 MB 数据,速度 93.7 Mbps。这 0.3MB 的差值就是那 117 个重传包造成的损耗。"sender_tcp_congestion": "cubic":使用的拥塞控制算法是 Linux 默认的 CUBIC。它喜欢激进地填满管道,这正是导致百兆瓶颈处发生丢包(重传)的原因。

总结与建议

(Sender)试图用越来越快的速度(增大窗口)去填满网络管道。当管道(百兆网线)被填满后,多余的数据包开始被丢弃(重传 117 次)。TCP 发现丢包后,立刻大幅缩减速率(窗口从 243K 降到 88K),等网络空闲一点再试探性加速,导致了锯齿状的波动。

  • 自动化分析,重点关注以下字段:

end.sum_received.bits_per_second:真实的接收带宽(约 93.7 Mbps)。end.sum_sent.retransmits:网络拥塞程度指标(117 次,数值越高网络越差)。end.cpu_utilization_percent:CPU 利用率极低(< 3%),说明性能瓶颈完全在网卡,不在 CPU。

四,补充

iperf(有时称为 iperf3)是对最初在 NLANR/DAST 开发的版本的重新设计。iperf3 是从头开始的全新实现,旨在拥有更小、更简洁的代码库,并提供可在其他程序中使用的库版本功能。iperf3 还包含其他工具(如 nuttcp 和 netperf)中存在但在原始 iperf 中缺失的一些功能。例如,这些功能包括零复制(zero-copy)模式和可选的 JSON 输出。请注意,iperf3 与原始 iperf 不向后兼容。

iperf3 的主要用途之一是作为 perfSONAR 网络测量系统(https://www.perfsonar.net/)的组件。它作为独立工具也非常有用,并被 ESnet 和其他科研网络(R&E networks)使用。它在通用网络社区中也被证明很有用,并已出现在各种商业产品中。

iperf3 的主要开发工作在 Ubuntu Linux、FreeBSD 和 macOS 上进行。目前,这些是唯一官方支持的平台,尽管有报告显示在 OpenBSD、NetBSD、Android、Solaris 和其他 Linux 发行版上也能成功运行。

iperf3 主要由 ESnet / 劳伦斯伯克利国家实验室 (Lawrence Berkeley National Laboratory) 开发。它是在三条款 BSD 许可证下发布的。

五,总结

文章内容比较多,不过也不用全部记住,主要记住以下几个参数用法就好

速查表:

参数
含义
典型值/示例
-c
客户端模式,后接服务器 IP
-c 192.168.1.1
-s
服务端模式
-s
-P
并行连接数(多线程)
-P 4
 (4线程)
-t
测试时长(秒)
-t 30
-R
反向测试(测下载)
-R
-u
UDP 模式
-u
-b
目标带宽(UDP 必选)
-b 50M
-w
TCP 窗口大小(调优用)
-w 256K
-J
JSON 格式输出
-J
-i
报告间隔(秒)
-i 1
 (每秒显示一次)

最后的最后(Last but not least),欢迎交流:

关注公众号留言,或者在下方直接留言:

最新文章

随机文章