linux下ab压力测试工具 查看ab版本 ab -V

ab是apache自带的压力测试工具。ab是apachebench命令的缩写。

ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。


1.吞吐率(Requests per second)

概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。

计算公式:总请求数 / 处理完成这些请求数所花费的时间,即

Request per second = Complete requests / Time taken for tests

2.并发连接数(The number of concurrent connections)

概念:某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。

3.并发用户数(The number of concurrent users,Concurrency Level)

概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。

4.用户平均请求等待时间(Time per request)

计算公式:处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即

Time per request = Time taken for tests /( Complete requests / Concurrency Level)

5.服务器平均请求等待时间(Time per request: across all concurrent requests)

计算公式:处理完成所有请求数所花费的时间 / 总请求数,即

Time taken for / testsComplete requests

可以看到,它是吞吐率的倒数。

同时,它也=用户平均请求等待时间/并发用户数,即

Time per request / Concurrency Level

安装:

yum -y install httpd-tools 

查看ab版本 

ab -V


命令的参数:

格式: ./ab [options] [http://]hostname[:port]/path
参数:
//总请求个数。默认时,仅执行一个请求
-n requests Number of requests to perform

//并发数。默认是一次一个。
-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

//以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-w Print out results in HTML tables

// 执行HEAD请求,而不是GET。
-i Use HEAD instead of GET

//设置<table>属性的字符串。
-x attributes String to insert as table attributes

//设置<tr>属性的字符串。
-y attributes String to insert as tr attributes

//设置<td>属性的字符串。
-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)

//对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")。
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)

//对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即,是否发送了401认证需求代码),此字符串都会被发送。
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.

//-P proxy-auth-username:password 对一个中转代理提供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

//启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。
-k Use HTTP KeepAlive feature

//不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)。
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.

//把所有测试结果写入一个'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。
-g filename Output collected data to gnuplot format file.

//产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比'gnuplot'格式更有用。
-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 -n 1000 -c 120 -p post_data.txt -H "authorization: Bearer $XXX" -T 'application/json' http://hostname:XXXX/XXX


ab压力测试结果:


This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
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
Total transferred:           296000 bytes                 #请求总数据大小(包括header头信息)
Total body sent:             1576000                        
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)

猜你喜欢

转载自blog.csdn.net/weixin_41926234/article/details/80747808
ab
今日推荐