如何使用 Iperf 诊断网络速度

Linux 系统管理员和网络管理员经常需要诊断网络速度的问题,但是目前这种专业的工具软件还是比较少的。Iperf 是一个运行在命令行下用来诊断网络速度的专业工具。

Iperf 可以测量出服务器的最大网络吞吐量。这是特别有用的,当出现网络连接速度问题时,可以使用 Iperf 确定哪些服务器无法达到的最大吞吐量。

注意 本文假设您使用的是root用户。如果您没有在超级用户下,需要在每个命令前加上 sudo

安装 Iperf

Debina 和 Ubuntu

您可以使用 apt-get 工具来直接安装,如下:

apt-get install iperf

CentOS

CentOS默认的源没有 Iperf 软件。我们这里使用 RPMForge 源,RPMForge 是 RedHat 类系统下的第三方软件源。RMForge 源不是由 RedHat 自带的,但是它里面有超过 5000 个 RPM 格式的软件,十分好用。

CentOS 7

1,使用 wget 和 rpm 命令安装 RMForge 源:

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
rpm -Uvh rpmforge-release-1.5.3-1.el7.rf.x86_64.rpm

2 使用 yum 命令安装 Iperf

yum update
yum install iperf
CentOS 6

1,使用 wget 和 rpm 命令安装 RPMForge 源:

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm
rpm -Uvh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm

2,使用 yum 工具安装 Iperf:

yum update
yum install iperf

Fedora

使用如下命令安装 Iperf:

yum update yum install iperf

Arch Linux

使用如下命令安装 Arch Linux

pacman -S iperf

Gentoo

使用如下命令安装 Iperf

emerge iperf

注意,您需要首先运行 emerge –sync 命令。另外,您需要使用 /usr/bin/iperf3(取决于您使用的Iperf版本) 来替代每一个 iperf 命令。

使用 Iperf

Iperf 需要同时安装在您需要测试的服务器和客户端上。如果您的本地是类 Linux 系统,您可以使用您的本机当做客户端啦。但是,如果您想要测试 Linode 服务器的最大吞吐量,您需要使用两个Linode服务器做测试,这样网络测试结果不会受到您的本地 ISP 网络状况的影响。

TCP 模式下 客户端&服务器

Iperf 需要两个计算机,一个充当服务器,一个充当客户端。客户端连接需要测速的服务器。

1,在您需要测试的 Linode 上,启用 Iperf 服务器模式:

ipsef -s

您可以看打如下类似的输出:

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

2,在您的第二台Linode VPS上,使用客户端模式连接第一台服务器。把 198.51.100.5 替换成您的第一台Linode 服务器的 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 50549 connected with 198.51.100.5 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec   142 MBytes   119 Mbits/sec

3,您也可以在第一台 Linode 服务器上(Iper f服务端)看到类似输出:

------------------------------------------------------------
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 50549
[ ID] Interval       Transfer     Bandwidth
[  4]  0.0-10.2 sec   142 MBytes   117 Mbits/sec

4,可以使用 CTRL+c 结束 Iperf 进程。

UDP 模式下 客户端&服务端

您也可以使用 Iperf 软件测试 UDP 连接的最大吞吐量

1,在需要测试的 Linode 服务器上开启 UDP 服务端:

iperf -s -u

您可以看打如下输出:

------------------------------------------------------------
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size:  208 KByte (default)
------------------------------------------------------------

2,在客户端上使用如下命令。需要把 198.51.100.5 替换成您第一台 Linode 服务器的地址

iperf -c 198.51.100.5 -u

使用 -u 选项告诉 Iperf 使用 UDP 连接。这是十分重要的,因为我们想看到 UDP 连接的网络状况。输出类似于:

------------------------------------------------------------
Client connecting to 198.51.100.5, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  3] local 198.51.100.6 port 58070 connected with 198.51.100.5 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec  1.25 MBytes  1.05 Mbits/sec
[  3] Sent 893 datagrams
[  3] Server Report:
[  3]  0.0-10.0 sec  1.25 MBytes  1.05 Mbits/sec   0.084 ms    0/  893 (0%)

我们看到 Bandwidth 是 1.05 Mbits/sec,少于我们刚才使用 TCP 测试的结果,也少于 Linod e所提供的网络速度。这是因为 Iperf 软件在 UDP 模式下默认限制 1Mbit/s 。

3,您可以使用 -b 参数来制定我们需要测试的最大网络带宽。如果您需要测出网络的最大可用带宽,最好设置一个大点的数据。例如:在 1GB Linode上,我们测试:

iperf -c 198.51.100.5 -u -b 150m

这就告诉客户端最多可以接收 150Mbits/sec 的带宽。 -b 参数只有在 UDP 模式下可用,因为 Iperf 不限制 TCP 客户端的带宽。

输出结果:

-----------------------------------------------------------
Client connecting to 198.51.100.5, UDP port 5001
Sending 1470 byte datagrams
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  3] local 198.51.100.6 port 41083 connected with 198.51.100.5 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec   145 MBytes   122 Mbits/sec
[  3] Sent 103625 datagrams
[  3] Server Report:
[  3]  0.0-10.3 sec   136 MBytes   111 Mbits/sec  13.488 ms 6464/103623 (6.2%)

现在比我们刚才看到的 1.05 Mbits/sec 结果好多了吧。

双向测试

在某些情况下,我们需要测试两个服务器之间的最大吞吐量,这就需要 Iperf 软件所提供的双向测试功能。

在客户端上运行一下命令来开启双向测试:

iperf -c 198.51.100.5 -d

这样,在客户端上开启了 server 和 client 两个连接。如下输出:

------------------------------------------------------------
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: 45.0 KByte (default)
------------------------------------------------------------
[  3] local 198.51.100.6 port 50550 connected with 198.51.100.5 port 5001
[  5] local 198.51.100.6 port 5001 connected with 198.51.100.5 port 36916
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec   142 MBytes   118 Mbits/sec
[  5]  0.0-10.1 sec   198 MBytes   165 Mbits/sec

在第一台服务器上,输出:

------------------------------------------------------------
Client connecting to 198.51.100.6, TCP port 5001
TCP window size: 45.0 KByte (default)
------------------------------------------------------------
[  6] local 198.51.100.5 port 36916 connected with 198.51.100.6 port 5001
[  6]  0.0-10.0 sec   198 MBytes   166 Mbits/sec
[  5]  0.0-10.2 sec   142 MBytes   117 Mbits/sec

我们需要注意一下 ID 标识,ID 标识可以区分不同服务器测速结果。在本文中,因为 第一台服务器 198.51.100.5 是2GB Linode ,第二台 Linode 198.51.100.6 是 1GB Linode。所以两台服务器的吞吐量有点差别的。

选项

-f 定制测试结果输出格式。例如: 使用 -f k 得到 Kbits/sec 结果的输出。类似的参数有: m (Mbits, default), k (Kbits), K (KBytes), and M (MBytes).

-V 强制 Iperf 使用 IPv6

-i 改变测试带宽的时间间隔。例如:-i 60 每个60秒报告一次带宽。

-p 更换端口。如果不指定,默认端口为 5001。您必须同时在 client 和 server 上指定端口。

-B 指定 Iperf 使用的接口或地址。如果应用在 server 端,入端口会被指定。如果应用在 client 端,出端口会被指定。

更多信息:

Iperf Official Website

总结(译者注):

我们可以使用 Iperf 测试两个 Linode 服务器的网络最大吞吐量,也可以测试任意两个服务器的最大吞吐量。另外,我们可以测试国内连接海外 VPS 的网络最大吞吐量等网络状况。甚至,Iperf 可以用来测试无线 WIFI 的网络状况。比如,我们在有线计算机上和无线计算机上同时部署 Iperf,这样来测试不同环境,不同距离,不同位置,WIFI连接的状况。著名的无线网络测试工具 AirMagnet 底层就运行了 Iperf 。

翻译于:

https://www.linode.com/docs/networking/diagnostics/diagnosing-network-speed-with-iperf

参考:

http://blog.csdn.net/evenness/article/details/7371845

http://sdnhub.cn/index.php/performance-testing-iperf/

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注