iperf3使用方法详解

iperf3是一款带宽测试工具,它支持调节各种参数,比如通信协议,数据包个数,发送持续时间,测试完会报告网络带宽,丢包率和其他参数。

一、安装

操作系统:Ubuntu20.04LTS

打开终端,直接输入

sudo apt-get install iperf3

说明:不同的系统安装方式不同,这里只介绍ubuntu系统

二、使用方法

环境假设:

假设两台设备。

A(客户端)IP地址:192.168.3.95

B(服务端)IP地址:192.168.3.250

案例一:TCP通信测试

在B(服务端)设备启动shell窗口,输入指令

iperf -s -i 1
# 作为服务端运行,报告回显间隔时间1s

效果如下

TCP上行测试:在A(客户端)设备启动shell窗口,输入指令

iperf3 -c 192.168.3.250 -i 1 -t 10 -b 7M
#作为客户端,连接服务端ip地址192.168.3.250,报告回显间隔1s,测试时间10s,带宽限制为7M。

测试效果如下:

我们这里测试吞吐量的结果为receiver 7.02M左右,是因为我们限制了测试数据的带宽就是7M,说明实际的吞吐量应该是比这个大,测试过程中,如果发现测试的带宽与限制带宽接近,说明限制设置的太小了,需要适当调整-b参数,或者参考硬件厂家给的指标测试。

那么我们调整-b参数到70M,测试结果如下:

可以看出来我们实际TCP上行的速度测试为19.1M/sec,我们还可以增加测试时间来提高测试准确性。

这里还有一点是sender结果为0bits/sec,是因为我们只测试了上行数据,那么如何测试下行通信速度呢?

TCP下行测试:在A(客户端)设备启动shell窗口,输入指令

iperf3 -c 192.168.3.250 -i 1 -t 10 -b 7M -R
#作为客户端,连接服务端ip地址192.168.3.250,报告回显间隔1s,测试时间10s,带宽限制为7M,-R为反向测试,这个参数也是iperf3的主要亮点,支持直接转换数据发送方向

测试效果如下:

我们这里测试TCP下行吞吐量的结果为2.4M左右。

案例二:UDP测试

可以继续使用与案例一服务器,如果未打开,参考案例一的服务器打开方式,注意不要同时打开多个服务器

在B(服务端)设备启动shell窗口,输入指令

iperf -s -i 1
# 作为服务端运行,报告回显间隔时间1s,服务端不区分tcp或udp

效果如下:

UDP上行测试:在A(客户端)设备启动shell窗口,输入指令

iperf3 -u -c 192.168.3.250 -b 70M -i 1 -t 10
#作为客户端运行,限制带宽70M,报告回显间隔1s,测试时间10s

测试效果如下:

这里的数据发现吞吐量结果为0,应该是只显示了下行结果,所以我们看客户端的结果

我们这里测试出UDP上行吞吐量的结果为35.4M/s,延迟抖动(Jitter)为0.36ms,丢包率为12%。

测试过程中,如果发现测试的带宽与限制带宽接近,说明限制设置的太小了,需要适当调整-b参数,或者参考硬件厂家给的指标测试。

这里我们测试了UDP上行速度,那么UDP下行速度如何测试呢?

UDP下行测试:在A(客户端)设备启动shell窗口,输入指令

iperf3 -u -c 192.168.3.250 -b 70M -i 1 -t 10 -R
#作为客户端运行,限制带宽70M,报告回显间隔1s,测试时间10s

测试效果如下:

我们这里测试出UDP上行吞吐量的结果为47M/s,延迟抖动(Jitter)为0.347ms,丢包率为32%。这里要注意,-R是翻转发送者和接收者,所以统计结果都要从接收者这边来统计,否则延迟和丢包统计将没有意义。

详细解读

我们可以直接参考系统自带的帮助文档,如下:

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
  -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)
  -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, 8 KB 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
  --no-fq-socket-pacing     disable fair-queuing based socket pacing
                            (Linux only)

[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

我们抽取我在测试过程中用到的一些参数做一些翻译和解读

Client/Server: # 客户端和服务端公有的参数
指定端口号,默认为5201             
    -p, --port      #        server port to listen on/connect to 
回显报告的间隔时间                       
    -i, --interval  #        seconds between periodic bandwidth reports  
显示帮助菜单  
    -h, --help               print this message and quit   
显示版本
    -v, --version            print version information and quit    

Server specific:  #服务端私有参数
指定以服务端运行                                                            
    -s, --server             run in server mode                                               

Client specific:  #客户端私有参数
带宽参数,单位:字节每秒:KMG,为2的n次方,比如1K=1024,;设置为0代表无限制,此参数UDP默认1M/s,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) 
指定以客户端运行,后面要带服务端的IP地址                            
    -c, --client    <host>   run in client mode, connecting to <host>   
udp模式,不带-u默认为tcp模式
    -u, --udp                use UDP rather than TCP   
指定测试时间,不带参数默认测试10s        
    -t, --time      #        time in seconds to transmit for (default 10 secs)  
翻转测试,这是iperf3比iperf2方便的主要亮点,iperf2不支持此功能,无法使用   
    -R, --reverse            reverse the test (client receives, server sends)  
tcp窗口大小,默认无上限,可以不设此参数,作为udp模式测试时也不需要此参数 ,单位:KM,1K=1024     
    -w, --window    #[KMG]    set window size / socket buffer size  

·
·
·

欢迎各位老铁一键三连,本号后续会不断更新树莓派、人工智能、STM32、ROS小车相关文章和知识。

大家对感兴趣的知识点可以在文章下面留言,我可以优先帮大家讲解哦

欢迎大家光临我的淘宝小店【玩转智能机器人】,会定期推出教程中使用的物美价优的硬件,你的光临就是对我的支持

原创不易,转载请说明出处。