美文网首页
Apache ab 压力测试

Apache ab 压力测试

作者: 穿越人海zx | 来源:发表于2019-01-11 10:36 被阅读0次
    ab命令原理

    Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx、lighthttp、IIS等其它Web服务器的压力。
    ab命令对发出负载的计算机要求很低,既不会占用很多CPU,也不会占用太多的内存,但却会给目标服务器造成巨大的负载,使用时设置参数需要谨慎,由小到大,一步一个脚印往上递增。否则一次目标服务器承受过多负载,会造成目标服务器直接因内存耗光而宕机。
    在带宽不足的情况下,最好是本机进行测试,建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)

    官方文档:
    http://httpd.apache.org/docs/current/programs/ab.html

    Linux 安装 ab 命令

    yum -y install httpd-tools
    

    查看ab版本

    ab -v
    

    常用参数详解

    -n:总请求次数(最小默认为1)
    
    -c:并发次数(模拟多少客户端,最小默认为1且不能大于总请求次数,如:10个请求,10个并发,实际就是1人请求1次)
    
    -p:post参数文档路径(-p和-T参数要配合使用)
    
    -T:header头内容类型(此处切记是大写英文字母T)
    
    后面直接写请求路径即可:http://www.test.com/xxx/xxx.html
    

    无参

    ab -n 100 -c 50 'http://10.43.xx.xx:8080/TV/api/getPayQrcode.do'
    
    (-n发出100个请求,-c模拟50并发,相当50人同时访问,-T 指定)
    
    其中-n代表请求数,-c代表并发数
    

    模拟get请求

    ab -n 1000 -c 50 'http://10.43.xx.xx:8080/TV/api/getPayQrcode.do?rechargeOrderId=365'
    
     请求次数1000 并发 50 
    
    This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
    
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, [http://www.zeustech.net/](http://www.zeustech.net/)
    
    Licensed to The Apache Software Foundation, [http://www.apache.org/](http://www.apache.org/)
    
    Benchmarking 10.43.124.145 (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:        10.43.xx.xx
    
    Server Port:            8080
    
    Document Path:          /TV/getPayQrcode.do?rechargeOrderId=365&channel=WX
    
    Document Length:        7080 bytes
    
    Concurrency Level:      50   并发数
    
    Time taken for tests:   160.645 seconds   一共使用了160.645s  
    
    Complete requests:      1000  请求的次数
    
    Failed requests:        998  失败的请求    
    
       (Connect: 0, Receive: 0, Length: 998, Exceptions: 0)
    
    Write errors:           0
    
    Total transferred:      6945667 bytes
    
    HTML transferred:       6872667 bytes
    
    Requests per second:    6.22 [#/sec] (mean)  吞吐率
    
    Time per request:       8032.249 [ms] (mean) 用户平均请求等待时间
    
    Time per request:       160.645 [ms] (mean, across all concurrent requests) 服务器平均请求处理时间
    
    Transfer rate:          42.22 [Kbytes/sec] received
    
    Connection Times (ms)
    
                  min  mean[+/-sd] median   max
    
    Connect:        0    1   0.7      0       5
    
    Processing:   571 7773 2815.0   8189   15428
    
    Waiting:      566 7772 2815.0   8189   15428
    
    Total:        571 7773 2814.7   8189   15428
    
    WARNING: The median and mean for the initial connection time are not within a normal deviation
    
            These results are probably not that reliable.
    
    Percentage of the requests served within a certain time (ms)
    
      50%   8189
    
      66%   8863
    
      75%   9373
    
      80%   9816
    
      90%  11219
    
      95%  11914
    
      98%  12621
    
      99%  14548
    
    100%  15428 (longest request)
    
    

    测试结果

    主要看三个数据

    Requests per second:吞吐率

    服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
    计算公式:总请求数 / 处理完成这些请求数所花费的时间,即
    Request per second = Complete requests / Time taken for tests

    Time per request:上面的是用户平均请求等待时间

    处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即
    Time per request = Time taken for tests /( Complete requests / Concurrency Level)

    Time per request:下面的是服务器平均请求处理时间

    处理完成所有请求数所花费的时间 / 总请求数,即
    Time taken for / testsComplete requests
    可以看到,它是吞吐率的倒数。
    同时,它也=用户平均请求等待时间/并发用户数,即
    Time per request / Concurrency Level

    可以根据固定并发数,增加请求次数或者固定请求次数,调整并发数来查看上面三个值得变化来调优服务器

    模拟post请求

    方式1

    新建一个文件crate_order.txt,里面放POST参数(承载POST参数的文件,不依赖于后缀名。可跨平台)
    POST参数文本内容如下rechargeOrderId=365&city=SGS&channel=WX

    ab -n 500 -c 500 -p create_order.txt -T application/x-www-form-urlencoded 'http://10.43.xx.xx:0000/api/create_order'
    

    [注意] -p是参数文件路径,-T是大写英文字母,post表单格式为:application/x-www-form-urlencoded

    方式2:json格式文件[没测成功]
    ab -n 50 -c 50 -p create_order.json -T 'application/json'   'http://10.43.xx.xx:0000/api/create_order'
    

    ab压测参数化,可以同时执行多条[未测试]

    参考:https://blog.csdn.net/qq_23668615/article/details/50801497

    相关文章

      网友评论

          本文标题:Apache ab 压力测试

          本文链接:https://www.haomeiwen.com/subject/inrarqtx.html