iperf可用于測試兩個計算機之間的網絡帶寬和延遲。它是一個開源項目,提供了用于Windows、Linux和其他操作系統的版本。通過在兩臺計算機之間運行iPerf服務器和客戶端,您可以測量它們之間的網絡性能。iPerf客戶端發送數據流到服務器,并且服務器測量接收到的數據量和速度,支持在TCP和UDP協議上運行。
本文將詳細介紹如何使用 iperf3 工具,測試彈性云主機間網絡性能。內容主要包括“測試前準備”、“TCP 帶寬測試”、“UDP PPS 測試”。
安裝使用
[root@ss-daliu1 ~]# sudo yum install iperf3
[root@ss-daliu2 ~]# sudo yum install iperf3
參數說明
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參數。
一對一場景
測試準備
| 類型 | 數量 | 鏡像 | 規格 |
|---|---|---|---|
| 被測機(被測機:被壓力測試網絡性能的彈性云服務器,可作為 iperf3 測試中的 client 端(發送端)) | 1臺 | CTyunOS 23.01(推薦) | 4C8G(推薦) |
| 輔助云服務器(作為 iperf3 測試中的 server 端(接收端)) | 1臺 | CTyunOS 23.01(推薦) | 4C8G(推薦) |
準備兩臺處于不同計算節點的彈性云主機,網絡可達,分別安裝iperf,添加對應的安全組規則、防護墻規則放行。
測試TCP吞吐量
-
TCP發送帶寬。
# Server端開啟iperf的服務器模式,指定TCP端口:10000,安全組規則入方向放行TCP 10000端口 [root@ss-daliu2 ~]# iperf3 -s -i 1 -p 10000 ----------------------------------------------------------- Server listening on 10000 (test #1) # Client端啟動iperf的客戶端模式,連接服務端。ip為server端地址 [root@ss-daliu1 ~]# iperf3 -c 192.168.0.15 -i 1 -t 60 -p 10000 Connecting to host 192.168.0.15, port 10000 [ 5] local 192.168.0.8 port 37720 connected to 192.168.0.15 port 10000 [ ID] Interval Transfer Bitrate Retr Cwnd [ 5] 0.00-1.00 sec 332 MBytes 2.78 Gbits/sec 4394 167 KBytes ... ... ... [ 5] 52.00-53.00 sec 180 MBytes 1.51 Gbits/sec 6546 130 KBytes [ 5] 53.00-54.00 sec 166 MBytes 1.39 Gbits/sec 5237 180 KBytes [ 5] 54.00-55.00 sec 166 MBytes 1.40 Gbits/sec 5505 19.8 KBytes [ 5] 55.00-56.00 sec 207 MBytes 1.74 Gbits/sec 7651 12.7 KBytes [ 5] 56.00-57.00 sec 167 MBytes 1.40 Gbits/sec 5822 314 KBytes [ 5] 57.00-58.00 sec 168 MBytes 1.41 Gbits/sec 5952 58.0 KBytes [ 5] 58.00-59.00 sec 178 MBytes 1.49 Gbits/sec 6940 372 KBytes [ 5] 59.00-60.00 sec 205 MBytes 1.72 Gbits/sec 6756 2.83 KBytes - - - - - - - - - - - - - - - - - - - - - - - - - [ ID] Interval Transfer Bitrate Retr [ 5] 0.00-60.00 sec 10.7 GBytes 1.53 Gbits/sec 343419 sender [ 5] 0.00-60.00 sec 10.7 GBytes 1.53 Gbits/sec receiver結果說明:
Interval表示時間間隔。
Transfer表示時間間隔里面轉輸的數據量。
Bitrate是時間間隔里的傳輸速率。 -
TCP接收帶寬。
# Server端開啟iperf的服務器模式,指定TCP端口:10000,安全組規則入方向放行TCP 10000端口 [root@ss-daliu2 ~]# iperf3 -s -i 1 -p 10000 ----------------------------------------------------------- Server listening on 10000 (test #1) # Client端啟動iperf的客戶端模式,連接服務端。ip為server端地址 [root@ss-daliu1 ~]# iperf3 -c 192.168.0.15 -i 1 -t 60 -p 10000 -R Connecting to host 192.168.0.15, port 10000 Reverse mode, remote host 192.168.0.15 is sending [ 5] local 192.168.0.8 port 38316 connected to 192.168.0.15 port 10000 [ ID] Interval Transfer Bitrate [ 5] 0.00-1.00 sec 336 MBytes 2.81 Gbits/sec [ 5] 1.00-2.00 sec 172 MBytes 1.45 Gbits/sec ... ... ... [ 5] 59.00-60.00 sec 179 MBytes 1.50 Gbits/sec - - - - - - - - - - - - - - - - - - - - - - - - - [ ID] Interval Transfer Bitrate Retr [ 5] 0.00-60.00 sec 10.7 GBytes 1.53 Gbits/sec 360910 sender [ 5] 0.00-60.00 sec 10.7 GBytes 1.53 Gbits/sec receiver結果說明:
Interval表示時間間隔。
Transfer表示時間間隔里面轉輸的數據量。
Bitrate是時間間隔里的傳輸速率。 -
TCP上下行帶寬測試(雙向傳輸)。
# Server端開啟iperf的服務器模式,指定TCP端口:10000,安全組規則入方向放行TCP 10000端口 [root@ss-daliu2 ~]# iperf3 -s -i 1 -p 10000 ----------------------------------------------------------- Server listening on 10000 (test #1) # Client端啟動iperf的客戶端模式,連接服務端。ip為server端地址 [root@ss-daliu1 ~]# iperf3 -c 192.168.0.15 -i 1 -d -t 60 -p 10000 ... Thread FD 5 stopped Sender threads stopped tcpi_snd_cwnd 335 tcpi_snd_mss 1448 tcpi_rtt 1054 send_results { "cpu_util_total": 4.7314293702543742, "cpu_util_user": 0.0694572749595791, "cpu_util_system": 4.6619720952947947, "sender_has_retransmits": 1, "congestion_used": "cubic", "streams": [{ "id": 1, "bytes": 11446517760, "retransmits": 354575, "jitter": 0, "errors": 0, "omitted_errors": 0, "packets": 0, "omitted_packets": 0, "start_time": 0, "end_time": 60.00129 }] } get_results { "cpu_util_total": 3.3835424728514676, "cpu_util_user": 0.14802798323701713, "cpu_util_system": 3.2355161562396457, "sender_has_retransmits": -1, "congestion_used": "cubic", "streams": [{ "id": 1, "bytes": 11442323456, "retransmits": -1, "jitter": 0, "errors": 0, "omitted_errors": 0, "packets": 0, "omitted_packets": 0, "start_time": 0, "end_time": 60.00149 }] } interval_len 1.000265 bytes_transferred 176291840 interval forces keep [ 5] 59.00-60.00 sec 168 MBytes 1.41 Gbits/sec 5895 474 KBytes - - - - - - - - - - - - - - - - - - - - - - - - - [ ID] Interval Transfer Bitrate Retr [ 5] 0.00-60.00 sec 10.7 GBytes 1.53 Gbits/sec 354575 sender [ 5] 0.00-60.00 sec 10.7 GBytes 1.53 Gbits/sec receiver Receiver threads stopped iperf Done. # 測試4線程TCP吞吐量,如果沒有指定發送方式,iperf客戶端只會使用單線程。 $ iperf3 -c 172.16.0.9 -i 1 -P 4 -t 60 -p 10000結果說明:
Interval表示時間間隔。
Transfer表示時間間隔里面轉輸的數據量。
Bitrate是時間間隔里的傳輸速率。
測試UDP吞吐量
帶寬測試通常采用UDP模式,因為能測出極限帶寬、時延抖動、丟包率。在進行測試時,首先以鏈路理論帶寬作為數據發送速率進行測試,例如,從客戶端到服務器之間的鏈路的理論帶寬為100Mbps,先用-b 100M進行測試,然后根據測試結果(包括實際帶寬,時延抖動和丟包率),再以實際帶寬作為數據發送速率進行測試,會發現時延抖動和丟包率比第一次好很多,重復測試幾次,就能得出穩定的實際帶寬。
# Server端開啟iperf的服務器模式,指定UDP端口:
[root@ss-daliu2 ~]# iperf3 -s -i 1 -p 10001
-----------------------------------------------------------
Server listening on 10001
-----------------------------------------------------------
# Client端啟動iperf的客戶端模式,連接服務端
[root@ss-daliu1 ~]# iperf3 -u -c 192.168.0.15 -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 Bitrate 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 192.168.0.15 -b 5m -P 4 -t 60 -p 10001
# 進行上下行帶寬測試(雙向傳輸)
$ iperf3 -u -c 192.168.0.15 -b 100M -d -t 60 -p 10001
結果說明:
jitter為抖動,在連續傳輸中的平滑平均值差。
Lost為丟包數量。
Total Datagrams為包數量。
iperf測試udp丟包嚴重:可以嘗試增大收發包緩沖區。
echo 16777216 > /proc/sys/net/core/rmem_max
echo 16777216 > /proc/sys/net/core/rmem_default
echo 16777216 > /proc/sys/net/core/wmem_max
echo 16777216 > /proc/sys/net/core/wmem_default
多流打流測試方案(針對大規格云主機)
針對大規格云主機,單進程打流可能無法發揮出云主機的性能,可以參考下述多流打流測試方案。
虛機網絡性能說明
虛機從同?個隊列發出的報文,在底層只能被?個cpu轉發。即虛機?個隊列的發包性能 <= 虛擬交換機單核轉發性能。
性能測試步驟
-
確定兩個?于性能測試的計算節點: A, B。
-
在每個計算節點上分別創建2個虛機,虛機規格盡量大,虛機內核版本4.19及以上(建議使用CTyunOS 23.01)。
-
4個測試虛機內安裝iperf3包。
yum install iperf3 -
B計算節點上的兩個虛機(B1, B2)分別起iperf服務端。
for i in {0..63}; do iperf3 -s -D -p $((5200 + $i)) ; done -
A計算節點上兩個虛機(A1, A2)起iperf客戶端(A1給B1發包, A2給B2發包),
創建發包腳本:#!/usr/bin/env bash flow_per_thread=2 max_thread=64 run_time=60 udp_opts="-u" # 默認用全力(100G)發送udp報文, 但可能存在丟包. udp_band_Mb=100000 tcp_opts="" use_tcp=1 usage() { echo ' usage: $0 -s <server-ip> [--pps] [-n <nprocess>] [-t <time>] [--tcp] -s Iperf server ip address. --pps Send small pkts for pps benchmark. By default send large pkts for bandwidth benckmark. -n Set max process number. If not set, tx queue number is used. -t time in seconds to transmit for, default 60 seconds. --udp Send udp instead of tcp(default). Udp is good for some special scenarios. -b Bandwidth to send for udp mode. i.e. 1M, 2G, ' } while [ $# -gt 0 ]; do case $1 in -s | --server) server="$2" shift ;; -n | --nprocess) max_thread="$2" shift ;; -t | --time) run_time="$2" shift ;; --pps) # 144 bytes tcp_opts="$tcp_opts -M 90" # 18(eth) + 28(ip) + 18(udp) = 64 udp_opts="$udp_opts -l 18" ;; --udp) use_tcp=0 ;; -b) udp_band="$2" _unit=$(echo $udp_band | grep -oiP '\d+\K[mg]') _num=$(echo $udp_band | grep -oP '\d+') if [ -z "$_unit" ] || [ -z "$_num" ];then echo "bad bandwidth" exit 1 fi if [ ${_unit,,} == 'g' ];then _num=$((_num*1024)) fi udp_band_Mb=$_num ;; -h | --help) usage exit 0 ;; *) usage exit 1 ;; esac shift done interface=$(ip route get $server |grep -oP "(?<=dev )\S+") n_txq=$(ls /sys/class/net/$interface/queues/tx-*/xps_cpus | wc -l) band_per_flow=$((udp_band_Mb/flow_per_thread/max_thread)) i=0 for fxps in `ls /sys/class/net/$interface/queues/tx-*/xps_cpus | sort` do aff=$(cat $fxps) if ((i >= max_thread));then break fi no_zero_aff=$(echo "$aff" | tr -d '0xX') if [ -z "$no_zero_aff" ];then _aff=$((1 << $i)) aff=$(printf "%x" $_aff) echo $aff > $fxps fi # Affinity on only one core suffix="" for (( j=${#aff}-1; j>=0; j-- )); do if [ "${aff:$j:1}" == "," ];then continue fi if [ "${aff:$j:1}" != "0" ];then aff_one="${aff:$j:1}${suffix}" break fi suffix+="0" done if [ -z "$aff_one" ];then echo "Failed to find txq affinity core" exit 1 fi aff_one=`echo $aff_one | tr '13579bdf' '1' | tr '26ae' '2' | tr '4c' '4'` if ((use_tcp > 0)) ;then taskset $aff_one iperf3 -c $server $tcp_opts -P 2 -p $((5200+$i)) -t $run_time & else taskset $aff_one iperf3 -c $server $udp_opts -b ${udp_band_Mb}M -P 2 -p $((5200+$i)) -t $run_time & fi ((i++)) done wait
測試pps(144B/64B小包):
bash iperf-client.sh -s <B1-ip> --pps -t 300
測試帶寬(1500B大包):
bash iperf-client.sh -s <B1-ip> -t 300
- 打流的過程中虛擬交換機會根據流量做balance,所以我們流量測試應持續5分鐘。等待底層balance到最佳狀態,以得到較好的性能結果。