一般除了功能性测试,我们还需要对产品进行性能测试。比如,这个网站目前能承受多大的访问量,在大访问量的情况下,性能及响应时间如何。这些指标会产生一个直接的效果,就是用户体验的好坏。
注意:压力测试的结果跟实际负载的结果不会完全相同。我们只能去模拟环境,做到心里有数。因此,面对ddos/cc攻击的时候,我还是需要用第三发的攻击的防御产品。
原理
ab命令会创建根据用户指令建立大量的并发访问线程,模拟多个访问者同时对某一URL地址进行访问。
参数说明
ab: invalid URL
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-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.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
常用参数
-n:执行请求的数量
-c:每个请求的并发连接数
-t:等待响应时间
-V:显示版本信息
测试
在控制台执行如下命令:
ab -t 60 -c 100 http://blog.csdn.net/wik_123
注意:网站地址,必须在后方加上"/",或指定相应文件。
结果分析
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking blog.csdn.net (be patient)
Completed 5000 requests
Completed 10000 requests
Finished 11770 requests
#Web服务器引擎
Server Software: openresty
#服务器地址
Server Hostname: blog.csdn.net
#服务器端口
Server Port: 80
#请求的文件路径
Document Path: /wik_123
#文件大小
Document Length: 162 bytes
#并发次数
Concurrency Level: 100
#测试所需时间
Time taken for tests: 60.026 seconds
#成功请求次数
Complete requests: 11770
#失败请求次数
Failed requests: 0
#写入错误
Non-2xx responses: 11776
#测试过程传输字节数
Total transferred: 3815424 bytes
#HTML内容传输字节数
HTML transferred: 1907712 bytes
#平均返回数据时间
Requests per second: 196.08 [#/sec] (mean)
#平均响应时间
Time per request: 509.993 [ms] (mean)
#平均并发请求时间
Time per request: 5.100 [ms] (mean, across all concurrent requests)
#平均传输速率
Transfer rate: 62.07 [Kbytes/sec] received
#响应时间小、中、大值
Connection Times (ms)
min mean[+/-sd] median max
Connect: 118 302 422.8 142 4418
Processing: 118 200 212.0 143 5902
Waiting: 118 199 202.9 142 3527
Total: 249 502 475.4 286 7116
Percentage of the requests served within a certain time (ms)
50% 286
66% 294
75% 307
80% 857
90% 1344
95% 1361
98% 1927
99% 2407
100% 7116 (longest request)
#整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中50%的用户响应时间小于286毫秒,66%的用户响应时间小于294毫秒,最大的响应时间小于7116毫秒。对于并发请求,cpu实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以基本上第一个Time per request时间约等于第二个Time per request时间乘以并发请求数。。
总结
ab是一个很方便的工具,安装也特别方便,有时间可以了解学习一下。
有时候由于网络延时等问题,会造成测试结果不准确,因此,建议小伙伴们在内网环境测试,这样准确度会提高。
博客已迁移,欢迎关注 最新博客