1. 程式人生 > 其它 >使用 iperf 測試網路

使用 iperf 測試網路

  iperf是一種命令列工具,用於通過測量伺服器可以處理的最大網路吞吐量來診斷網路速度問題。它在遇到網路速度問題時特別有用,通過該工具可以確定哪個伺服器無法達到最大吞吐量。

開始之前

  • 您需要root許可權具有sudo許可權的使用者帳戶。
  • update 系統源

安裝iperf

  • 該iperf軟體包在大多數Linux發行版的儲存庫中

Debian和Ubuntu安裝

apt-get update
apt-get install iperf

CentOS 安裝

  • CentOS儲存庫沒有iperf,使用EPEL儲存庫,它是用於在Redhat系統上安裝第三方軟體包的儲存庫
yum install epel-release
yum update
yum install iperf

使用iperf的注意事項

  • 必須在測試的兩臺計算機上同時安裝iPerf。如果在個人計算機上使用基於Unix或 Linux的作業系統,則可以在本地計算機上安裝iPerf。

  • 但是,如果要測試網路提供商的吞吐量,最好使用另一臺伺服器作為終點,因為本地ISP可能會施加影響測試結果的網路限制。

Iperf引數說明

主客端引數

-s     //以server模式啟動,eg:iperf -s

-c     //host以client模式啟動,host是server端地址,eg:iperf -c 222.35.11.23

===============

通用引數

-f     //[kmKM] 分別表示以Kbits, Mbits, KBytes, MBytes顯示報告,預設以Mbits為單位,eg:iperf -c 222.35.11.23 -f K
-i //sec 以秒為單位顯示報告間隔,eg:iperf -c 222.35.11.23 -i 2 -l //緩衝區大小,預設是8KB,eg:iperf -c 222.35.11.23 -l 16 -m //顯示tcp最大mtu值 -o //將報告和錯誤資訊輸出到檔案eg:iperf -c 222.35.11.23 -o ciperflog.txt -p //指定伺服器端使用的埠或客戶端所連線的埠eg:iperf -s -p 9999;iperf -c 222.35.11.23 -p 9999 -u //使用udp協議 -w //指定TCP視窗大小,預設是8KB
-B //繫結一個主機地址或介面(當主機有多個地址或介面時使用該引數) -C //相容舊版本(當server端和client端版本不一樣時使用) -M //設定TCP資料包的最大mtu值 -N //設定TCP不延時 -V //傳輸ipv6資料包 =============== server專用引數 -D //以服務方式執行iperf,eg:iperf -s -D -R //停止iperf服務,針對-D,eg:iperf -s -R =============== client端專用引數 -d //同時進行雙向傳輸測試 -n //指定傳輸的位元組數,eg:iperf -c 222.35.11.23 -n 100000 -r //單獨進行雙向傳輸測試 -t //測試時間,預設10秒,eg:iperf -c 222.35.11.23 -t 5 -F //指定需要傳輸的檔案 -T //指定ttl值

TCP客戶端和伺服器的測試

  iperf需要兩個系統,因為一個系統必須充當服務端,另外一個系統充當客戶端,客戶端連線到需要測試速度的服務端
  1.在需要測試的電腦上,以伺服器模式啟動iperf

iperf -s

  可以看到類似於下圖的輸出

------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------

  2.在第二臺電腦上,以客戶端模式啟動iperf連線到第一臺電腦,替換198.51.100.5為地臺電腦的ip地址

iperf -c 198.51.100.5
------------------------------------------------------------
Client connecting to 198.51.100.5, TCP port 5001
TCP window size: 45.0 KByte (default)
------------------------------------------------------------
[ 3] local 198.51.100.6 port 50616 connected with 198.51.100.5 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec

  3.這時可以在第一步中的服務端終端看到連線和結果

------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
[ 4] local 198.51.100.5 port 5001 connected with 198.51.100.6 port 50616
[ ID] Interval Transfer Bandwidth
[ 4] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec

  4.要停止iperf服務程序,請按CTRL+c

UDP客戶端和伺服器的測試

  使用iperf,還可以測試通過UDP連線實現的最大吞吐量

  需要測試的機器作為服務端

iperf -s -p 5001 -f m -i 1

  找另一臺機器做客戶端,兩臺機器的用網線連線要測試的網絡卡埠

iperf -u -c 192.168.1.1. -p 5001 -p 1 -f m -i 1 -b 1000M -t 30

雙向測試

  在某些情況下,可能希望測試兩臺伺服器以獲得最大吞吐量。使用iperf提供的內建雙向測試功能可以輕鬆完成此測試。

  • 要測試兩個連線,從客戶端執行一下命令,替換ip為服務端ip地址
  • iperf -c 198.51.100.5 -d
  • 結果是iperf將在客戶端伺服器上啟動伺服器和客客戶端(198.51.100.6)連線。完成此操作後,iperf會將iperf伺服器連線到客戶端,該連線現在既充當伺服器連線又充當客戶端連線。
  • ------------------------------------------------------------
    Server listening on TCP port 5001
    TCP window size: 85.3 KByte (default)
    ------------------------------------------------------------
    ------------------------------------------------------------
    Client connecting to 198.51.100.5, TCP port 5001
    TCP window size: 351 KByte (default)
    ------------------------------------------------------------
    [ 3] local 198.51.100.6 port 50618 connected with 198.51.100.5 port 5001
    [ 5] local 198.51.100.6 port 5001 connected with 198.51.100.5 port 58650
    [ ID] Interval Transfer Bandwidth
    [ 5] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec
    [ 3] 0.0-10.2 sec 1.28 GBytes 1.08 Gbits/sec

    在伺服器是哪個,可以看到:

  • ------------------------------------------------------------
    Client connecting to 198.51.100.6, TCP port 5001
    TCP window size: 153 KByte (default)
    ------------------------------------------------------------
    [ 6] local 198.51.100.5 port 58650 connected with 198.51.100.6 port 5001
    [ 6] 0.0-10.1 sec 1.27 GBytes 1.08 Gbits/sec
    [ 5] 0.0-10.2 sec 1.28 GBytes 1.08 Gbits/sec