Linux带宽测试

iperf是linux下一款非常实用的测带宽工具,是C/S模型的,支持使用TCP和UDP测试,可以测试带宽,丢包(丢包指的是UDP的丢包,TCP丢包会重传)等信息。若想使用此工具必须服务端必须是公网可访问的机器。

iperf原理

服务端监听监听在某个端口,客户端对该端口发送流量,由此可以测的客户端带宽的相关信息。(由于一般都是测试服务器的带宽,因此测试的时候需要有两台拥有公网地址的机器)。

带宽换算

通信公司说的带宽一般为Gb/s 、Mb/s;而我们使用的带宽实际说的是GB/s、MB/s;所以通信公司所给的带宽需要除以8进行换算。

以下分别用两台带宽分别为5Mb与100Mb的的主机互相进行测试

ct5           代表带宽为5Mb的主机

ct100       代表带宽为100Mb的主机

一、安装

以Centos8为例

[root@01-0001 yum.repos.d]# wget https://download-ib01.fedoraproject.org/pub/epel/8/Everything/aarch64/Packages/e/epel-release-8-16.el8.noarch.rpm
[root@01-0001 yum.repos.d]# rpm -ivh epel-release-8-16.el8.noarch.rpm 
[root@01-0001 yum.repos.d]# yum install iperf

二、测试

测试5M带宽的主机,带宽是否为能达到5M
1.tcp方式
服务器端

[root@ecs-29d1 ~]# iperf -s
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
[  4] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 57730 (peer 2.1.6)
[ ID] Interval       Transfer     Bandwidth
[  4]  0.0-10.4 sec  6.13 MBytes  4.95 Mbits/sec
[  4] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 57732 (peer 2.1.6)
[  4]  0.0-10.3 sec  6.00 MBytes  4.88 Mbits/sec

客户端:

[root@01-0001 ~]# iperf -c ct100 -t 10 -i 1
------------------------------------------------------------
Client connecting to ct100, TCP port 5001
TCP window size: 85.0 KByte (default)
------------------------------------------------------------
[  1] local 192.168.0.215 port 57732 connected with 36.112.136.105 port 5001
[ ID] Interval       Transfer     Bandwidth
[  1] 0.00-1.00 sec   768 KBytes  6.29 Mbits/sec
[  1] 1.00-2.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 2.00-3.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 3.00-4.00 sec   512 KBytes  4.19 Mbits/sec
[  1] 4.00-5.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 5.00-6.00 sec   384 KBytes  3.15 Mbits/sec
[  1] 6.00-7.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 7.00-8.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 8.00-9.00 sec   512 KBytes  4.19 Mbits/sec
[  1] 9.00-10.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 10.00-10.33 sec   128 KBytes  3.21 Mbits/sec
[  1] 0.00-10.33 sec  6.00 MBytes  4.87 Mbits/sec

2.udp方式
服务器端:

[root@ecs-29d1 ~]# iperf -s -u
------------------------------------------------------------
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  3] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 44211 (peer 2.1.6)
[ ID] Interval       Transfer     Bandwidth        Jitter   Lost/Total Datagrams
[  3]  0.0-10.0 sec  1.25 MBytes  1.05 Mbits/sec   0.017 ms    1/  895 (0.11%)
[  4] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 55893 (peer 2.1.6)
[  4]  0.0-10.0 sec  6.04 MBytes  5.07 Mbits/sec   0.018 ms 4612/ 8920 (52%)
[  3] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 47713 (peer 2.1.6)
[  4] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 45984 (peer 2.1.6)
[  4]  0.0-30.1 sec  5.53 MBytes  1.54 Mbits/sec   0.030 ms 4973/ 8920 (56%)
[  5] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 44776 (peer 2.1.6)
[  5]  0.0-46.1 sec  5.75 MBytes  1.05 Mbits/sec   0.021 ms  360/ 4462 (8.1%)
[  4] local 192.168.1.77 port 5001 connected with 36.112.130.213 port 52849 (peer 2.1.6)
[  4]  0.0-59.5 sec  5.37 MBytes   757 Kbits/sec   0.050 ms  632/ 4462 (14%)

客户端:

//设置为10M带宽进行测试
[root@01-0001 ~]# iperf -c ct100 -t 10 -i 1 -u -b 10M
------------------------------------------------------------
Client connecting to ct100, UDP port 5001
Sending 1470 byte datagrams, IPG target: 1121.52 us (kalman adjust)
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  1] local 192.168.0.215 port 45984 connected with 36.112.136.105 port 5001
[ ID] Interval       Transfer     Bandwidth
[  1] 0.00-1.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 1.00-2.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 2.00-3.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 3.00-4.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 4.00-5.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 5.00-6.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 6.00-7.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 7.00-8.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 8.00-9.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 9.00-10.00 sec  1.25 MBytes  10.5 Mbits/sec
[  1] 0.00-10.00 sec  12.5 MBytes  10.5 Mbits/sec
[  1] Sent 8921 datagrams
[  1] Server Report:
[ ID] Interval       Transfer     Bandwidth        Jitter   Lost/Total Datagrams
[  1] 0.00-0.00 sec  0.000 Bytes  -nan bits/sec  131.073 ms 4294966296/0 (inf%)


//设置为5M带宽进行测试
[root@01-0001 ~]# iperf -c ct100 -t 10 -i 1 -u -b 5M
------------------------------------------------------------
Client connecting to ct100, UDP port 5001
Sending 1470 byte datagrams, IPG target: 2243.04 us (kalman adjust)
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  1] local 192.168.0.215 port 44776 connected with 36.112.136.105 port 5001
[ ID] Interval       Transfer     Bandwidth
[  1] 0.00-1.00 sec   642 KBytes  5.26 Mbits/sec
[  1] 1.00-2.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 2.00-3.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 3.00-4.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 4.00-5.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 5.00-6.00 sec   639 KBytes  5.23 Mbits/sec
[  1] 6.00-7.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 7.00-8.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 8.00-9.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 9.00-10.00 sec   640 KBytes  5.24 Mbits/sec
[  1] 0.00-10.00 sec  6.26 MBytes  5.25 Mbits/sec
[  1] Sent 4463 datagrams
[  1] Server Report:
[ ID] Interval       Transfer     Bandwidth        Jitter   Lost/Total Datagrams
[  1] 0.00-0.00 sec  0.000 Bytes  -nan bits/sec  131.073 ms 4294966296/0 (inf%)

用UDP测试时注意查看服务端的丢包情况,可以发现当设置带宽为10M,会有接近50%的丢包,而当带宽设置为5M,丢包显著下降只有10%左右,而带宽设置为1M的时候,丢包为0.11%,说明了客户端带宽为5M左右,同时也说明UDP本身传输就不可靠,即使带宽设置的很低也存在丢包。

同理测试100M主机的带宽

//服务端
[root@01-0001 ~]# iperf -s
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
[  1] local 192.168.0.215 port 5001 connected with 36.112.136.105 port 52722
[ ID] Interval       Transfer     Bandwidth
[  1] 0.00-10.04 sec   104 MBytes  87.3 Mbits/sec
[  2] local 192.168.0.215 port 5001 connected with 36.112.136.105 port 52724
[ ID] Interval       Transfer     Bandwidth
[  2] 0.00-10.08 sec   109 MBytes  90.4 Mbits/sec


//客户端
[root@ecs-29d1 ~]# iperf -c ct5 -t 10 -i 1
------------------------------------------------------------
Client connecting to ct5, TCP port 5001
TCP window size:  374 KByte (default)
------------------------------------------------------------
[  3] local 192.168.1.77 port 52724 connected with 36.112.130.213 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0- 1.0 sec  5.62 MBytes  47.2 Mbits/sec
[  3]  1.0- 2.0 sec  12.4 MBytes   104 Mbits/sec
[  3]  2.0- 3.0 sec  12.0 MBytes   101 Mbits/sec
[  3]  3.0- 4.0 sec  11.2 MBytes  94.4 Mbits/sec
[  3]  4.0- 5.0 sec  10.5 MBytes  88.1 Mbits/sec
[  3]  5.0- 6.0 sec  11.5 MBytes  96.5 Mbits/sec
[  3]  6.0- 7.0 sec  12.2 MBytes   103 Mbits/sec
[  3]  7.0- 8.0 sec  11.8 MBytes  98.6 Mbits/sec
[  3]  8.0- 9.0 sec  9.62 MBytes  80.7 Mbits/sec
[  3]  9.0-10.0 sec  11.8 MBytes  98.6 Mbits/sec
[  3]  0.0-10.1 sec   109 MBytes  90.6 Mbits/sec

iperf常用参数说明:

适用客户端/服务器:

-f --格式[k|m|K|M] 分别表示以Kbits、Mbits、KBytes、MBytes显示报告,默认是Mbits
-i 以秒为单位统计带宽值 
-l 读写缓冲区大小,默认是8KB
-m 显示最大的TCP数据段大小 (MTU - TCP/IP header)
-o 将报告和错误信息输出到文件
-p 指定服务器和客户端连接的端口
-u 使用udp协议
-w 指定TCP窗口大小,默认是8KB
-B 绑定一个主机地址或接口(当主机有多个地址或接口时使用该参数)
-C 兼容旧版本(当server端和client端版本不一样时使用)
-M 设定TCP数据包的最大mtu值
-N 设定TCP不延时
-V 传输ipv6数据包

适用服务器端:

-s 以服务器模式启动
-U 单线程UDP模式下运行
-D 以守护进程模式运行

适用客户端:

-b 指定客户端通过UDP协议发送信息的带宽,默认值为1Mbit/s
-c 指定服务器地址  
-d 同时进行双向传输测试
-n 指定传输的字节数
-r 单独进行双向传输测试
-t 指定Iperf测试时间,默认10秒
-F 指定需要传输的文件
-I 从标准输入(stdin)中读取要传输的数据 
-L 指定一个端口,服务器将利用这个端口与客户机连接
-P 客户端到服务器的连接数,默认值为1
-T 指定ttl值

带宽也可以配合scp进行快速测试,只需一个公网地址,节省资源


ct100为带宽100Mb/s的主机
[root@zyq tmp]# scp -P 5499 ct100:/boot/initramfs-0-rescue-6748d698cc244bdebd23ef4793c8dd1d.img /tmp/
initramfs-0-rescue-6748d698cc244bdebd23ef4793c8dd1d.img                            100%   56MB   8.5MB/s   00:06    

ct5为带宽5Mb/s的主机
[root@zyq tmp]# scp ct5:/boot/initramfs-4.18.0-240.el8.x86_64.img /tmp/
initramfs-4.18.0-240.el8.x86_64.img                                                100%   28MB 576.2KB/s   00:49    

你可能感兴趣的:(linux,centos,tcp/ip,网络,服务器)