iperf 的介紹和使用

iperf 是一個網絡性能測試工具,做服務開發或者測試的同學,接觸的可能比較多。因爲最近有用到這個工具,並且這個工具做的非常不錯,這裏記錄一下工具的使用方法。iperf 是個開源並且跨平臺的軟件,代碼託管在 GitHub 上,可以從 Releases 找到各個發行版本,也可以去 官網 下載各個平臺的版本。 使用 iperf 時,需要分別運行服務端和客戶端,在測試是最好保證兩個端的軟件版本一致,這樣會免去一些沒必要的麻煩。

下載好後,可以先在本機做一個簡單的迴環測試,結果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 運行服務端
$ ./iperf -s
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 128 KByte (default)
------------------------------------------------------------
[ 4] local 127.0.0.1 port 5001 connected with 127.0.0.1 port 54817
[ ID] Interval Transfer Bandwidth
[ 4] 0.0-10.0 sec 35.1 GBytes 30.1 Gbits/sec
 
# 運行客戶端
$ ./iperf -c 127.0.0.1
------------------------------------------------------------
Client connecting to 127.0.0.1, TCP port 5001
TCP window size: 144 KByte (default)
------------------------------------------------------------
[ 4] local 127.0.0.1 port 54817 connected with 127.0.0.1 port 5001
[ ID] Interval Transfer Bandwidth
[ 4] 0.0-10.0 sec 35.1 GBytes 30.1 Gbits/sec

默認情況下,會使用 TCP 連接,綁定在 5001 端口上,可以從上述結果看到,當前本機的帶寬爲 30.1 Gbits/sec

主要參數信息

適用於 服務端/客戶端

1
2
3
4
5
6
7
8
9
10
11
-f 指定數據顯示格式 [k|m|K|M] 分別表示 Kbits、Mbits、KBytes、MBytes,默認是 Mbits
-l 讀寫緩衝區的大小,默認是 8K
-u 使用 udp 協議
-i 以秒爲單位統計帶寬值
-m 顯示最大的 TCP 數據段大小
-p 指定服務端或者客戶端的端口號
-w 指定 TCP 窗口大小
-B 綁定道指定的主機地址或接口
-C 兼容舊版本
-M 設置 TCP 數據包的最大 MTU 值
-V 傳輸 IPV6 包

適用於 服務端

1
2
3
-s 以服務器模式啓動
-U 單線程 UDP 模式
-D 以守護進程模式運行

適用於 客服端

1
2
3
4
5
6
7
8
9
10
-c 以客戶端模式運行,並指定服務端的地址
-b 指定客戶端通過 UDP 協議發送信息的帶寬,默認爲 1Mbit/s
-d 同時進行雙向傳輸測試
-n 指定傳輸的字節數
-r 單獨進行雙向傳輸測試
-t 指定 iperf 測試的時間,默認 10s
-F 指定要傳輸的文件
-L 指定一個端口,服務利用這端口與客戶端連接
-P 指定客戶端到服務器的連接數,默認是 1
-T 指定 ttl 值
  • 用 -u 參數來指定使用 UDP 協議,需要在 -p 參數之前指定
  • 測試之前確保防火牆爲關閉狀態

網絡性能測試

TCP 協議測試帶寬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 運行服務端
$ iperf -s
 
# 運行客戶端
$ iperf -c 172.18.142.62 -i 1 -t 10
------------------------------------------------------------
Client connecting to 172.18.142.62, TCP port 5001
TCP window size: 129 KByte (default)
------------------------------------------------------------
[ 4] local 172.18.98.209 port 57809 connected with 172.18.142.62 port 28756
[ ID] Interval Transfer Bandwidth
[ 4] 0.0- 1.0 sec 384 KBytes 3.15 Mbits/sec
[ 4] 1.0- 2.0 sec 256 KBytes 2.10 Mbits/sec
[ 4] 2.0- 3.0 sec 256 KBytes 2.10 Mbits/sec
[ 4] 3.0- 4.0 sec 256 KBytes 2.10 Mbits/sec
[ 4] 4.0- 5.0 sec 512 KBytes 4.19 Mbits/sec
[ 4] 5.0- 6.0 sec 1.12 MBytes 9.44 Mbits/sec
[ 4] 6.0- 7.0 sec 1.12 MBytes 9.44 Mbits/sec
[ 4] 7.0- 8.0 sec 1.12 MBytes 9.44 Mbits/sec
[ 4] 8.0- 9.0 sec 1.25 MBytes 10.5 Mbits/sec
[ 4] 9.0-10.0 sec 1.12 MBytes 9.44 Mbits/sec
[ 4] 0.0-10.1 sec 7.50 MBytes 6.25 Mbits/sec

使用 TCP 協議進行測試時,需要注意的就是 TCP 窗口大小,可以使用 -w 參數指定,網絡通道的容量 capacity = bandwidth * round-trip time,而理論 TCP 窗口大小就是網絡通道的容量。例如,網絡帶寬爲 40Mbit/s,迴環路徑消耗時間是 2ms,那麼 TCP 的窗口大小不小於 40Mbit/s×2ms = 80kbit = 10Kbytes

UDP 協議測試帶寬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 運行服務端
$ iperf -u -s
 
# 運行客戶端
$ iperf -c 172.18.142.62 -u -i 1 -t 10 -b 30M
------------------------------------------------------------
Client connecting to 172.18.142.62, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size: 9.00 KByte (default)
------------------------------------------------------------
[ 4] local 172.18.98.209 port 53220 connected with 172.18.142.62 port 28756
[ ID] Interval Transfer Bandwidth
[ 4] 0.0- 1.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 1.0- 2.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 2.0- 3.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 3.0- 4.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 4.0- 5.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 5.0- 6.0 sec 3.57 MBytes 30.0 Mbits/sec
[ 4] 6.0- 7.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 7.0- 8.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 8.0- 9.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 9.0-10.0 sec 3.58 MBytes 30.0 Mbits/sec
[ 4] 0.0-10.0 sec 35.8 MBytes 30.0 Mbits/sec
[ 4] Sent 25511 datagrams
[ 4] Server Report:
[ 4] 0.0-11.6 sec 13.6 MBytes 9.83 Mbits/sec 1.971 ms 15786/25497 (62%)
[ 4] 0.0-11.6 sec 140 datagrams received out-of-order

上述命令指定了客戶端以 30Mbit/s 速度發送數據,由於 UDP 協議是無連接不可靠的,並且只管發包,不確保包在服務端是否接收到,所以需要查看服務報告才能確定當前網絡性能數據。如果在不知道當前網絡帶寬的情況下,需要不斷的調整參數值,並且查看丟包率,來確定當前網絡性能情況。如果你當前是遠程登錄到服務器上進行測試的,可以從小到大的方式進行測試,否則很容易導致服務當前帶寬被佔滿。

參考資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章