【压测】压力测试ab的用法【原创】.md

简介

ab(Apache Bench)是Apache附带的压力测试软件,容易使用,功能完全能够满足我们的要求,安装apache时会自带该压力测试软件

参数说明

ab自带的参数说明:

[root@xiaozhu images]# 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
    -t timelimit Seconds to max. wait for responses
    -b windowsize Size of TCP send/receive buffer, in bytes
    -p postfile File containing data to POST. Remember also to set -T
    -u putfile File containing data to PUT. Remember also to set -T
    -T content-type Content-type header for POSTing, 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
    -i Use HEAD instead of GET
    -x attributes String to insert as table attributes
    -y attributes String to insert as tr attributes
    -z attributes String to insert as td or th attributes
    -C attribute Add cookie, eg. 'Apache=1234. (repeatable)
    -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                    Inserted after all normal header lines. (repeatable)
    -A attribute Add Basic WWW Authentication, the attributes
                    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
    -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.
    -g filename Output collected data to gnuplot format file.
    -e filename Output CSV file with percentages served
    -r Don't exit on socket receive errors.
    -h Display usage information (this message)
    -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)

ab的参数比较多,常用的就几个:

  • -n :在测试会话中执行的请求个数,即请求数,默认执行一个请求
  • -c :要创建的并发用户数,即并发数,默认创建一个用户数
  • -t :等待Web服务器响应的最大时间(单位:秒),默认没有时间限制
  • -k :使用Keep-Alive(持久连接)特性
  • -C :对请求附加一个Cookie,形式为 name=value

用法

下面使用ab进行一次压力测试:

[root@xiaozhu images]# ab -c5 -n100 -k xxx.com/
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 cms.dev.paidui.com (be patient).....done

Server Software: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80

Document Path: /
Document Length: 30692 bytes

Concurrency Level: 5
Time taken for tests: 20.663 seconds
Complete requests: 100
Failed requests: 10
   (Connect: 0, Receive: 0, Length: 10, Exceptions: 0)
Write errors: 0
Non-2xx responses: 10
Keep-Alive requests: 10
Total transferred: 2881762 bytes
HTML transferred: 2822279 bytes
Requests per second: 4.84 [#/sec] (mean)
Time per request: 1033.169 [ms] (mean)
Time per request: 206.634 [ms] (mean, across all concurrent requests)
Transfer rate: 136.19 [Kbytes/sec] received

Connection Times (ms)
              min mean[+/-sd] median max
Connect: 0 5 2.1 6 11
Processing: 9 984 1007.0 739 7508
Waiting: 9 330 230.7 257 1278
Total: 9 989 1007.2 744 7513

Percentage of the requests served within a certain time (ms)
  50% 744
  66% 919
  75% 1130
  80% 1222
  90% 1772
  95% 2422
  98% 4720
  99% 7513
 100% 7513 (longest request)

测试结果一目了然,吞吐率为4.84 reqs/s(吞吐率即单位时间内服务器处理的请求数)

测试结果中关注以下内容:

  • Server Software: 被测试Web服务器的软件名称
  • Server Hostname: 请求URL中的主机名称
  • Server Port: 被测试Web服务器的侦听端口
  • Document Path: 请求的URL的绝对路径
  • Document Length: HTTP响应数据的正文长度
  • Concurrency Level: 并发用户数,设置的“-c”参数
  • Time taken for tests: 所有请求处理完成所花费的总时间
  • Complete requests: 总请求数,设置的“-n”参数
  • Failed requests: 失败的请求数
  • Total transferred: 所有请求的响应数据长度总和
  • HTML transferred: 所有请求的响应数据中正文数据的总和,也就是减去Total transferred中HTTP响应数据中头信息的长度
  • Requests per second: Web服务器的吞吐率,等于 Complete requests/Time taken for tests
  • Time per request: 用户平均请求等待时间,等于 Time taken for tests/(Complete requests/Concurrency Level)
  • Transfer rate: 这些请求在单位时间内从服务器取得的数据长度,等于 Total transferred/Time taken for tests

持久连接

持久连接(Keep-Alive)又称长连接,指TCP连接中连续发送多份数据而不断开的连接,与之对应的称为短连接,就是连接后发送一次数据便断开的连接。长连接对于密集型图片或者网页等小数据请求处理有明显的加速作用。

在请求大量小文件时,长连接能够有效地减少重新建立连接的开销。在启动ab时加上-k参数可以使用Keep-Alive模式进行测试

比如:

先测试不使用长连接请求一个图片:

[root@xiaozhu images]# ab -c10 -n1000 cms.dev.paidui.com/images/3gphost.jpg
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 cms.dev.paidui.com (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: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80

Document Path: /images/3gphost.jpg
Document Length: 169 bytes

Concurrency Level: 10
Time taken for tests: 5.166 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 659912 bytes
HTML transferred: 169000 bytes
Requests per second: 193.59 [#/sec] (mean)
Time per request: 51.657 [ms] (mean)
Time per request: 5.166 [ms] (mean, across all concurrent requests)
Transfer rate: 124.76 [Kbytes/sec] received

Connection Times (ms)
        min mean[+/-sd] median max
Connect: 4 5 1.2 5 17
Processing: 14 46 26.9 39 219
Waiting: 14 46 26.9 39 219
Total: 19 51 26.9 44 224

Percentage of the requests served within a certain time (ms)
  50% 44
  66% 49
  75% 52
  80% 55
  90% 64
  95% 113
  98% 174
  99% 193
 100% 224 (longest request)

不使用长连接的时候吞吐率为193.59

下面使用长连接:

[root@xiaozhu images]# ab -c10 -n1000 -k cms.dev.paidui.com/images/3gphost.jpg
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 cms.dev.paidui.com (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: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80

Document Path: /images/3gphost.jpg
Document Length: 169 bytes

Concurrency Level: 10
Time taken for tests: 3.086 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Keep-Alive requests: 992
Total transferred: 648975 bytes
HTML transferred: 169000 bytes
Requests per second: 324.08 [#/sec] (mean)
Time per request: 30.856 [ms] (mean)
Time per request: 3.086 [ms] (mean, across all concurrent requests)
Transfer rate: 205.39 [Kbytes/sec] received

Connection Times (ms)
       min mean[+/-sd] median max
Connect: 0 0 0.8 0 7
Processing: 12 31 21.7 27 232
Waiting: 12 31 21.7 27 232
Total: 12 31 22.2 27 238

Percentage of the requests served within a certain time (ms)
  50% 27
  66% 31
  75% 34
  80% 35
  90% 40
  95% 46
  98% 53
  99% 220
 100% 238 (longest request)

使用长连接的吞吐率为324,除此之外,Keep-Alive request为992,即使用长连接的有992个请求,也就是说有992个请求重用同一个TCP连接。

参考资料

《PHP核心技术与最佳实践》的第11章 高性能网站架构方案

发布了64 篇原创文章 · 获赞 47 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/jiandanokok/article/details/79646090