Ubuntu 22.04 netperf 安装与命令参数详解
在 Ubuntu 22.04 上安装 netperf 2.7.0(含 netserver)并逐参数实测吞吐/时延/CPU 命令输入与回显,涵盖 netserver 启动、-H/-l/-t(TCP_STREAM/UDP_STREAM/TCP_RR/UDP_RR)/-m/-s/-S/-p/-P/-c/-C/OMNI 及在线与离线安装。
Ubuntu 22.04 netperf 安装与命令参数详解
目标:在 Ubuntu 22.04 上安装 netperf 2.7.0(软件包内含 netserver 服务端),并逐参数实测吞吐、时延与 CPU 统计命令的输入与输出,涵盖 netserver 启动、
-H/-l/-t(TCP_STREAM/UDP_STREAM/TCP_RR/UDP_RR)/-m/-s/-S/-p/-P/-c/-C/-t OMNI,以及在线/离线安装。本文命令与输出均为 Ubuntu 22.04(netperf 2.7.0,Debian 包netperf 2.7.0-0.1 amd64)实测回显。
一、环境信息
- 操作系统:Ubuntu 22.04 LTS(22.04.5)
- netperf / netserver 版本:2.7.0(Debian 包
netperf 2.7.0-0.1 amd64,一个包同时提供/usr/bin/netperf与/usr/bin/netserver) - 服务端主机:网卡 eth0,IP
172.25.0.2/16,MACbe:17:8a:09:74:a9,netserver 默认监听端口12865 - 客户端主机:与服务端同一
172.25.0.0/16网段,通过-H 172.25.0.2指定服务端发起压制 - 数据目录:
/opt/data/netperf/(离线 .deb 包存放目录) - 测试视角:服务端为 netserver 监听方,客户端为 netperf 发压方;安装回显中的
invoke-rc.d: policy-rc.d denied execution of start.说明 netserver 不以 init 脚本自启,需手工启动
二、工具概述
netperf 是由 Hewlett-Packard(惠普实验室)开发、现由 Netperf / hp.com 维护的一套网络性能基准测试工具,分为两部分:netserver(服务端,在目标机上监听并提供测试)与 netperf(客户端,负责发起各类型测试)。它覆盖 TCP / UDP / SCTP / DCCP 等多种协议,可测吞吐(STREAM)、请求/应答时延(RR)、双向(DUPLEX)、OMNI 结构化输出等,是衡量「两台主机间网络极限性能、不同消息大小对吞吐影响、协议栈往返时延」的常用命令行工具。本文针对 Ubuntu 22.04 上的 2.7.0 实测。
三、安装
3.1 在线安装(apt)
netperf 在 Ubuntu 22.04 的 multiverse 仓库中,直接用 apt 安装即可(包内同时含 netperf 与 netserver):
sudo apt update
sudo apt install -y netperf
The following NEW packages will be installed:
netperf
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 540 kB of archives.
After this operation, 1108 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 netperf amd64 2.7.0-0.1 [540 kB]
Selecting previously unselected package netperf.
Preparing to unpack .../netperf_2.7.0-0.1_amd64.deb ...
Unpacking netperf (2.7.0-0.1) ...
Setting up netperf (2.7.0-0.1) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
安装后验证:
dpkg -l netperf
which netperf netserver
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==============-============-============-=================================
ii netperf 2.7.0-0.1 amd64 Network performance benchmark
/usr/bin/netperf
/usr/bin/netserver
💡 该 Debian 包同时安装
/usr/bin/netperf(客户端)与/usr/bin/netserver(服务端)两个二进制,因此服务端与客户端都用同一个软件包即可。
3.2 离线安装(.deb 包)
在可联网的 Ubuntu 机器上下载 netperf 的 .deb 包,拷入离线机器后用 dpkg 安装:
mkdir -p /opt/data/netperf
cd /opt/data/netperf
apt-get download netperf
Get:1 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 netperf amd64 2.7.0-0.1 [540 kB]
Fetched 540 kB in 2s (264 kB/s)
# 目录下生成 netperf_2.7.0-0.1_amd64.deb
-rw-r--r-- 1 root root 539524 Nov 25 2020 netperf_2.7.0-0.1_amd64.deb
拷贝到离线机器后本地安装(实测先 apt-get purge -y netperf 卸载再 dpkg 装):
dpkg -i /opt/data/netperf/netperf_2.7.0-0.1_amd64.deb
Selecting previously unselected package netperf.
Preparing to unpack netperf_2.7.0-0.1_amd64.deb ...
Unpacking netperf (2.7.0-0.1) ...
Setting up netperf (2.7.0-0.1) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
安装后验证版本:
netperf -V
Netperf version 2.7.0
四、命令实测
测试期间客户端、服务端均为 Ubuntu 22.04(netperf 2.7.0),netserver 在 172.25.0.2:12865 监听,客户端统一用 -H 172.25.0.2 指定服务端、用 -l 控制时长。各参数节中「输入=实测命令,输出=实测回显」。
4.1 启动服务端 netserver
用途:在作为「被测服务端」的主机上启动 netserver,监听默认端口 12865,等待客户端连接。-D 表示前台运行(不做 daemon),便于观察启动回显。
netserver -p 12865 -D
Starting netserver with host 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC
💡 netserver 默认端口即为
12865(RFC 注册的 netperf 控制端口),无参数直接运行 netserver 即是监听该端口;若端口已被占用会提示Unable to start netserver with 'IN(6)ADDR_ANY' port '12865'。需要换端口时用-p指定,见 4.7。
4.2 指定服务端 IP —— -H
用途:-H name|ip 指定被测服务端(netserver 所在机)的 IP 或主机名。客户端最基础的压制就是「-H + 服务端 IP + 时长」。
netperf -H 172.25.0.2 -p 12865 -l 5
MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
131072 16384 16384 5.00 12593.50
解读:默认执行 TCP 单向吞吐测试(TCP_STREAM)。131072 为接收套接字缓冲、16384 为发送套接字缓冲与每包消息大小(字节);Elapsed Time 5.00 为测试时长;Throughput 12593.50 10^6bits/sec 即约 12.59 Gbit/s 的单向吞吐。MIGRATED TCP STREAM TEST 字头末端的 to 172.25.0.2 表示数据流向 -H 指定的对端。
4.3 测试时长 —— -l
用途:-l testlen 指定测试时长,单位为秒(>0 为秒数,<0 表示按传输字节|事务数):客户端发起、服务端配合,到点即输出本次统计并结束。对比不同时长可看稳定窗口内结果的波动。
netperf -H 172.25.0.2 -p 12865 -l 3
MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
131072 16384 16384 3.00 11828.06
解读:Elapsed Time 3.00 证明测试只跑了 3 秒。与 4.2 的 5 秒(12593.50 10^6bits/sec)对照,可见时长越短、结果波动越大;生产取数建议用 ≥30 秒窗口。
💡
-l传负值表示按字节数/事务数做测试(帮助原文:Specify test duration (>0 secs) (<0 bytes|trans)),适用于「一定要传满 N 字节」的场景。
4.4 测试类型 —— -t
用途:-t testname 指定测试类型。未指定时默认 TCP_STREAM。常用类型:TCP_STREAM、UDP_STREAM、TCP_RR、UDP_RR 分别对应「TCP 吞吐、UDP 吞吐、TCP 请求/应答时延、UDP 请求/应答时延」。
TCP_STREAM(TCP 单向吞吐,-t 可省):
netperf -H 172.25.0.2 -p 12865 -l 5 -t TCP_STREAM
MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
131072 16384 16384 5.00 12593.50
UDP_STREAM(UDP 单向吞吐,含报文计数):
netperf -H 172.25.0.2 -p 12865 -l 5 -t UDP_STREAM
MIGRATED UDP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
enable_enobufs failed: getprotobyname
Socket Message Elapsed Messages
Size Size Time Okay Errors Throughput
bytes bytes secs # # 10^6bits/sec
212992 65507 5.00 68753 0 7205.74
212992 5.00 63697 6675.84
解读:UDP 无可靠传输,两行分别对应客户端发送行(收发各一行 Socket Size 行):第一行 68753 条报文全部成功后约 7.2 Gbit/s;第二行为服务端接收视角约 63697 条、6.68 Gbit/s。enable_enobufs failed: getprotobyname 是比 2.7.x 新版还早的警告(该构建未链接对应库),不影响统计。
TCP_RR(TCP 请求/应答时延,1 字节往返):
netperf -H 172.25.0.2 -p 12865 -l 5 -t TCP_RR
MIGRATED TCP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo : first burst 0
Local /Remote
Socket Size Request Resp. Elapsed Trans.
Send Recv Size Size Time Rate
bytes Bytes bytes bytes secs. per sec
16384 131072 1 1 5.00 23889.23
16384 131072
解读:Request Size 1 bytes + Resp. Size 1 bytes 说明每次事务只发 1 字节请求、回 1 字节应答,测得的是最小消息下的往返事务速率 23889.23 per sec(约 2.39 万次/秒,对应约 42µs 一次往返),衡量网络小包时延/连接往返能力。
UDP_RR(UDP 请求/应答时延):
netperf -H 172.25.0.2 -p 12865 -l 5 -t UDP_RR
MIGRATED UDP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo : first burst 0
Local /Remote
Socket Size Request Resp. Elapsed Trans.
Send Recv Size Size Time Rate
bytes Bytes bytes bytes secs. per sec
212992 212992 1 1 5.00 29267.91
212992 212992
解读:UDP 小包往返事务速率 29267.91 per sec。212992 为收发 socket 缓冲字节数。相比 TCP_RR,UDP 无需三次握手/拥塞窗口,小包互锤吞吐更高,但体现的是裸 UDP 往返能力。
4.5 消息大小 —— -m
用途:-m size(测试参数,写在 -- 之后)设置客户端每次发送的消息(Message)字节数。TCP_STREAM 默认 16384 字节;改小会降低吞吐,改大(配合收发缓冲)常可逼近网卡上限。用于观察「不同消息大小对吞吐的影响」。
netperf -H 172.25.0.2 -p 12865 -l 5 -t TCP_STREAM -- -m 2048
MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
131072 16384 2048 5.00 1924.35
解读:Message Size 一列由默认 16384 变为 2048,吞吐骤降到 1924.35 10^6bits/sec(约 1.9 Gbit/s)。小消息高频发包会放大系统调用与协议开销,故同样 5 秒内吞吐显著下降——这是评估业务「短包多、还是大包少」时的重要参数。
4.6 收发缓冲 —— -s / -S
用途:测试参数 -s size 设置客户端本端发送 socket 缓冲、-S size 设置(对端/发送)socket 缓冲字节数。注意:这在 -- 之后是「缓冲大小」,与全局的 -s seconds(等待 N 秒再开始)含义不同。
netperf -H 172.25.0.2 -p 12865 -l 5 -t TCP_STREAM -- -s 65536 -S 65536
MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
131072 131072 131072 5.00 16156.90
解读:Socket Size 两列都变为 131072(收发缓冲被显式放大到 131072),Message Size 跟随为 131072,吞吐提升到 16156.90 10^6bits/sec(约 16.16 Gbit/s)。收发缓冲越大,TCP 窗口可搬运的在途数据越多,利于高带宽时延积场景。
⚠️ 帮助中全局
-s seconds的语义是「Wait seconds between test setup and test start」;只有当它位于--之后、作为测试参数传给对应测试时,才被解释为发送缓冲大小。请务必放对位置。
4.7 端口 —— -p
用途:-p port 指定 netserver 端口与目标。服务端可起多个 netserver 监听不同端口做多路压测;客户端用 -p 对齐。
先在服务端再起一个监听 12866 的 netserver:
netserver -p 12866 -D
Starting netserver with host 'IN(6)ADDR_ANY' port '12866' and family AF_UNSPEC
客户端连过去:
netperf -H 172.25.0.2 -p 12866 -l 4
MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
131072 16384 16384 4.00 12138.56
💡
-p接受双参port,lport:port为远端 netserver 端口,,后为本地源端口(帮助原文:Specify netserver port number and/or local port)。
4.8 结果字头开关 —— -P
用途:-P 0|1 控制是否显示测试头(header)。-P 0 关闭两行 MIGRATED TCP STREAM TEST 字头,只输出数据行,便于脚本批量解析。
netperf -H 172.25.0.2 -p 12865 -l 5 -P 0
131072 16384 16384 5.00 12065.15
解读:MIGRATED TCP STREAM TEST 字头被隐藏,只留数据区(Socket/Message/Elapsed/Throughput 行)。结合 -f(如 -f M 输出 Mbit/s)适合做性能回归脚本。
4.9 CPU 统计 —— -c / -C
用途:-c 报告本地(客户端)CPU 利用率、-C 报告远端(服务端)CPU 利用率;配合 -t TCP_RR/UDP_RR 可评估「每次事务消耗的 CPU 微秒数」。
netperf -H 172.25.0.2 -p 12865 -l 5 -t TCP_RR -c -C
MIGRATED TCP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo : first burst 0
Local /Remote
Socket Size Request Resp. Elapsed Trans. CPU CPU S.dem S.dem
Send Recv Size Size Time Rate local remote local remote
bytes bytes bytes bytes secs. per sec % S % S us/Tr us/Tr
16384 131072 1 1 5.00 51265.36 81.98 81.99 95.944 95.955
16384 131072
解读:追加了 CPU local/CPU remote/S.dem local/S.dem remote 四列——本例 81.98%/81.99% 为两端单核利用率(小包高频互锤把单核打满),95.944/95.955 us/Tr 表示每次事务本端/对端各消耗约 96 微秒 CPU,是「小消息时延场景下 CPU 是否成为瓶颈」的直接证据。
4.10 OMNI 结构化输出 —— -t OMNI
用途:-t OMNI 启用「通用/结构化」输出形态,可自选输出列(字段、单位可定制)。-- -H <ip> -p <port> 内 -H 指向对端、-p 指向端口,OMNI 用更灵活的列布局给出结果。
netperf -H 172.25.0.2 -p 12865 -t OMNI -- -H 172.25.0.2 -p 12865 -l 5
OMNI Send TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 172.25.0.2 () port 0 AF_INET : demo
Local Remote Local Elapsed Throughput Throughput
Send Socket Recv Socket Send Time Units
Size Size Size (sec)
Final Final
4194304 6291456 16384 10.00 12820.89 10^6bits/s
解读:OMNI 布局展示 Local Send Socket(4194304)、Remote Recv Socket(6291456)、Local Send Size(16384)、Elapsed (sec)(10.00)、Throughput(12820.89)与 Units(10^6bits/s);OMNI 模式默认跑了 10 秒(-l 5 在此构建下以测试参数生效处为准),并可用 -O/-o 指定输出字段(如 -O local_throughput,remote_throughput)做高度定制化报表。
五、帮助输出
5.1 netperf -h(帮助全文,Ubuntu 22.04 / Debian 版 2.7.0)
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
For those options taking two parms, at least one must be specified;
specifying one value without a comma will set both parms to that
value, specifying a value with a leading comma will set just the second
parm, a value with a trailing comma will set just the first. To set
each parm to unique values, specify both and separate them with a
comma.
* For these options taking two parms, specifying one value with no comma
will only set the first parms and will leave the second at the default
value. To set the second value it must be preceded with a comma or be a
comma-separated pair. This is to retain previous netperf behaviour.
💡 从帮助可见:
-s全局语义为「测试开始前等待秒数」,收/发缓冲是放在--之后的测试参数-s size/-S size(见 4.6)。此版本帮助中存在两行-v(verbosity)的重复显示,为 2.7.0 原样输出。
5.2 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
5.3 版本查询
netperf -V
netserver -V
Netperf version 2.7.0
Netperf version 2.7.0
💡 注意
netserver -V输出的也是Netperf version 2.7.0(netserver 与 netperf 同源码构建、共享版本号)。包管理器侧版本号为dpkg -l netperf显示的2.7.0-0.1。
六、常见问题
6.1 netserver 启动报「Unable to start」
Q:netserver -D 报 Unable to start netserver with 'IN(6)ADDR_ANY' port '12865'?
A:12865 端口被占用(通常已有一个 netserver 在监听)。用 ss -lntp | grep 12865 排查占用进程,或改用 -p 指定其他端口(netserver -p 12866 -D),客户端用对应 -p 对齐。
6.2 客户端连不上 netserver
Q:netperf 报连接失败/一直超时,可能原因?
A:① 服务端 netserver 未启动或监听端口与客户端不一致(默认 12865,-p 指定则需两端一致);② 防火墙/安全组未放行 12865 控制端口;③ 客户端 -H 写错 IP。先用 ping/nc -vz <ip> 12865 验证三层与端口连通。
6.3 UDP_STREAM 输出来了个警告 enable_enobufs failed
Q:UDP 测试开头出现 enable_enobufs failed: getprotobyname 是什么?
A:这是该 2.7.0 构建未链接对应协议库导致的非致命警告,不影响 UDP 吞吐/报文计数统计,可忽略。
6.4 吞吐与厂商标称不符怎么排查
Q:同一网络测出的吞吐波动大或偏低?
A:依次排查:单条流本身有上限(可用多并发/多 TCP 流逼近),消息大小太小(改大 -m/收发缓冲),CPU 是否饱和(-c -C 看 us/Tr 与 %),以及网络链路本身瓶颈。生产取数建议 -l 30 以上多次求稳定的中位值。
七、参考链接
- netperf 官方站点:https://hewlettpackard.github.io/netperf/
- netperf 源码与文档仓库:https://github.com/HewlettPackard/netperf/
- Debian/Ubuntu 包页面:https://packages.ubuntu.com/jammy/netperf
- man 手册页:netperf(1)、netserver(1)(随软件包安装)
- 本文素材来源:Ubuntu 22.04(netperf 2.7.0)双机实测采集