网络测试之iperf3

概述

简介

iperf3
The iperf series of tools perform active measurements to determine the maximum achievable bandwidth on IP networks. It supports tuning of various parameters related to timing, protocols, and buffers. For each test it reports the measured throughput, loss, and other parameters.

This version, sometimes referred to as iperf3, is a redesign of an original version developed at NLANR / DAST. iperf3 is a new implementation from scratch, with the goal of a smaller, simpler code base, and a library version of the functionality that can be used in other programs. iperf3 also incorporates a number of features found in other tools such as nuttcp and netperf, but were missing from the original iperf. These include, for example, a zero-copy mode and optional JSON output. Note that iperf3 is not backwards compatible with the original iperf.

iPerf 系列工具通过主动测量来确定 IP 网络上可实现的最大带宽。它支持调整与时间、协议和缓冲区相关的各种参数。每次测试都会报告测得的吞吐量、丢包率及其他相关参数。

官网主页:https://software.es.net/iperf/
源码仓库:https://github.com/esnet/iperf

帮助说明

帮助说明(以iperf3 v3.7版本为例):

yyy@xxx:~$ iperf3 -h
Usage: iperf3 [-s|-c host] [options]
       iperf3 [-h|--help] [-v|--version]

Server or Client:
  -p, --port      #         server port to listen on/connect to
  -f, --format   [kmgtKMGT] format to report: Kbits, Mbits, Gbits, Tbits
  -i, --interval  #         seconds between periodic throughput reports
  -F, --file name           xmit/recv the specified file
  -A, --affinity n/n,m      set CPU affinity
  -B, --bind      <host>    bind to the interface associated with the address <host>
  -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
  --rsa-private-key-path    path to the RSA private key used to decrypt
                            authentication credentials
  --authorized-users-path   path to the configuration file containing user
                            credentials
Client specific:
  -c, --client    <host>    run in client mode, connecting to <host>
  --sctp                    use SCTP rather than TCP
  -X, --xbind <name>        bind SCTP association to links
  --nstreams      #         number of SCTP streams
  -u, --udp                 use UDP rather than TCP
  --connect-timeout #       timeout for control connection setup (ms)
  -b, --bitrate #[KMG][/#]  target bitrate in bits/sec (0 for unlimited)
                            (default 1 Mbit/sec for UDP, unlimited for TCP)
                            (optional slash and packet count for burst mode)
  --pacing-timer #[KMG]     set the timing for pacing, in microseconds (default 1000)
  --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, --length    #[KMG]    length of buffer to read or write
                            (default 128 KB for TCP, dynamic or 1460 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)
  --bidir                   run in bidirectional mode.
                            Client and server send and receive data.
  -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, 0-255.
                            The usual prefixes for octal and hex can be used,
                            i.e. 52, 064 and 0x34 all specify the same value.
  --dscp N or --dscp val    set the IP dscp value, either 0-63 or symbolic.
                            Numeric values can be specified in decimal,
                            octal and hex (see --tos above).
  -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
  --extra-data str          data string to include in client and server JSON
  --get-server-output       get results from server
  --udp-counters-64bit      use 64-bit counters in UDP test packets
  --repeating-payload       use repeating pattern in payload, instead of
                            randomized payload (like in iperf2)
  --username                username for authentication
  --rsa-public-key-path     path to the RSA public key used to encrypt
                            authentication credentials

[KMG] indicates options that support a K/M/G suffix for kilo-, mega-, or giga-

iperf3 homepage at: https://software.es.net/iperf/
Report bugs to:     https://github.com/esnet/iperf

简单基本使用

iperf3是标准的C/S架构,测试时,需要一台机器跑服务端,一台机器跑客户端,都是同一个命令iperf3,只是参数不一样。

服务端

#启动 iPerf3 作为服务端监听连接
iperf3 -s

#默认情况下,它会监听 TCP 端口 5201,指定其他端口,使用 -p 参数
iperf3 -s -p <端口>

客户端

#默认5201端口,-p 指定端口,默认使用tcp测试
iperf3 -c <服务端IP> -p <端口>

#-u 使用udp测试,-b 设置目标比特率,-t 设置测试时长(s),-i 报告间隔(s),-l 指定包长
iperf3 -u -c <服务端IP> -p <端口> -b 300M -t 3000 -i 1 -l 1200

注意:需放行防火墙,或其他网络规则!

客户端高级参数

针对tcp测试:

iperf3 -c <server_ip> \
  --time 7200 \          # 测试时长(2小时)
  --parallel 4 \         # 多流测试,避免单流问题
  --window 256K \        # 增大窗口,应对高延迟
  --set-mss 1400 \       # 避免分片
  --fq-rate 90% \        # 启用 Linux 的 FQ 拥塞控制,更适合 WiFi 环境
  --omit 10 \            # 前10秒不计入结果
  --json \               # JSON 格式输出,便于分析
  --logfile iperf3_wifi.log

客户端其他重要参数:

参数 作用
--bidir 客户端同时测试上下行
-Z 启用内核零拷贝提升性能(Linux)