ab是apache自带的压力测试工具。ab是apachebench命令的缩写。
ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。
ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。
概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
计算公式:总请求数 / 处理完成这些请求数所花费的时间,即
Request per second = Complete requests / Time taken for tests
概念:某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。
概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。
计算公式:处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level)
计算公式:处理完成所有请求数所花费的时间 / 总请求数,即
Time taken for / testsComplete requests
可以看到,它是吞吐率的倒数。
同时,它也=用户平均请求等待时间/并发用户数,即
Time per request / Concurrency Level
安装:
yum -y install httpd-tools
查看ab版本
ab -V
命令的参数:
格式: ./ab [options] [http://]hostname[:port]/path参数:
//总请求个数。默认时,仅执行一个请求
//并发数。默认是一次一个。
-c concurrency Number of multiple requests to make
//POST数据文件. -d参数
-p postfile File containing data to POST
//测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
-t timelimit Seconds to max. wait for responses
//Content-type头信息。
-T content-type Content-type header for POSTing
//设置显示信息的详细程度 - 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
// 执行HEAD请求,而不是GET。
-i Use HEAD instead of GET
//设置<table>属性的字符串。
-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)
//对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")。
-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)
例子:
curl -s -X POST \http://hostname:XXXX/XXX \
-H "authorization: Bearer $XXX" \
-H "content-type: application/json" \
-d '{
"XXX": ["XXX","XXX"],
"XXX":"XXX",
"XXX":["XXX","XXX"]
}')
--------------------------------------------------------------
----------post_data.txt----------
{
"XXX": ["XXX","XXX"],
"XXX":"XXX",
"XXX":["XXX","XXX"]
}
----------------------------------
ab压力测试结果:
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:
Server Hostname: localhost #域名
Server Port: 4000 #请求端口号
Document Path: /XXX #请求端口号
Document Length: 64 bytes #页面字节数
Concurrency Level: 120 #请求的并发数
Time taken for tests: 43.945 seconds #总访问时间
Complete requests: 1000 #请求成功数量
Failed requests: 0 #请求失败数量
Write errors: 0
HTML transferred: 64000 bytes #html页面实际总字节数
Requests per second: 22.76 [#/sec] (mean) #每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request: 5273.443 [ms] (mean) #用户平均请求等待时间
Time per request: 43.945 [ms] (mean, across all concurrent requests)
# 服务器平均处理时间,也就是服务器吞吐量的倒数
Transfer rate: 6.58 [Kbytes/sec] received #每秒获取的数据长度35.02 kb/s sent
41.60 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.8 0 4
Processing: 1724 4660 592.4 4738 6072
Waiting: 1724 4660 592.4 4737 6072
Total: 1724 4661 592.7 4738 6074
Percentage of the requests served within a certain time (ms)
50% 4738 #50%用户请求在4738s内返回
66% 4857
75% 4908
80% 4966
90% 5158
95% 5606
98% 5876
99% 6052
100% 6074 (longest request)