Linux下限制网卡的带宽,可用来模拟服务器带宽耗尽,从而测试服务器在此时的访问效果。
8 q3 Y) D9 | Q& r! x: q 1、安装iproute+ b/ h' ]5 G3 Z* k( N
yum -y install iproute2 W( T( h! B; s8 o f7 [
2、限制eth0网卡的带宽为50kbit:
# C- d) f: g; Q3 d; C5 T5 X7 a /sbin/tc qdisc add dev eth0 root tbf rate 50kbit latency 50ms burst 1000! z; t) m4 V u; [
3、限制带宽为50kbit后,在百兆局域网中wget下载一个大文件:
4 _+ t9 e K- h- b' y$ c' |4 O [root@localhost ~]# wget http://192.168.1.7/test.zip
$ S7 X' a6 q; x# K0 N" O; V( _/ K --19:40:27-- http://192.168.1.7/test.zip, Y$ q8 Z; I) `( ~$ C
Connecting to 192.168.1.7:80... connected.
' [# m8 A- c) f6 n: }" s! F HTTP request sent, awaiting response... 200 OK0 b5 f/ c* n% h+ M
Length: 23862312 (23M) [application/zip]2 a7 o$ d0 U, F9 B
Saving to: `test.zip'
i) ~3 h/ D' T8 h0 | 37% [=======> ] 8,994,816 457K/s eta 27s
- S0 b3 V4 r, Y 下载速度为457K/s,限制效果达到。
$ _% e7 h) `- J& ~$ c 4、解除eth0网卡的带宽限制:) \1 ~6 @" h6 u) M% S
/sbin/tc qdisc del dev eth0 root tbf7 Y$ H. S; p( R& |
5、对比:未作带宽限制情况下,在百兆局域网中wget下载一个大文件:
- R" z4 t# F' C$ }; S' |) p [root@localhost ~]# wget http://192.168.1.7/test.zip0 N& L8 v2 G6 R/ l# ]
--19:44:33-- http://192.168.1.7/test.zip4 S7 X; R# g6 a3 C, t6 u- a
Connecting to 192.168.1.7:80... connected.
1 T0 x+ O( m p1 ^. h# N2 n4 [6 C7 y; J) W HTTP request sent, awaiting response... 200 OK
3 S0 g/ k) _% g! F) z* \8 W Length: 23862312 (23M) [application/zip]/ r- M, G6 r! x5 c* @- d
Saving to: `test.zip'
& I E6 C# k8 w+ U" f4 B% o 100%[==========>] 23,862,312 6.14M/s in 3.7s, h( v4 r! o! z9 G) o
19:44:36 (6.16 MB/s) - `test.zip' saved [23862312/23862312]% g- c S/ W( W; x0 J+ D5 f! l
下载速度为6.16MB/s。 |