掘金 后端 ( ) • 2024-04-16 18:36

目录 [TOC]

网络PPS(每秒传输数据包数)、网络带宽和网络时延是衡量云服务器网络性能的重要指标,测试网络性能可以帮助您提高网络的稳定性、延迟和吞吐量,从而为您提供更好的用户体验和应用程序性能。

常用网络性能测试工具包括iperf,netperf,jperf(iperf的图形化版本),sockperf等。运行网络测试时,注意系统防火墙、安全组等影响,可以添加放行规则或者关闭防火墙后进行测试。

1. iperf(3)测试网络带宽和质量

iperf可用于测试两个计算机之间的网络带宽和延迟。它是一个开源项目,提供了用于Windows、Linux和其他操作系统的版本。通过在两台计算机之间运行iPerf服务器和客户端,您可以测量它们之间的网络性能。iPerf客户端发送数据流到服务器,并且服务器测量接收到的数据量和速度。支持在TCP和UDP协议上运行

iperf包括iperf2和iperf3两个版本。

官网 https://iperf.fr/

1.1 安装使用

# Ubuntu安装
$ sudo apt-get update
$ sudo apt-get install iperf
$ sudo apt-get install iperf3

# CentOS安装
$ sudo yum install epel-release
$ sudo yum install iperf
$ sudo yum install iperf3

1.1.1 参数说明

$ 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
 iperf常用参数中文说明:
(1)-s,–server:iperf服务器模式,默认启动的监听端口为5201,eg:iperf -s
(2)-c,–client host:iperf客户端模式,host是server端地址,eg:iperf -c 222.35.11.23
(3)-i,–interval:指定每次报告之间的时间间隔,单位为秒,eg:iperf3 -c 192.168.12.168 -i 2
(4)-p,–port:指定服务器端监听的端口或客户端所连接的端口,默认是5001端口。
(5)-u,–udp:表示采用UDP协议发送报文,不带该参数表示采用TCP协议。
(6)-l,–len:设置读写缓冲区的长度,(default 128 KB for TCP, dynamic or 1 for UDP)。通常测试 PPS 的时候该值为16,测试BPS时该值为1400。
(7)-b,–bandwidth [K|M|G]:指定UDP模式使用的带宽,单位bits/sec,默认值是1 Mb/s。
(8)-t,–time:指定数据传输的总时间,即在指定的时间内,重复发送指定长度的数据包。默认10秒。
(9)-A:CPU亲和性,可以将具体的iperf3进程绑定对应编号的逻辑CPU,避免iperf进程在不同的CPU间调度。

通用参数(Server端和Client端共用):
 (1)-f,–format [k|m|g|K|M|G]:指定带宽输出单位,“[k|m|g|K|M|G]”分别表示以Kb, Mb, Gb, KBytes, MBytes,GBytes显示输出结果,默认Mb,eg:iperf3 -c 192.168.12.168 -f M
(2)-p,–port:指定服务器端监听的端口或客户端所连接的端口,默认是5001端口。
(3)-i,–interval:指定每次报告之间的时间间隔,单位为秒,eg:iperf3 -c 192.168.12.168 -i 2
(4)-F:指定文件作为数据流进行带宽测试。例如:iperf3 -c 192.168.12.168 -F web-ixdba.tar.gz

Server端专用参数:
(1)-s,–server:iperf服务器模式,默认启动的监听端口为5201,eg:iperf -s
(2)-c,–client host:如果iperf运行在服务器模式,并且用-c参数指定一个主机,那么iperf将只接受指定主机的连接。此参数不能工作于UDP模式。
(3)-D:Unix平台下将Iperf作为后台守护进程运行。在Win32平台下,Iperf将作为服务运行。
(4)-R:卸载Iperf服务(仅用于Windows)。
(5)-o:重定向输出到指定文件(仅用于Windows)。
(6)-P,–parallel:服务器关闭之前保持的连接数。默认是0,这意味着永远接受连接。

Client端专用参数:
(1)-c,–client host:iperf客户端模式,host是server端地址,eg:iperf -c 222.35.11.23
(2)-u,–udp:表示采用UDP协议发送报文,不带该参数表示采用TCP协议。
(3)-b,–bandwidth [K|M|G]:指定UDP模式使用的带宽,单位bits/sec,默认值是1 Mbit/sec。
(4)-t,–time:指定数据传输的总时间,即在指定的时间内,重复发送指定长度的数据包。默认10秒。
(5)-l,–len:设置读写缓冲区的长度,(default 128 KB for TCP, dynamic or 1 for UDP)。通常测试 PPS 的时候该值为16,测试BPS时该值为1400。
(6)-n,–num [K|M|G]:指定传输数据包的字节数,例如:iperf3 -c 192.168.12.168 –n 100M
(7)-P,–parallel:指定客户端与服务端之间使用的线程数。默认是1个线程。需要客户端与服务器端同时使用此参数。
(8)-w,–window:指定套接字缓冲区大小,在TCP方式下,此设置为TCP窗口的大小。在UDP方式下,此设置为接受UDP数据包的缓冲区大小,用来限制可以接收数据包的最大值
(9)-B,–bind:用来绑定一个主机地址或接口,这个参数仅用于具有多个网络接口的主机。在UDP模式下,此参数用于绑定和加入一个多播组。
(10)-M,–mss:设置TCP最大信息段的值
(11)-N,–nodelay:设置TCP无延时
(12)-V:绑定一个IPv6地址。
(13)-d,–dualtest:运行双测试模式。将使服务器端反向连接到客户端,使用-L参数中指定的端口(或默认使用客户端连接到服务器端的端口)。使用参数-r以运行交互模式。
(14)-L,–listenport:指定服务端反向连接到客户端时使用的端口。默认使用客户端连接至服务端的端口。
(15)-r,–tradeoff:往复测试模式。当客户端到服务器端的测试结束时,服务器端反向连接至客户端。当客户端连接终止时,反向连接随即开始。如果需要同时进行双向测试,请尝试-d参数。

1.2 测试

准备两台机器,网络可达,分别安装iperf。注意添加系统防火墙规则或者关闭防火墙。如果是云环境,添加对应的安全组规则、防护墙规则放行。

1.2.1 测试TCP吞吐量

# Server端开启iperf的服务器模式,指定TCP端口:
$ iperf3 -s -i 1 -p 10000
-----------------------------------------------------------
Server listening on 10000
-----------------------------------------------------------

# Client端启动iperf的客户端模式,连接服务端。ip为server端地址
$ iperf3 -c 172.16.0.9 -i 1 -t 60 -p 10000
Connecting to host 172.16.0.9, port 10000
[  4] local 172.16.0.2 port 42756 connected to 172.16.0.9 port 10000
[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
[  4]   0.00-1.00   sec  68.0 MBytes   570 Mbits/sec  3298   4.24 KBytes
[  4]   1.00-2.00   sec  48.1 MBytes   404 Mbits/sec  2077   4.24 KBytes
...
[  4]  57.00-58.00  sec  48.1 MBytes   403 Mbits/sec  2046   5.66 KBytes
[  4]  58.00-59.00  sec  49.1 MBytes   412 Mbits/sec  1990   4.24 KBytes
[  4]  59.00-60.00  sec  40.1 MBytes   336 Mbits/sec  1653   11.3 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-60.00  sec  2.85 GBytes   408 Mbits/sec  126933             sender
[  4]   0.00-60.00  sec  2.85 GBytes   407 Mbits/sec                  receiver

iperf Done.


# 测试4线程TCP吞吐量,如果没有指定发送方式,iperf客户端只会使用单线程。
$ iperf3 -c 172.16.0.9 -i 1 -P 4 -t 60 -p 10000
Connecting to host 172.16.0.9, port 10000
[  4] local 172.16.0.2 port 42764 connected to 172.16.0.9 port 10000
[  6] local 172.16.0.2 port 42766 connected to 172.16.0.9 port 10000
[  8] local 172.16.0.2 port 42768 connected to 172.16.0.9 port 10000
[ 10] local 172.16.0.2 port 42770 connected to 172.16.0.9 port 10000
[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
[  4]   0.00-1.00   sec  29.5 MBytes   247 Mbits/sec  1905   4.24 KBytes
[  6]   0.00-1.00   sec  17.0 MBytes   143 Mbits/sec  249   63.6 KBytes
[  8]   0.00-1.00   sec  13.6 MBytes   114 Mbits/sec  676   2.83 KBytes
[ 10]   0.00-1.00   sec  7.88 MBytes  66.1 Mbits/sec  462   2.83 KBytes
...
[SUM]  59.00-60.00  sec  43.8 MBytes   368 Mbits/sec  2866
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-60.00  sec   836 MBytes   117 Mbits/sec  49435             sender
[  4]   0.00-60.00  sec   834 MBytes   117 Mbits/sec                  receiver
[  6]   0.00-60.00  sec   834 MBytes   117 Mbits/sec  53454             sender
[  6]   0.00-60.00  sec   833 MBytes   116 Mbits/sec                  receiver
[  8]   0.00-60.00  sec   627 MBytes  87.7 Mbits/sec  38115             sender
[  8]   0.00-60.00  sec   626 MBytes  87.6 Mbits/sec                  receiver
[ 10]   0.00-60.00  sec   618 MBytes  86.4 Mbits/sec  39273             sender
[ 10]   0.00-60.00  sec   617 MBytes  86.3 Mbits/sec                  receiver
[SUM]   0.00-60.00  sec  2.85 GBytes   408 Mbits/sec  180277             sender
[SUM]   0.00-60.00  sec  2.84 GBytes   407 Mbits/sec                  receiver

iperf Done.


# 进行上下行带宽测试(双向传输)
$ iperf3 -c 172.16.0.9 -i 1 -d -t 60 -p 10000
send_parameters:
{
        "tcp":  true,
        "omit": 0,
        "time": 60,
        "parallel":     1,
        "len":  131072,
        "client_version":       "3.1.7"
}
Connecting to host 172.16.0.9, port 10000
SNDBUF is 16384, expecting 0
RCVBUF is 87380, expecting 0
Congestion algorithm is cubic
[  4] local 172.16.0.2 port 42774 connected to 172.16.0.9 port 10000
tcpi_snd_cwnd 3 tcpi_snd_mss 1448 tcpi_rtt 96
[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
[  4]   0.00-1.00   sec  67.6 MBytes   567 Mbits/sec  2708   4.24 KBytes
tcpi_snd_cwnd 2 tcpi_snd_mss 1448 tcpi_rtt 36
...
[  4]  58.00-59.00  sec  48.7 MBytes   408 Mbits/sec  2095   5.66 KBytes
tcpi_snd_cwnd 3 tcpi_snd_mss 1448 tcpi_rtt 104
send_results
{
        "cpu_util_total":       2.346862,
        "cpu_util_user":        0.094838,
        "cpu_util_system":      2.270644,
        "sender_has_retransmits":       1,
        "congestion_used":      "cubic",
        "streams":      [{
                        "id":   1,
                        "bytes":        3065514464,
                        "retransmits":  126989,
                        "jitter":       0,
                        "errors":       0,
                        "packets":      0
                }]
}
get_results
{
        "cpu_util_total":       5.909981,
        "cpu_util_user":        1.167991,
        "cpu_util_system":      4.745647,
        "sender_has_retransmits":       -1,
        "congestion_used":      "cubic",
        "streams":      [{
                        "id":   1,
                        "bytes":        3064309728,
                        "retransmits":  -1,
                        "jitter":       0,
                        "errors":       0,
                        "packets":      0
                }]
}
[  4]  59.00-60.00  sec  48.4 MBytes   406 Mbits/sec  1757   4.24 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-60.00  sec  2.85 GBytes   409 Mbits/sec  126989             sender
[  4]   0.00-60.00  sec  2.85 GBytes   409 Mbits/sec                  receiver

iperf Done.

结果说明:

  • Interval表示时间间隔。
  • Transfer表示时间间隔里面转输的数据量。
  • Bandwidth是时间间隔里的传输速率。

1.2.2 测试UDP吞吐量

带宽测试通常采用UDP模式,因为能测出极限带宽、时延抖动、丢包率。在进行测试时,首先以链路理论带宽作为数据发送速率进行测试,例如,从客户端到服务器之间的链路的理论带宽为100Mbps,先用-b 100M进行测试,然后根据测试结果(包括实际带宽,时延抖动和丢包率),再以实际带宽作为数据发送速率进行测试,会发现时延抖动和丢包率比第一次好很多,重复测试几次,就能得出稳定的实际带宽。

# Server端开启iperf的服务器模式,指定UDP端口:
$ iperf3 -s -i 1 -p 10001
-----------------------------------------------------------
Server listening on 10001
-----------------------------------------------------------

# Client端启动iperf的客户端模式,连接服务端
$ iperf3 -u -c 172.16.0.9 -b 100m -t 60 -p 10001
Connecting to host 172.16.0.9, port 10001
[  4] local 172.16.0.2 port 46011 connected to 172.16.0.9 port 10001
[ ID] Interval           Transfer     Bandwidth       Total Datagrams
[  4]   0.00-1.00   sec  10.8 MBytes  90.3 Mbits/sec  7799
...
[  4]  58.00-59.00  sec  11.9 MBytes   100 Mbits/sec  8640
[  4]  59.00-60.00  sec  11.9 MBytes  99.9 Mbits/sec  8625
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Jitter    Lost/Total Datagrams
[  4]   0.00-60.00  sec   714 MBytes  99.8 Mbits/sec  0.001 ms  133914/517118 (26%)
[  4] Sent 517118 datagrams

iperf Done.

# 测试多线程UDP吞吐量
$ iperf3 -u -c 172.16.0.9 -b 5m -P 4 -t 60 -p 10001

# 进行上下行带宽测试(双向传输)
$ iperf3 -u -c 172.16.0.9 -b 100M -d -t 60 -p 10001

结果说明:

  • jitter为抖动,在连续传输中的平滑平均值差。
  • Lost为丢包数量。
  • Total Datagrams为包数量。

说明: 在网络性能测试中,"jitter"(抖动)是指网络数据包到达目的地的时间之间的变化量。换句话说,它是网络延迟的变化量,通常以毫秒(ms)为单位。抖动表示数据包到达的不稳定性,即使平均延迟很低,高抖动也可能导致网络连接出现问题。 在iPerf中,当执行UDP测试时,抖动是一个重要的指标之一,它能帮助评估网络连接的质量。UDP测试通常会更加关注抖动,因为UDP协议本身不提供丢失数据包的重新传输机制,而且应用程序可能对数据到达的时间敏感。 通过测量抖动,您可以了解到数据包到达目的地的时间变化范围。较低的抖动表示网络连接的稳定性较高,而较高的抖动可能会导致数据包的丢失或延迟增加。 因此,在进行网络性能测试时,特别是对于实时或延迟敏感的应用程序(如VoIP或视频流),监测和评估抖动是非常重要的。

2. netperf测试网络带宽和PPS

Netperf是一种网络性能的测量工具(由惠普公司开发的,测试网络栈。即测试不同类型的网络性能的benchmark工具)主要针对基于TCP或UDP的传输。Netperf根据应用的不同,可以进行不同模式的网络性能测试,即批量数据传输(bulk data transfer)模式和请求/应答(request/reponse)模式。Netperf测试结果所反映的是一个系统能够以多快的速度向另外一个系统发送数据,以及另外一个系统能够以多块的速度接收数据。

netperf官网: http://www.netperf.org/netperf/ https://github.com/HewlettPackard/netperf

2.1 安装

准备至少两台机器,分别安装netperf。

# 源码编译安装
# ubuntu安装依赖
$ sudo apt-get install automake texinfo

$ wget -O netperf-2.7.0.tar.gz -c  https://codeload.github.com/HewlettPackard/netperf/tar.gz/netperf-2.7.0
$ tar xf netperf-2.7.0.tar.gz
$ cd netperf-netperf-2.7.0/
# 编译
$ ./autogen.sh && ./configure
# 安装
$ sudo make && sudo make install

2.2 参数说明

# 查看帮助信息。更多帮助信息通过man netserver/netperf查看

$ netserver -h

Usage: netserver [options]

Options:
    -h                Display this text
    -D                Do not daemonize
    -d                Increase debugging output
    -f                Do not spawn chilren for each test, run serially
    -L name,family    Use name to pick listen address and family for family
    -N                No debugging output, even if netperf asks
    -p portnum        Listen for connect requests on portnum.
    -4                Do IPv4
    -6                Do IPv6
    -v verbosity      Specify the verbosity level
    -V                Display version information and exit
    -Z passphrase     Expect passphrase as the first thing received

$ netperf -h

Usage: netperf [global options] -- [test options]

Global options:
    -a send,recv      Set the local send,recv buffer alignment
    -A send,recv      Set the remote send,recv buffer alignment
    -B brandstr       Specify a string to be emitted with brief output
    -c [cpu_rate]     Report local CPU usage
    -C [cpu_rate]     Report remote CPU usage
    -d                Increase debugging output
    -D time,[units] * Display interim results at least every time interval
                      using units as the initial guess for units per second
                      A negative value for time will make heavy use of the
                      system's timestamping functionality
    -f G|M|K|g|m|k    Set the output units
    -F lfill[,rfill]* Pre-fill buffers with data from specified file
    -h                Display this text
    -H name|ip,fam *  Specify the target machine and/or local ip and family
    -i max,min        Specify the max and min number of iterations (15,1)
    -I lvl[,intvl]    Specify confidence level (95 or 99) (99)
                      and confidence interval in percentage (10)
    -j                Keep additional timing statistics
    -l testlen        Specify test duration (>0 secs) (<0 bytes|trans)
    -L name|ip,fam *  Specify the local ip|name and address family
    -o send,recv      Set the local send,recv buffer offsets
    -O send,recv      Set the remote send,recv buffer offset
    -n numcpu         Set the number of processors for CPU util
    -N                Establish no control connection, do 'send' side only
    -p port,lport*    Specify netserver port number and/or local port
    -P 0|1            Don't/Do display test headers
    -r                Allow confidence to be hit on result only
    -s seconds        Wait seconds between test setup and test start
    -S                Set SO_KEEPALIVE on the data connection
    -t testname       Specify test to perform
    -T lcpu,rcpu      Request netperf/netserver be bound to local/remote cpu
    -v verbosity      Specify the verbosity level
    -W send,recv      Set the number of send,recv buffers
    -v level          Set the verbosity level (default 1, min 0)
    -V                Display the netperf version and exit
    -y local,remote   Set the socket priority
    -Y local,remote   Set the IP_TOS. Use hexadecimal.
    -Z passphrase     Set and pass to netserver a passphrase

2.3 测试场景及指标说明

2.3.1 测试模式

  • TCP_STREAM:client端向server端发送批量TCP数据。
  • UDP_STREAM:client端向server端发送批量UDP数据。
  • TCP_RR和TCP_CRR:前者是在同一个连接中进行多次request和response请求(并发),后者(新建)是每次请求新建一个连接(HTTP)。
  • UDP_RR:使用UDP进行request和response请求。

2.3.2 测试场景及性能指标

指标监控

sar命令监控网络指标,运行sar -n DEV 1如下:

# 安装sar命令,CentOS
$ sudo yum install sysstat
# 安装sar命令,Ubuntu
$ sudo apt-get install sysstat
# 监控网络性能指标
$ sar -n DEV 1
Linux 3.10.0-1160.21.1.el7.x86_64 (i-HKDvtZ7t2)         03/22/2024      _x86_64_        (4 CPU)

11:30:11 AM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
11:30:12 AM      eth0 646796.00      2.00  27160.40      0.39      0.00      0.00      0.00
11:30:12 AM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

字段解释:

  • rxpck:每秒收包数,单位pps
  • txpck:每秒发包数,单位pps
  • rxkB/s:接收带宽
  • txkB:发送带宽
测试场景及性能指标

测试场景:

测试场景 客户端运行命令命令 SAR监控指标 UDP 64 netperf -t UDP_STREAM -H <server ip> -l 100 -- -m 64 -R 1 & PPS TCP 1440 netperf -t TCP_STREAM -H <server ip> -l 100 -- -m 1440 -R 1 & 带宽 TCP RR netperf -t TCP_RR -H <server ip> -l 100 -- -r 32,128 -R 1 & PPS

性能指标:

指标 说明 1440字节 TCP 收发带宽(Mbits/秒) 表示通过 TCP 进行批量数据传输时的数据传输吞吐量,能反映网络极限带宽能力(可能会存在丢包)。 64字节 UDP 收发 PPS(包/秒) 表示通过 UDP 进行批量数据传输时的数据传输吞吐量,能反映网络极限转发能力(可能会存在丢包)。 TCP-RR(次/秒) 表示在 TCP 长链接中反复进行 Request/Response 操作的交易吞吐量,能反映 TCP 不丢包网络转发能力。

2.4 操作步骤

TCP带宽测试可以采用多流模型:

  • 当测试TCP发送带宽时,采用一对多模型,即保证接收端能力足够。
  • 当测试TCP接收带宽时,采用多对一模型,即保证发送端能力足够。

2.4.1 测试发包性能

以下测试为1台客户端,1台服务端。为了测试出最大发包性能,可以增加服务端(增加netserver进程或者机器)。

# server端
$ pkill netserver && pkill netperf
$ netserver
Starting netserver with host 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC

# client端根据测试场景执行对应命令。客户端可以增减netperf进程,直到客户端发包性能不再增加。
# UDP 64测试发包pps。测试时长100s,发送测试分组的大小为64,也可设置为1进行测试。
$ netperf -t UDP_STREAM -H 172.16.0.9 -l 100 -- -m 64 -R 1 &
# TCP 1440测试发包带宽
$ netperf -t TCP_STREAM -H 172.16.0.9 -l 100 -- -m 1440 -R 1 &
# TCP_RR测试TCP不丢包转发能力,记录pps
$ netperf -t TCP_RR -H 172.16.0.9 -l 100 -- -r 32,128 -R 1 &     

# 在客户端执行sar命令,根据测试的场景记录对应的指标
$ sar -n DEV 1
Linux 3.10.0-1127.19.1.el7.x86_64 (i-uCoeT1mjZ)         03/22/2024      _x86_64_        (2 CPU)

12:25:01 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
12:25:02 PM      eth0  11444.00  11444.00   1095.23   1095.23      0.00      0.00      0.00
12:25:02 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

2.4.2 测试收包性能

以下测试为1台客户端,1台服务端。为了测试出最大收包性能,可以增加客户端(增加netperf进程或者机器)。

# server端
$ pkill netserver && pkill netperf
$ netserver
Starting netserver with host 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC

# client端根据测试场景执行对应命令。客户端可以增减netperf进程,指导服务端收包性能不再增加。
# UDP 64测试发包pps。测试时长100s,发送测试分组的大小为64,也可设置为1进行测试。
$ netperf -t UDP_STREAM -H 172.16.0.9 -l 100 -- -m 64 -R 1 &
# TCP 1440测试发包带宽
$ netperf -t TCP_STREAM -H 172.16.0.9 -l 100 -- -m 1440 -R 1 &
# TCP_RR测试TCP不丢包转发能力,记录pps
$ netperf -t TCP_RR -H 172.16.0.9 -l 100 -- -r 32,128 -R 1 &     

# 在客户端执行sar命令,根据测试的场景记录对应的指标
$ sar -n DEV 1
Linux 3.10.0-1127.19.1.el7.x86_64 (i-uCoeT1mjZ)         03/22/2024      _x86_64_        (2 CPU)

12:25:01 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
12:25:02 PM      eth0  11444.00  11444.00   1095.23   1095.23      0.00      0.00      0.00
12:25:02 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

客户端启用多个netperf的脚本:

#!/bin/bash
count=$1
for ((i=1;i<=count;i++))
do
    echo "Instance:$i-------"
    # -H 后填写服务器 IP 地址;
    # -l 后为测试时间,为了防止 netperf 提前结束,因此时间设为 10000;
    netperf -t UDP_STREAM -H <server ip> -l 10000 -- -m 64 -R 1 &
done

参考资料: 阿里云ECS网络性能测试最佳实践 华为云网络性能测试方法 腾讯云netperf测试云服务器网络性能

3. socketperf测试网络带宽和时延

sockperf是基于套接字API的网络基准测试实用程序,旨在测试高性能系统的性能(延迟和吞吐量)。下面使用socketperf测试网络时延。

3.1 安装

# CentOS镜像源安装
$ sudo yum install sockperf
$ sudo apt-get install sockperf

# 通过源码安装
# Ubuntu安装依赖
$ sudo apt-get install libtool
# CentOS安装依赖
$ sudo yum install -y autoconf automake libtool gcc-c++

$ wget https://github.com/Mellanox/sockperf/archive/refs/tags/3.8.tar.gz
$ tar xf 3.8.tar.gz
$ cd sockperf-3.8/
$ sudo ./autogen.sh
$ sudo ./configure
$ sudo make && sudo make install

命令解析:

sockperf -h
sockperf is a tool for testing network latency and throughput.
version 3.8-no.git

Usage: sockperf <subcommand> [options] [args]
Type: 'sockperf <subcommand> --help' for help on a specific subcommand.
Type: 'sockperf --version' to see the program version number.

Available subcommands:
   help (h ,?)          Display list of supported commands.
   under-load (ul)      Run sockperf client for latency under load test.
   ping-pong (pp)       Run sockperf client for latency test in ping pong mode.
   playback (pb)        Run sockperf client for latency test using playback of predefined traffic, based on timeline and message size.
   throughput (tp)      Run sockperf client for one way throughput test.
   server (sr)          Run sockperf as a server.

For additional information visit our website http://github.com/mellanox/sockperf , see README file, or Type 'sockperf <subcommand> --help'.

通过man sockperf可以查看更详细的介绍。

3.2 测试网络带宽

# 测试机1运行如下命令启动服务
$ sockperf sr --tcp --daemonize

# 测试机2运行
$ sockperf tp -i 172.16.0.9 --tcp -t 30 -m 1440 --full-log=result.json
sockperf: == version #3.6-no.git ==
sockperf[CLIENT] send on:
[ 0] IP = 172.16.0.9      PORT = 11111 # TCP
sockperf: Warmup stage (sending a few dummy messages)...
sockperf: Starting test...
sockperf: Test end (interrupted by timer)
sockperf: Test ended
sockperf: Total of 1580358 messages sent in 30.000 sec

sockperf: NOTE: test was performed, using msg-size=1440. For getting maximum throughput consider using --msg-size=1472
sockperf: Summary: Message Rate is 52678 [msg/sec]
sockperf: Summary: BandWidth is 72.342 MBps (578.738 Mbps)

3.3 测试网络时延

准备两台机器,网络可达,分别安装sockperf。

# 测试机1运行如下命令启动服务
$ sockperf sr --tcp --daemonize

# 测试机2运行
$ sockperf pp -i 172.16.0.9 --tcp -t 30 -m 14 --full-log=result.json
sockperf: == version #3.8-no.git ==
sockperf[CLIENT] send on:sockperf: using recvfrom() to block on socket(s)

[ 0] IP = 172.16.0.9      PORT = 11111 # TCP
sockperf: Warmup stage (sending a few dummy messages)...
sockperf: Starting test...
sockperf: Test end (interrupted by timer)
sockperf: Test ended
sockperf: [Total Run] RunTime=30.000 sec; Warm up time=400 msec; SentMessages=379885; ReceivedMessages=379884
sockperf: ========= Printing statistics for Server No: 0
sockperf: [Valid Duration] RunTime=29.550 sec; SentMessages=373920; ReceivedMessages=373920
sockperf: ====> avg-latency=39.491 (std-dev=18.407, mean-ad=4.211, median-ad=3.914, siqr=2.671, cv=0.466, std-error=0.030, 99.0% ci=[39.413, 39.569])
sockperf: # dropped messages = 0; # duplicated messages = 0; # out-of-order messages = 0
sockperf: Summary: Latency is 39.491 usec
sockperf: Total 373920 observations; each percentile contains 3739.20 observations
sockperf: ---> <MAX> observation = 4097.089
sockperf: ---> percentile 99.999 = 1990.656
sockperf: ---> percentile 99.990 =  744.546
sockperf: ---> percentile 99.900 =  130.858
sockperf: ---> percentile 99.000 =   64.290
sockperf: ---> percentile 90.000 =   44.510
sockperf: ---> percentile 75.000 =   41.040
sockperf: ---> percentile 50.000 =   38.170
sockperf: ---> percentile 25.000 =   35.697
sockperf: ---> <MIN> observation =   27.761

3.4 结果分析

查看上述结果数据,示例如下图所示。

  • 以avg-latency开头的结果数据表示平均时延,单位为us。

  • 以percentile 99.000开头的结果数据表示99分位时延,单位为us。

参考资料: 阿里云ECS网络性能测试最佳实践

本文由mdnice多平台发布