Contents

如何使用iPerf来衡量网络性能

1. 概述

在本教程中,我们将学习如何使用iPerf 工具来测量网络性能和带宽。

iPerf 是一个用 C 编程语言编写的开源工具。此外,它以客户端-服务器模型工作并支持 UDP 和 TCP。因此,我们需要有两个系统都安装了该工具。首先,我们需要启动服务器。之后,我们需要从客户端机器连接到服务器。

2. 在客户端和服务器上安装iPerf

我们需要在客户端和服务器上安装 iPerf:

$ sudo apt install iperf

之后,我们应该检查它是否已安装:

$ iperf --version
iperf version 2.0.13 (21 Jan 2019) pthreads

假设它们都运行 Ubuntu/Debian,则两台机器上的过程是相同的。

3.启动服务器

安装 iPerf 后,我们需要启动服务器:

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

服务器现在正在侦听 TCP 端口 5001。默认情况下,iPerf 使用 TCP 并将侦听端口 5001。 我们可以包括的可选标志:

  • -u将使服务器使用 UDP 而不是 TCP
  • -p将更改默认端口

例如,让服务器使用 UDP 并监听端口 5003:

## # iperf -s -u -p 5003
Server listening on UDP port 5003
Receiving 1470 byte datagrams
## UDP buffer size:  208 KByte (default)

服务器现在正在侦听 UDP 端口 5003。

4. 从客户端连接到服务器

我们可以使用 TCP 或 UDP。

4.1. 使用TCP

启动服务器后,我们应该从客户端机器连接到它:

## $ iperf -c 5.182.18.49
Client connecting to 5.182.18.49, TCP port 5001
## TCP window size: 85.0 KByte (default)
[  3] local 192.168.1.24 port 38616 connected with 5.182.18.49 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.1 sec  6.12 MBytes  5.10 Mbits/sec

iperf -c 5.182.18.49将开始连接到 IP 地址为 5.182.18.49 的服务器。

我们可以看到使用了 85 KB 的默认 TCP 窗口大小。此外,间隔时间为 10 秒。此外,还传输了 6.12 MB。最后,带宽为 5.10 Mbits/sec,这表明了性能。

我们可以包括的可选标志:

  • -i以秒为单位指定间隔时间。10 是默认值
  • -t指定以秒为单位运行测试的时间
  • -p指定端口。5001 是默认值
  • -w指定 TCP 窗口大小。85 KB 是默认值

让我们使用一些可选的标志:

##  iperf -c 5.182.18.49 -i 5 -t 15 -w 416K -p 5003
Client connecting to 5.182.18.49, TCP port 5003
## TCP window size:  416 KByte
[  3] local 192.168.1.24 port 47300 connected with 5.182.18.49 port 5003
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0- 5.0 sec  2.88 MBytes  4.82 Mbits/sec
[  3]  5.0-10.0 sec  2.75 MBytes  4.61 Mbits/sec
[  3] 10.0-15.0 sec  2.75 MBytes  4.61 Mbits/sec
[  3]  0.0-15.2 sec  8.38 MBytes  4.62 Mbits/sec

我们将间隔时间设置为 5 秒。此外,我们将测试持续时间设置为 15 秒,将 TCP 窗口大小设置为 416 KB。最后,我们将端口设置为 5003。最重要的是,服务器需要监听端口 5003 而不是 5001 才能建立连接

4.2. 使用 UDP

或者,我们可以使用 UDP 而不是 TCP。当然,服务器也需要使用UDP。

## $ iperf -c 5.182.18.49 -u
Client connecting to 5.182.18.49, UDP port 5001
Sending 1470 byte datagrams, IPG target: 11215.21 us (kalman adjust)
## UDP buffer size:  208 KByte (default)
[  3] local 192.168.1.24 port 45640 connected with 5.182.18.49 port 5001
[  3] WARNING: did not receive ack of last datagram after 10 tries.
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec  1.25 MBytes  1.05 Mbits/sec
[  3] Sent 892 datagrams

我们注意到带宽比 TCP 低得多。原因是iPerf 默认将 UDP 的带宽限制为 1Mbits/sec。但是,我们可以通过添加*-b*标志来替换限制:

## $ iperf -c 5.182.18.49 -u -b 1000M
Client connecting to 5.182.18.49, UDP port 5001
Sending 1470 byte datagrams, IPG target: 11.22 us (kalman adjust)
## UDP buffer size:  208 KByte (default)
[  3] local 192.168.1.24 port 56981 connected with 5.182.18.49 port 5001
[  3] WARNING: did not receive ack of last datagram after 10 tries.
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec   180 MBytes   151 Mbits/sec
[  3] Sent 128140 datagrams

我们可以看到现在带宽明显更高了。