简单的初级压力测试
在网络攻击日益严重的今天,一个网站上线之前的压力测试是十分重要的,这不仅仅可以检测网站的并发负载能力,也可以检测网站对于各种黑客攻击的抵御,目前的 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(并发测试)
-
测试所进行的最大秒数。其内部隐含值是 - n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
-t timelimit Seconds to max. wait for responses
-
包含了需要 POST 的数据的文件.
-p postfile File containing data to POST
-
POST 数据所使用的 Content-type 头信息。
-T content-type Content-type header for POSTing
-
设置显示信息的详细程度 4 或更大值会显示头信息,3 或更大值可以显示响应代码 (404,200 等),2 或更大值可以显示警告和其他信息。-V 显示版本号并退出。
-v verbosity How much troubleshooting info to print
-
以 HTML 表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-w Print out results in HTML tables
-
执行 HEAD 请求,而不是 GET。
-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 cookie-name=value 对请求附加一个 Cookie: 行。 其典型形式是 name=value 的一个参数对。此参数可以重复。
-C attribute Add cookie, eg. ‘Apache=1234. (repeatable)
-
attribute 是设置属性的字符串。缺陷程序中有各种静态声明的固定长度的缓冲区。另外,对命令行参数、服务器的响应头和其他外部输入的解析也很简单,这可能会有不良后果。它没有完整地实现 HTTP/1.x; 仅接受某些’预想’的响应格式。strstr (3) 的频繁使用可能会带来性能问题,即你可能是在测试 ab 而不是服务器的性能。
-
设置请求头有关的参数,几乎可以设置所有的正确请求头
-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 proxy-auth-username:password v 对一个中转代理提供 BASIC 认证信任。用户名和密码由一个:隔开,并以 base64 编码形式发送。无论服务器是否需要 (即,是否发送了 401 认证需求代码),此字符串都会被发送。
-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)
一般的常用命令是./ab -n 2000 -c 1000 http://www.xxx.com/index.php#
上面那个命令一般是我们最常用的的,意思是对 www.xxx.com 的主页发送 2000 个请求 (-n 代表发多少请求) 1000 个并发 (-c 代表并发的请求)
接下来我们会得到这样的响应:#
响应参数详解:#
-
测试服务器版本
Server Software: Apache/2.4.7
-
测试域名或 IP
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 内容字节
HTML transferred: 34624000 bytes
-
指的是吞吐率,相当于 LR 中的每秒事务数,后面括号中的 mean 表示这是一个平均值
Requests per second: 5.90 [#/sec] (mean)
-
用户平均请求等待时间,相当于 LR 中的平均事务响应时间,后面括号中的 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
-
请求响应时间的分布,在场景中每个请求都有一个响应时间。对于并发请求,cpu 实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以基本上第一个 Time per request 时间约等于第二个 Time per request 时间乘以并发请求数。
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 来说吞吐率越高,服务器性能越好。在实际的场景中,可能因为网速的问题造成测试结果不准确,如果可以进行内网的测试,那得到的数据就会准确好多,并且这个工具仅仅是最初级的压力测试工具,而在互联网中真正的线上环境或者攻击要比这种测试强烈许多倍。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: