# iperf3 安裝/執行/參數 學習筆記
`linux` `iPerf` `網路測速`
> [TOC]
指令式的網路速度測試工具
支援
* IPv4 . IPv6
* 常見的 TCP、UDP 傳輸協定
* Windows / Mac OS X / Linux / 手機平台
## 官方網站/參數使用文件:
* https://iperf.fr
* https://iperf.fr/iperf-doc.php
## 下載位置:
> Debian / Ubuntu Linux
`sudo apt-get install iperf3`
> macOS Homebrew
`brew install iperf3`
> Window
https://iperf.fr/iperf-download.php
備註:
iperf3安装配置使用踩坑记录
[需要放在 C:\Windows\System32 sudo 權限](https://tinychen.com/20190409-iperf3/)
# 參數設定
> 前置作業:需要 server 及 client 端
> [公用伺服器](https://iperf.fr/iperf-servers.php)
## Server 端
### iperf3 -s
**iperf3 -s -p 5205**
> Serve 端模式 預設監聽 5201 port
![](https://hackmd.io/_uploads/S1muwwwTn.png)
**lsof -i | grep iperf3**
> 確認是否真的有開 Port
![](https://hackmd.io/_uploads/rJ89y_Y62.png)
## Client 端
### iperf3 -c [Server IP]
> Client 端模式
> 輸入 Server IP 位址
> 伺服器在本地可輸入[ localhost / 127.0.0.1](https://zh.wikipedia.org/zh-tw/Localhost)
### iperf3 -c localhost -i 2 -t 30 --logfile output.log
* -i 輸出數據間隔時間
* -t 監控數據持續時間
* -w 測試傳輪檔案大小
* -p 更換預設Port號
* --logfile 輸出數據結果
* -J file.log 指定JSON格式
* -R 反轉,下載測速
<!--
![](https://hackmd.io/_uploads/SyvVdOKph.png)
-->
## iperf3 使用語法總覽
iperf命令 中文說明
https://a42033.gitbooks.io/system/content/system/network/iperf.html
iperf -help
```bash=
Usage: iperf3 [-s|-c host] [options]
iperf3 [-h|--help] [-v|--version]
Server or Client:
-p, --port # server port to listen on/connect to
-f, --format [kmgtKMGT] format to report: Kbits, Mbits, Gbits, Tbits
-i, --interval # seconds between periodic throughput reports
-I, --pidfile file write PID file
-F, --file name xmit/recv the specified file
-B, --bind <host> bind to the interface associated with the address <host>
-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
--timestamps<=format> emit a timestamp at the start of each output line
(optional "=" and format string as per strftime(3))
--rcv-timeout # idle timeout for receiving data (default 120000 ms)
-d, --debug[=#] emit debugging output
(optional optional "=" and debug level: 1-4. Default is 4 - all messages)
-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
-1, --one-off handle one client connection then exit
--server-bitrate-limit #[KMG][/#] server's total bit rate limit (default 0 = no limit)
(optional slash and number of secs interval for averaging
total data rate. Default is 5 seconds)
--idle-timeout # restart idle server after # seconds in case it
got stuck (default - no timeout)
--rsa-private-key-path path to the RSA private key used to decrypt
authentication credentials
--authorized-users-path path to the configuration file containing user
credentials
--time-skew-threshold time skew threshold (in seconds) between the server
and client during the authentication process
Client specific:
-c, --client <host>[%<dev>] run in client mode, connecting to <host>
(option <dev> equivalent to `--bind-dev <dev>`)
-u, --udp use UDP rather than TCP
--connect-timeout # timeout for control connection setup (ms)
-b, --bitrate #[KMG][/#] target bitrate in bits/sec (0 for unlimited)
(default 1 Mbit/sec for UDP, unlimited for TCP)
(optional slash and packet count for burst mode)
--pacing-timer #[KMG] set the timing for pacing, in microseconds (default 1000)
-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, --length #[KMG] length of buffer to read or write
(default 128 KB for TCP, dynamic or 1460 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)
--bidir run in bidirectional mode.
Client and server send and receive data.
-w, --window #[KMG] set send/receive socket buffer sizes
(indirectly sets TCP window size)
-M, --set-mss # set TCP/SCTP maximum segment size (MTU - 40 bytes)
-N, --no-delay set TCP/SCTP no delay, disabling Nagles Algorithm
-4, --version4 only use IPv4
-6, --version6 only use IPv6
-S, --tos N set the IP type of service, 0-255.
The usual prefixes for octal and hex can be used,
i.e. 52, 064 and 0x34 all specify the same value.
--dscp N or --dscp val set the IP dscp value, either 0-63 or symbolic.
Numeric values can be specified in decimal,
octal and hex (see --tos above).
-Z, --zerocopy use a 'zero copy' method of sending data
-O, --omit N perform pre-test for N seconds and omit the pre-test statistics
-T, --title str prefix every output line with this string
--extra-data str data string to include in client and server JSON
--get-server-output get results from server
--udp-counters-64bit use 64-bit counters in UDP test packets
--repeating-payload use repeating pattern in payload, instead of
randomized payload (like in iperf2)
--dont-fragment set IPv4 Dont Fragment flag
--username username for authentication
--rsa-public-key-path path to the RSA public key used to encrypt
authentication credentials
[KMG] indicates options that support a K/M/G suffix for kilo-, mega-, or giga-
iperf3 homepage at: https://software.es.net/iperf/
Report bugs to: https://github.com/esnet/iperf
```
## 參考資料:
### [指令式的網路速度測試工具 iPerf3](https://t301000.blogspot.com/2020/05/iperf3.html)
解釋原理/緣由:
**Window:**
### [如何使用Iperf測試TP-Link路由器的速度?](https://www.tp-link.com/tw/support/faq/2408/)
### [指令式的網路速度測試工具 iPerf3 ,揪出網路頻寬真實的一面](https://walker-a.com/archives/5857)
純指令教學:
**Linux**
## [iPerf3 網路速度測試教學,頻寬檢測指令工具](https://blog.gtwang.org/useful-tools/iperf-network-bandwidth-testing-tool-tutorial/)
## [利用IPERF來測試網路傳輸速度](https://jasslee.pixnet.net/blog/post/61209718-利用iperf來測試網路傳輸速度)
**MacOS**
## [macOS 必裝! Homebrew 套件管理工具---iperf3](https://www.jkg.tw/p677/#span-stylecolor-008000iperf3span)
## 實際案例
**Ysuper Liang:WinSCP 檔案傳輸速度異常,確認網速是否正常**
https://hackmd.io/@-HgNk7Y5R_G72ULgE21oOA/BJDhzCaLs
> linux iperf3 -c 211.73.xx.xx -i 3 -t 30 -P 4
<!-- 工人智慧(手動 key 測試參數)
![](https://hackmd.io/_uploads/SyxApGQjp3.png)
-->
## 進階使用
```
for i in 1 2 4 8; do iperf3 -c 192.168.1.1 -t 30 -Z -O 3 -P $i | tail -n 3 | sed -nE "s/.* ([0-9.]+ [M|G|K]bits.sec).*/P=$i \1/p"; done
```
sed (stream editor) 可以分析並做篩選處理輸出資料,
參考資料:
https://dywang.csie.cyut.edu.tw/dywang/linuxProgram/linuxProgram.html
## 進階學習
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/8/html/monitoring_and_managing_system_status_and_performance/tuning-tcp-connections-for-high-throughput_tuning-the-network-performance
-Z
zerocopy:實現零複製方法,而不使用write ()系統調用。只有在您要模擬具有零複製的應用程序或在單一流量上達到40 Gbps時,才需要此選項。
Zero Copy 詳細說明:
https://hackmd.io/@hpc-ai/HPCNotes/%2FHjAXXQSWRB-3jJm7GyEjAA%3Fview#Zero-Copy
https://www.cnblogs.com/chengd/p/7572066.html
-O
忽略前幾秒冷啟動時間
## 備註筆記
第十二章、學習 Shell Scripts
https://linux.vbird.org/linux_basic/centos7/0340bashshell-scripts.php#for
https://shengyu7697.github.io/shell-script-for/
https://medium.com/程式裡有蟲/mac-os-設定環境變數-2351da88523f
Linux 命令手册、详解、学习
https://wangchujiang.com/linux-command/
除錯判斷:
區網/還是FW問題
* 廣域網絡 (WAN) 與局部區域網絡 (LAN)
* 看防火牆狀態 ubuntu / sudo ufw status
* 設定封包過濾規則 192.168.1.1/16
---
[Linux 查看端口占用情况](https://www.runoob.com/w3cnote/linux-check-port-usage.html)
> lsof -i:portn_umber 查看佔用 port
> netstat -ntlp //查看目前所有 tcp port
nmap localhost
[用 nmap 檢查本機與遠端電腦上開啟的 Port](https://ephrain.net/pentest-用-nmap-檢查本機與遠端電腦上開啟的-port/)
[Netcat(Linux nc 指令)網路管理者工具實用範例](
https://blog.gtwang.org/linux/linux-utility-netcat-examples/)
什麼是 WAN?| WAN 與 LAN
廣域網路 (WAN) 是任何延伸至大型地理區域的網路,通常會連接多個區域網路 (LAN)。
https://www.cloudflare.com/zh-tw/learning/network-layer/what-is-a-wan/