性能测试工具ab使用记录

来源:互联网 发布:算法工程师专业 编辑:程序博客网 时间:2024/05/22 04:38

几个重要的性能指标

吞吐率 Requests per second

概念:单位时间内,客户端向服务器成功发送的请求数,也就是每秒事务数。tps、qps。单位reqs/s,是服务器并发处理能力的量化描述。某个并发用户数下单位时间内能处理的最大请求数,称为最大吞吐率。

吞吐率 = 总请求数/处理完成这些请求的总时间

Request Per Second = Complete requests / Time Taken for Test


并发用户数 Concurrency Level  / The number of concurrent users

并发:同时执行同一个动作的数量。和并发连接数有时候有区别的,一个用户会产生多个回话、连接数。


并发连接数 The number of concurrent connections

某时刻服务器所接收的请求数,其实就是一个回话。


平均事务响应时间  用户平均请求等待时间 Time per request

平均每组并发用户并发完所用时间。

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

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


服务器平均等待时间 Time per request: across all concurrent requests

服务器处理每个请求的平均时间,是吞吐率的倒数。

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

Time taken for / testsComplete requests

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

Time per request / Concurrency Level

ab工具

ab全称:Apache bench,是Apache自带的可以对Apache、Nginx、Tomcat、IIS、lighthttp等web服务器网站访问进行压力测试。

ab命令的原理

  1. ab命令可以创建很多并发访问线程,模拟多个访问者同时对某一URL地址进行访问,它的测试目标是基于URL的,所以可以用来测试很多web服务器的负载能力。
  2. ab命令对发出负载的计算机配置要求很低,不会占用很高的CPU也不会暂用很高的内存(CPU核心、内存还是越大越好),却可以对目标服务器造成巨大的负载,其原理类似于CC攻击。一次上太多的负载,可能造成目标服务器因资源耗完,严重时甚至导致死机。
  3. Apache ab也是一种ddos攻击工具。

ab注意事项

  1. 在远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大),建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。如果只有单独的一台服务器,可以直接本地测试,比远程测试效果要准确。
  2. ab可以完成不复杂的的压力测试,比较适合单一的URL压力测试,比喻测试某个前端页面的访问压力、接口压力测试等;
  3. ab判断成功与否,只能通过2XX的状态码为依据,不接受服务器的返回值,loadrunner是可以接受服务器完成的返回值的,所以在相同的响应时间下,ab支持的并发数量会大于loadrunner,tps也会大于loadrunner;
  4. ab运行并发的时候和所在运行机器上的cpu颗数有关,越多则并发越大。在linux下支持的并发大于在Windows下
  5. 大并发下数据不准,更多的是看ab本身的性能了而不是服务端的性能;
  6. ab是不支持HTTPS请求的(测试时可以让开发把IP改为http的)

ab安装

yum install  httpd

安装Apache后一般都会有ab命令的,使用which ab 就可以看到ab的路径。

ab的使用

ab语法:

ab  -n 请求总数   -c 并发用户数   -t  测试时间   -k  (发送keep-alive指令到服务器端) -w  "http://www.xxxxx/path?userId=12345"   >> /data/report/test.html

eg:

ab  -n  10000   -c 100  -t  50000 -k -w https://www.baidu.com/  >> ./test.html

说明:上面例子表示:总共访问百度 10000次,每次并发 100 测试总时间是50000s,keepalive连接,测试结果已HTML格式保存在当前目录下的test.html中

ab参数说明

-n :总共的请求执行数,缺省是1;
-c: 并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s;

-k:发送keep-alive指令到服务器;

-T:header头内容类型,T是大写的,内容是字符串形式;

-p:post请求,后面加post参数文档路径,默认为当前路径或者-p后可以是json格式、可以是&连接参数,放在""双引号之间;

-w:以HTML表格输出结果,默认是两列宽度的一个表;

-C:请求附加一个cookie,典型形式是cookieName = value;C大写

-H:请求附加多个cookie;-H  “Cookie: key1=value1; key2=value2”

-i:执行的是head请求不是get;

ab发送无参数的get请求

ab -n 1000 -c 100  http://www.xxx.com/

Benchmarking www.xxx.com (be patient).....doneServer Software:        BWS/1.1Server Hostname:        www.baidu.comServer Port:            443SSL/TLS Protocol:       TLSv1/SSLv3,ECDHE-RSA-AES128-GCM-SHA256,2048,128Document Path:          /Document Length:        227 bytesConcurrency Level:      10               并发数量Time taken for tests:   0.554 seconds    测试时间Complete requests:      100              总请求数Failed requests:        0Write errors:           0Keep-Alive requests:    0Total transferred:      87200 bytesHTML transferred:       22700 bytesRequests per second:    180.57 [#/sec] (mean)    吞吐量Time per request:       55.381 [ms] (mean)       每个并发组请求的平均时间Time per request:       5.538 [ms] (mean, across all concurrent requests)   服务器处理每个请求的时间Transfer rate:          153.76 [Kbytes/sec] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:       25   37   6.3     37      57Processing:     9   13   3.4     12      27Waiting:        9   13   2.9     12      22Total:         35   50   7.1     48      71Percentage of the requests served within a certain time (ms)  50%     48  66%     53  75%     54  80%     56  90%     59  95%     63  98%     70  99%     71 100%     71 (longest request)

发送get请求

ab -n 100000 - 1000 -w -k "http://114.55.34.196:5000/channelPage/280000448024533.htmresTime=50&userId=270000127243445"

注意:请求URL需要用双引号括起来,否则ab无法识别&符号

在测试页面压力中,需要持续一定的时间才能完成压力测试,可以使用循环使得整个测试满足时间需求:

for i in `req 20`; do ab -n 100000 - 1000 -w -k "http://114.55.34.196:5000/live/channel/channelPage/280000448024533.htm?resTime=50&userId=270000127243445" 

使用for循环,执行ab测试20边,对对服务器持续造成压力;

发送post请求

1.post文档

ab -n 10000  -c 1000  -k -T "application/json"   -p  data.txt   http://inner.test1.qlchat.com/add/comment

data.txt  放在执行ab命令的当前目录,也可以指定路径

{
    "id": "1502694903355097",
    "sign": "8636eda2e42ab986ac64612354825839",
    "timestamp": 1502694903355,
    "data": {
        "topicId": "100000036990229",
        "speaker": "?右右",
        "status": "Y",
        "userId": "100000348907198"
    }
}

2.post json串

ab -n 100 -c 10 -k -T "application/json" -w -p "{'key1': 'value1', 'key2': 'value2'}"  "http://httpbin.org/post"  >> report.html

3.post 参数值

ab -n 100 -c 10 -k -T "application/json" -p "key1=value1&key2=value2"  "http://httpbin.org/post" 

Benchmarking httpbin.org (be patient).....doneServer Software:        meinheld/0.6.1#web服务器名称Server Hostname:        httpbin.org#主机地址Server Port:            80#端口号Document Path:          /post                                    #post路径Document Length:        178 bytes#请求数据长度Concurrency Level:      10                                      #并发用户Time taken for tests:   8.393 seconds                           #测试时间Complete requests:      100                                     #总请求数Failed requests:        0Write errors:           0Non-2xx responses:      100#http状态码2xx请求成100个请求Keep-Alive requests:    100Total transferred:      51179 bytes#响应数据总长度HTML transferred:       17800 bytes#响应数据除去响应头外,响应正文信息长度Requests per second:    11.92 [#/sec] (mean)                    #吞吐量 每秒事务数  qpsTime per request:       839.277 [ms] (mean)#每组并发用户所用时间Time per request:       83.928 [ms] (mean, across all concurrent requests)   #服务器处理每个请求时间Transfer rate:          5.96 [Kbytes/sec] received#单位时间内响应数据总长度Connection Times (ms)#请求的阶梯耗时表类似Chrome控制台中的Network下内容              min  mean[+/-sd] median   maxConnect:        0   65 244.2      0    1349Processing:   347  584 557.4    361    3754Waiting:      347  584 557.4    361    3754Total:        347  649 608.0    366    3754Percentage of the requests served within a certain time (ms)#每个请求处理时间的分布情况表  50%    366#50%请求的处理时间不超过 336ms  66%    379#90%的请求处理时间不超过1.384s  75%    751  80%   1007  90%   1384  95%   2414  98%   2728  99%   3754 100%   3754 (longest request)

ab结合Python操作测试接口

利用Python操作shell:

import os,sysos.chdir("/bash_test")# 并发用户数 从外部导入user = int(sys.argv[2])# 总请求数   从外部导入sum_r = int(sys.argv[1])# 循环执行ab命令并发,使压测场景时间满足for i in range(5):    os.system("ab -n %d -c %d -p json.txt -k -T 'application/json' 'http://httpbin.org/post' >>report.html" %(sum_r,user))