在网络攻击日益严重的今天,一个网站上线之前的压力测试是十分重要的,这不仅仅可以检测网站的并发负载能力,也可以检测网站对于各种黑客攻击的抵御,目前的DDoS和CC攻击十分常见,通常情况下黑客惯用的攻击手段无非是用肉鸡或者webshell的js来发送请求,来将我们的服务器打死(非法或无效请求过多占满服务器资源,使服务器无非响应处理正常请求),通常的做法一般也就是加缓存或者做IP防护(基于平时的积累),而有些大型的攻击会将你的带宽占满,这种是最不好防御的,最后请求没有到达服务器,上层交换机的带宽被打满了。这种攻击的详细内容会在后面讲解,接下来我们会介绍一种简单的网站压力测试工具,Apache自带的一款简单的压力测试工具ApacheBench。
在Win系统下安装Apache,在Apache的bin目录下会有一个名字叫ab.exe的文件,而Unix或者Linux上面我们装完Apache之后就会有ab(ApacheBench),我们运行ab -h(写这篇文章的时候是在win上,所以下面的演示都是win上的,Linux上几乎相同)
-n requests Number of requests to perform(负载测试)
-c concurrency Number of multiple requests to make(并发测试)
-t timelimit Seconds to max. wait for responses
-p postfile File containing data to POST
-T content-type Content-type header for POSTing
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. ‘Apache=1234. (repeatable)
-H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-h Display usage information (this message)
上面那个命令一般是我们最常用的的,意思是对www.xxx.com的主页发送2000个请求(-n 代表发多少请求)1000个并发(-c 代表并发的请求)
Server Software: Apache/2.4.7
Server Hostname: www.xxx.com
Server Port: 80
Document Path: /index.php
Document Length: 17312 bytes
Concurrency Level: 1000
Time taken for tests: 339.103 seconds
Complete requests: 2000
Failed requests: 3
(Connect: 3, Receive: 0, Length: 0, Exceptions: 0)
Total transferred: 35940144 bytes
HTML transferred: 34624000 bytes
Requests per second: 5.90 [#/sec] (mean)
Time per request: 169551.607 [ms] (mean)
Time per request: 169.552 [ms] (mean, across all concurrent requests)
Transfer rate: 103.50 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 35 138 218.2 119 9047
Processing: 482 128178 43025.7 143197 185146
Waiting: 156 89750 50700.2 87897 181836
Total: 656 128316 43025.8 143337 185260
Percentage of the requests served within a certain time (ms)
50% 143337
66% 155779
75% 156247
80% 163121
90% 164492
95% 174654
98% 181557
99% 182233
100% 185260 (longest request)
对于性能指标Requests per second来说吞吐率越高,服务器性能越好。在实际的场景中,可能因为网速的问题造成测试结果不准确,如果可以进行内网的测试,那得到的数据就会准确好多,并且这个工具仅仅是最初级的压力测试工具,而在互联网中真正的线上环境或者攻击要比这种测试强烈许多倍。
本文为范骏原创文章,转载无需和我联系,但请注明来自范骏博客https://fjun.org
最新评论