用iPerf3测试网络性能

什么是 iPerf3 ?

iPerf3 是一种用于主动测量 IP 网络上可实现的最大带宽的工具。它支持调整与时序 (timing)、缓冲区 (buffers) 和协议 (TCP、UDP、SCTP with IPv4 & IPv6) 相关的各种参数。对于每个测试,它都会报告带宽 (bandwidth)、丢包 (loss) 和其他参数。

iPerf3 被广泛用于测试内网网速、网卡性能、内网穿透性能等。



iPerf3 和 iPerf2 的关系

iPerf3 不是 iPerf2 的升级版,事实上,二者没什么关系。iPerf3 和 iPerf2 的区别如下:

iPerf3 iPerf2
线程数 单线程 多线程
双工测试 不支持 支持
并行技术 基于进程 基于线程

其余的不同之处可参考这个网站:



配置环境

需要参与测试的两台设备都安装iPerf3。

安装方法如下(未列举的操作系统请自行搜索安装方法):

  1. 下载iPerf3程序包(由于iPerf3没有官方Windows版,你可以选择以下版本:
  2. 解压压缩包;
  3. 使用cmd打开文件夹所在目录(或将 iperf3.exe 和 cygwin1.dll 文件复制到 C:\Windows 路径下;或配置环境变量),输入 iperf3 –version 查看是否可以运行。
  • 对于基于Debian的系统(如Ubuntu):

    1
    2
    sudo apt-get update
    sudo apt-get install iperf3
  • 对于基于RedHat的系统(如CentOS):

    1
    2
    sudo yum update
    sudo yum install iperf3
  • 对于Fedora:

    1
    sudo dnf install iperf3
  • 对于Arch Linux:

    1
    sudo pacman -Sy iperf3
  • 安装完成后,使用运行iperf3命令来验证安装是否成功。

  1. 运行终端。
  2. 运行以下命令以安装Homebrew(如果尚未安装):
    1
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  3. 更新Homebrew的软件库:
    1
    brew update
  4. 安装iperf3:
    1
    brew install iperf3
  • 安装完成后,使用运行iperf3命令来验证安装是否成功。
  • 直接在OpenWrt软件包/Docker注册表里内搜索iperf3安装即可。


使用 iPerf3 测速

随便选择一个设备作为服务器端,另一个设备作为客户端,不影响接下来的测试。(如果你使用的是Windows 3.1.3版,则最好不要用Windows作为服务器端,否则可能会影响UDP测试)

下面是基本操作,可以满足90%的使用场景:

服务器端:

1
2
# 启动服务器端:
iperf3 -s

客户器端:
TCP上传测试

1
iperf3 -c [服务器端ip] -P [连接数]
1
iperf3 -c 192.168.1.21 -P 5

TCP下载测试

1
iperf3 -c [服务器端ip] -P [连接数] -R
1
iperf3 -c 192.168.1.21 -P 5 -R

UDP测试

1
iperf3 -c [服务器端ip] -u -b [大致的网络带宽,例如1000M]

Windows 3.1.3版有个bug:-u参数不能与-P共用,Linux服务器端则无此问题。

1
iperf3 -c 192.168.1.21 -u -b 2500M

完整的使用参数可通过iperf3 -h获取:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[root@localhost ]# iperf3 -h
Usage: iperf [-s|-c host] [options]
iperf [-h|--help] [-v|--version]

Server or Client:
-p, --port # server port to listen on/connect to
-f, --format [kmgKMG] format to report: Kbits, Mbits, KBytes, MBytes
-i, --interval # seconds between periodic bandwidth reports
-F, --file name xmit/recv the specified file
-A, --affinity n/n,m set CPU affinity
-B, --bind <host> bind to a specific interface
-V, --verbose more detailed output
-J, --json output in JSON format
--logfile f send output to a log file
--forceflush force flushing output at every interval
-d, --debug emit debugging output
-v, --version show version information and quit
-h, --help show this message and quit
Server specific:
-s, --server run in server mode
-D, --daemon run the server as a daemon
-I, --pidfile file write PID file
-1, --one-off handle one client connection then exit
Client specific:
-c, --client <host> run in client mode, connecting to <host>
-u, --udp use UDP rather than TCP
-b, --bandwidth #[KMG][/#] target bandwidth in bits/sec (0 for unlimited)
(default 1 Mbit/sec for UDP, unlimited for TCP)
(optional slash and packet count for burst mode)
--fq-rate #[KMG] enable fair-queuing based socket pacing in
bits/sec (Linux only)
-t, --time # time in seconds to transmit for (default 10 secs)
-n, --bytes #[KMG] number of bytes to transmit (instead of -t)
-k, --blockcount #[KMG] number of blocks (packets) to transmit (instead of -t or -n)
-l, --len #[KMG] length of buffer to read or write
(default 128 KB for TCP, dynamic or 1 for UDP)
--cport <port> bind to a specific client port (TCP and UDP, default: ephemeral port)
-P, --parallel # number of parallel client streams to run
-R, --reverse run in reverse mode (server sends, client receives)
-w, --window #[KMG] set window size / socket buffer size
-C, --congestion <algo> set TCP congestion control algorithm (Linux and FreeBSD only)
-M, --set-mss # set TCP/SCTP maximum segment size (MTU - 40 bytes)
-N, --no-delay set TCP/SCTP no delay, disabling Nagle's Algorithm
-4, --version4 only use IPv4
-6, --version6 only use IPv6
-S, --tos N set the IP 'type of service'
-L, --flowlabel N set the IPv6 flow label (only supported on Linux)
-Z, --zerocopy use a 'zero copy' method of sending data
-O, --omit N omit the first n seconds
-T, --title str prefix every output line with this string
--get-server-output get results from server
--udp-counters-64bit use 64-bit counters in UDP test packets
[KMG] indicates options that support a K/M/G suffix for kilo-, mega-, or giga-
iperf3 homepage at: http://software.es.net/iperf/
Report bugs to: https://github.com/esnet/iperf