Linux命令-ab命令

php php 1607      收藏
ab压力测试

ab全名是(ApacheBench)命令是Apache httpd的Web服务器自带的压力性能测试工具,它可以测试安装Web服务器每秒种处理的HTTP请求。
ab对HTTP协议的支持较全面,有HEAD,GET,POST,PUT请求方式,以及自定义TCP收发缓冲的大小,自定义Request Header和cookie,支持KeepAlive特性,HTTPS协议和WWW-Authentication认证等。
AB只支持单个URL和并发请求/总请求数的测试模式。

centos安装ab  yum install httpd-tools

准备之后我们就可以测试了  ab -kc 1000 -n 1000 http://localhost/ab.html

ab命令选项

-A:指定连接服务器的基本的认证凭据; 
-c:指定一次向服务器发出请求数; 
-C:添加cookie; 
-g:将测试结果输出为“gnuolot”文件; 
-h:显示帮助信息; 
-H:为请求追加一个额外的头; 
-i:使用“head”请求方式; 
-k:激活HTTP中的“keepAlive”特性; 
-n:指定测试会话使用的请求数; 
-p:指定包含数据的文件; 
-q:不显示进度百分比; 
-T:使用POST数据时,设置内容类型头; 
-v:设置详细模式等级; 
-w:以HTML表格方式打印结果; 
-x:以表格方式输出时,设置表格的属性; 
-X:使用指定的代理服务器发送请求; 
-y:以表格方式输出时,设置表格属性。 
 
[root@Linux78 ]# ab -n 2000 -c 1000 www.XXX.cn/index.html
-c:并发数
-n:总请求数

This is ApacheBench, Version 2.3 <$revision: 655654="">
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.XXX.cn (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests
Server Software:        Apache   # web服务器请求软件及版本
Server Hostname:        www.XX.cn # 请求地址
Server Port:            80 # 请求端口
Document Path:          /index.html # 请求页面路径
Document Length:        234 bytes # 而面大小
Concurrency Level:      1000 # 并发数
Time taken for tests:   38.900 seconds # 测试总共花费时间
Complete requests:      2000 # 完成请求数
Failed requests:        0 # 失败请求数
Write errors:           0 # 写入错误数
Non-2xx responses:      2017
Total transferred:      895548 bytes # 总共传输字节数,包含httpd的头信息
HTML transferred:       471978 bytes # html字节数,实际而面传递字节数
Requests per second:    51.41 [#/sec] (mean) # 每秒处理的请求数,服务器的吞吐量
Time per request:       19449.847 [ms] (mean) # 平均数,用户平均请求等待时间
Time per request:       19.450 [ms] (mean, across all concurrent requests) # 服务器平均处理时间
Transfer rate:          22.48 [Kbytes/sec] received # 平均传输速率(每秒收到的速度)
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       41 1235 2468.2     71   15043
Processing:    42 8057 8915.3   4085   38294
Waiting:       42 6848 8345.8   1829   37223
Total:        154 9292 9399.9   5947   38595
Percentage of the requests served within a certain time (ms)
  50%   5947
  66%  11514
  75%  16775
  80%  17320
  90%  21496
  95%  31725
  98%  34918
  99%  36105
 100%  38595 (longest request)

ab测试过程中错误解决方法

1)ab并发数不能大于请求数,会提示
            ab: Cannot use concurrency level greater than total number of requests
        2)请求数默认不能超过1024个,会提示
            socket: Too many open files (24)
            可用ulimit -n命令修改,例如:ulimit -n 8192 (设置用户可以同时打开的最大文件数)。
        3)并发数默认不能大于20000个,会提示
            ab: Invalid Concurrency [Range 0..20000]
            需要修改apache源代码support目录下ab.c文件,找到:
        #define MAX_CONCURRENCY 20000
        将宏定义的值改大,重新编译安装apache。

自学php博客