摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 但愿转载,保留摘要,谢谢!html
“我老是但愿本身不要过于草率地去批评一我的” -《傲慢与偏见》nginx
写代码,处处是指标和数据的。在写完接口,为了保证代码在必定压力下没问题,咱们须要对接口进行压测。好比写完一模块的RestAPI,就须要用Web压测工具进行压测。压测工具备不少,新潮的getling,还有Old版的ApacheBench压测。web
"ApacheBench (ab) is a single-threaded command line computerprogram for measuring the performance of HTTP web servers." AB是一个测试HTTP WEB服务器性能的单线程命令程序。实战才是真理,但须要学会怎么使用AB命令。 json
经过简单的指令 ab -h ,就能够获得AB命令参数列表。以下:windows
ab -h 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 测试时间 以上通常搭配 -n & -c 或者 -n & -t -k HTTP保持Keep-Alive方式(长链接) -p POST方式下发送的数据 -T POST或PUT方式下的Content-type 服务器
从经常使用命令参数中,咱们用案例说话: a. 同时10个请求,连续点击1000次(每次Request完毕自动销毁,从新请求)cookie
ab -n 1000 -c 10 http://www.test.com/index.html
b. 同时10个请求,连续点击1000次,并保持Keep-Alive方式(保持长链接的方式)网络
ab -n 1000 -c 10 -k http://www.test.com/index.html
c. 同时10个请求,请求测试时间为20s并发
ab -c 10 -t 20 http://www.test.com/index.html
d. 将请求性能详情输出到CSV档文件app
ab -e output.csv -n 1000 -c 10 http://www.test.com/index.html
out.csv: {请求序号 - 请求耗时ms} e. 包含Post的数据的文件,例如POST发送JSON格式数据
ab -p p.json -T application/json -n 1000 -c 10 http://www.test.com/index.html
p.json:
{"mallId" : 1, "itemIds" : [9102,9101]}
好比 咱们拿简单案例第一个测试,获得了返回报告以下:
Server Software: nginx/1.9.15 Server Hostname: www.test.com Server Port: 80 Document Path: /index.html Document Length: 161 bytes Concurrency Level: 10 Time taken for tests: 5.760 seconds Complete requests: 100 Failed requests: 0 Non-2xx responses: 100 Total transferred: 35200 bytes HTML transferred: 16100 bytes Requests per second: 17.36 [#/sec] (mean) Time per request: 575.955 [ms] (mean) Time per request: 57.595 [ms] (mean, across all concurrent requests) Transfer rate: 5.97 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 179 197 28.3 187 320 Processing: 179 240 236.8 186 2074 Waiting: 179 231 229.2 186 2073 Total: 360 437 238.4 374 2263 Percentage of the requests served within a certain time (ms) 50% 374 66% 381 75% 418 80% 427 90% 513 95% 567 98% 1685 99% 2263 100% 2263 (longest request)
指标太多,咱们要去看关键指标,好比 ----关键指标---- Failed requests 失败的请求数。若是太多证实Web服务器稳定度不够 Request per second 每秒请求的次数。表明web服务器的承载量,即吞吐量(不考虑带宽限制) Time per request:855.897 [ms] (mean)用户平均请求等待时间。能够经过1000ms/Time per request算出每秒的事务数
QPS ,mean是平均值 Time per request:85.590 [ms] (mean, across all concurrent requests) 服务器平均处理每个并发请求的时间。能够1000ms/Time per request算出平均事务响应时间,mean是平均值
详细解释以下:
Server Software: 服务器名称(从http响应数据的头信息获取) Server Hostname: 服务器Host名称(从http请求数据的头信息获取) Server Port: 服务器端口 Document Path: 请求的根绝对路径, 默认为 / Document Length: 响应数据的正文大小, 单位为 Byte Concurrency Level: 并发用户数(命令中表现为-c后面跟着的参数) Time taken for tests: 全部请求测试的总耗时(若是是 -t 参数,则为该数值;若是是 -n 请求,通常来讲 值越大,耗时越多) Complete requests: 总请求数(-n 参数值) Failed requests: 失败的请求数(2XX失败的请求除外) Write errors: 写入失败的数量 Total transferred: 数据传输量 HTML transferred: 全部请求的响应数据长度总和 Requests per second: 每秒请求的次数。表明web服务器的承载量,即吞吐量(不考虑带宽限制) Time per request: 用户平均请求等待时间 Time per request: 服务器平均处理每个并发请求的时间 Transfer rate: 请求在单位时间内从服务器获取的数据长度
压测中,咱们要关注服务器的性能,通常28原则。即 1. 压测要将某个指标压到 80%左右。好比Linux服务器 top命令,实时查看服务器性能:
top - 15:22:08 up 41 days, 23:39, 1 user, load average: 0.21, 0.05, 0.02 Tasks: 392 total, 1 running, 391 sleeping, 0 stopped, 0 zombie Cpu0 : 1.0%us, 0.0%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ P COMMAND 24425 root 20 0 13728 1580 944 R 1.0 0.1 0:00.98 0 top
关键指标达到 80% ,得出报告最准确。 load average (0.21, 0.05, 0.02)- 系统负载,即 1分钟、5分钟、15分前的任务队列的平均长度,因此能够看第一个是否达到80% Cpu0 CPU占用百分比 wa 等待输入输出的CPU时间百分比,即网络IO操做 2. 按部就班:同时请求数阶梯式加进去。达到上面某个指标的域 3. 服务器压测服务器,不用多说 4. 排除带宽限制、多台服务器测试
如以上文章或连接对你有帮助的话,别忘了在文章结尾处评论哈~ 你也能够点击页面右边“分享”悬浮按钮哦,让更多的人阅读这篇文章。