代码改变世界

Apache ab压力测试

2018-07-20 14:54  软件测试汪  阅读(7420)  评论(0编辑  收藏  举报

什么是Apache ab

ab是用于对Apache超文本传输协议(HTTP)服务器进行基准测试的工具。这么解释有点官方,下面会用几个实例进行解释。

 

环境搭建

APache ab 可以安装在Linux下面,也可以安装在Windows下面。但是安装在Windows下面有点麻烦,而且自己比较喜欢Linux,所以下面只介绍在Linux环境下面搭建Apache ab

 

Linux环境搭建

sudo apt-get install apache2-utils //Ubuntu举例 

 

验证是否安装成功

 

 

参数介绍

 

root@ubuntu:~# 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 //超时,默认每个response 30s
                    Default is 30 seconds
    -b windowsize   Size of TCP send/receive buffer, in bytes //TCP发送/接收缓冲区的大小,以字节为单位
    -B address      Address to bind to when making outgoing connections   //Address to bind to when making outgoing connections
    -p postfile     File containing data to POST. Remember also to set -T  //包含POST数据的文件。 
    -u putfile      File containing data to PUT. Remember also to set -T   //包含PUT的文件
    -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          //在HTML表格中打印结果
    -i              Use HEAD instead of GET                   //使用HEAD而不是GET
    -x attributes   String to insert as table attributes      //作为表属性插入的字符串
    -y attributes   String to insert as tr attributes         //作为tr属性插入的字符串
    -z attributes   String to insert as td or th attributes   //作为td或th属性插入的字符串
    -C attribute    Add cookie, eg. 'Apache=1234'. (repeatable) //添加cookie
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip' //添加header
                    Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes //添加基本WWW身份验证
                    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 // 要使用的Proxyserver和端口号
    -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  //执行超过150个请求时不要显示进度
    -l              Accept variable document length (use this for dynamic pages) //接受可变文档长度(用于动态页面)
    -g filename     Output collected data to gnuplot format file. //将收集的数据输出到gnuplot格式文件
    -e filename     Output CSV file with percentages served //输出已提供百分比的CSV文件
    -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
                    (TLS1, TLS1.1, TLS1.2 or ALL)

 

实例

上面有部分参数我没有进行翻译~因为我暂时想不到使用场景。下面我会介绍Apache ab常用的几种使用技巧

 

设置并发数,设置总请求数,请求方式为get请求

ab -c10 -n100 http://www.baidu.com/     // 10个用户 一共发送100次请求,访问百度首页

 

设置设置并发数,设置总请求数,请求方式为post请求

post请求呢,我是用本地django项目调试的,试了一小时,发现真的不好用。网上找的资料也都模棱两可,技术所限,后面再找找看。不过真心觉得不要用ab做post请求~jmeter特别好用

 

在HTML表格中打印结果

 

ab -c10 -n100 http://www.baidu.com/>report.html     

 

测试结果分析

 

 

主要看以下数据:

Requests per second:吞吐率

Time taken for tests    测试总耗时

Failed requests    请求失败数目

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

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

 

总结

虽然找了很多资料,去写这个博客,但是自己慢慢用ab的时候,发现真的不好用,locust、siege、jmeter等都比这玩意好用,反正,仁者见仁,智者见智吧。想写这个也只是兴趣