基准测试工具:ab

原创
2016-07-23 08:54:45 765浏览
基准测试工具(ab)

ab是什么?
ab是基于命令行的工具,均可运行在windows、linux平台下。
为什么选用这个工具?
易于使用,输入少量命令选项,即可得出结果,同时易于安装。

安装ab

win:如果本地有apache的开发环境,那么ab默认在apache目录下的bin目录下,打开bin目录可以看到
linux:直接yum install httpd-tools,安装即可

简单上手

直接敲入下面代码(注意切换ab所在的目录)
-c 10代表并发数是10
-n 10总共进行100次访问
后面接要访问的网址,切记不可缺少http://

  1. D:\phpStudy\Apache\bin>ab -c 10 -n 1000 http://example.com/phpinfo.php
复制代码

待ab自己跑一下,得出下面结果

  1. #ab工具的描述,注意其中的版本号
  2. This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. #测试结果,共测试1000次,每100次显示访问进度,访问的网站是example.com
  6. Benchmarking example.com (be patient)
  7. Completed 100 requests
  8. Completed 200 requests
  9. Completed 300 requests
  10. Completed 400 requests
  11. Completed 500 requests
  12. Completed 600 requests
  13. Completed 700 requests
  14. Completed 800 requests
  15. Completed 900 requests
  16. Completed 1000 requests
  17. Finished 1000 requests
  18. #服务器的信息
  19. Server Software: nginx/1.6.2 #服务器web软件
  20. Server Hostname: example.com #主机地址
  21. Server Port: 80 #访问端口
  22. #文档信息
  23. Document Path: /phpinfo.php #访问的文档
  24. Document Length: 94804 bytes #返回结果大小字节数(包含html,js,css,图片及响应中的所有内容字节数总和)
  25. #链接信息
  26. Concurrency Level: 10 #并发数,-c 10 设置了10个并发
  27. Time taken for tests: 7.498 seconds #总耗时,单位秒
  28. Complete requests: 1000 #总共请求中已完成的请求总数的和
  29. Failed requests: 219 #失败的请求数总和
  30. (Connect: 0, Receive: 0, Length: 219, Exceptions: 0)
  31. Non-2xx responses: 9 #未收到2xx系列成功的请求总数
  32. Total transferred: 94135779 bytes #整个请求总数中响应总数距的总大小字节数,包含头信息
  33. HTML transferred: 93952076 bytes #整个请求总数中正文内容的总大小字节
  34. Requests per second: 133.36 [#/sec] (mean) #每秒支持的并发数,这里是每秒支持133.36个并发
  35. Time per request: 74.984 [ms] (mean) #完成一个请求的总耗时
  36. Time per request: 7.498 [ms] (mean, across all concurrent requests) #完成所有并发请求中一个请求的总耗时
  37. Transfer rate: 12259.83 [Kbytes/sec] received #每秒收到的字节总数(KB)
  38. #测试的时间结果数据,留意Total一项,10个并发的情况下,完成一个请求,最小耗时(min)9毫秒,最大耗时(max)146毫秒
  39. Connection Times (ms)
  40. min mean[+/-sd] median max
  41. Connect: 0 0 0.7 0 20
  42. Processing: 9 74 22.2 68 146
  43. Waiting: 6 73 22.1 67 143
  44. Total: 9 74 22.2 68 146
  45. #完成请求百分比,例如第一项,50% 68,代表一半的请求在68毫秒内完成,95% 129,代表1000的请求里,百分之九十八的请求在129毫秒里完成
  46. Percentage of the requests served within a certain time (ms)
  47. 50% 68
  48. 66% 78
  49. 75% 86
  50. 80% 92
  51. 90% 109
  52. 95% 122
  53. 98% 129
  54. 99% 133
  55. 100% 146 (longest request)
复制代码
ab选项

输入下面命令得到ab帮助文档,该帮助文档,分别说明了用法和命令选项的含义

  1. D:\phpStudy\Apache\bin>ab -h
复制代码

ab帮助文档的输出

  1. Usage: ab [options] [http://]hostname[:port]/path
  2. Options are:
  3. -n requests Number of requests to perform
  4. -c concurrency Number of multiple requests to make at a time
  5. -t timelimit Seconds to max. to spend on benchmarking
  6. This implies -n 50000
  7. -s timeout Seconds to max. wait for each response
  8. Default is 30 seconds
  9. -b windowsize Size of TCP send/receive buffer, in bytes
  10. -B address Address to bind to when making outgoing connections
  11. -p postfile File containing data to POST. Remember also to set -T
  12. -u putfile File containing data to PUT. Remember also to set -T
  13. -T content-type Content-type header to use for POST/PUT data, eg.
  14. 'application/x-www-form-urlencoded'
  15. Default is 'text/plain'
  16. -v verbosity How much troubleshooting info to print
  17. -w Print out results in HTML tables
  18. -i Use HEAD instead of GET
  19. -x attributes String to insert as table attributes
  20. -y attributes String to insert as tr attributes
  21. -z attributes String to insert as td or th attributes
  22. -C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
  23. -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
  24. Inserted after all normal header lines. (repeatable)
  25. -A attribute Add Basic WWW Authentication, the attributes
  26. are a colon separated username and password.
  27. -P attribute Add Basic Proxy Authentication, the attributes
  28. are a colon separated username and password.
  29. -X proxy:port Proxyserver and port number to use
  30. -V Print version number and exit
  31. -k Use HTTP KeepAlive feature
  32. -d Do not show percentiles served table.
  33. -S Do not show confidence estimators and warnings.
  34. -q Do not show progress when doing more than 150 requests
  35. -l Accept variable document length (use this for dynamic pages)
  36. -g filename Output collected data to gnuplot format file.
  37. -e filename Output CSV file with percentages served
  38. -r Don't exit on socket receive errors.
  39. -m method Method name
  40. -h Display usage information (this message)
复制代码

英文看不懂,附上中文解释,网上抄的,我没试过

  1. -n 即requests,用于指定压力测试总共的执行次数。
  2. -c 即concurrency,用于指定压力测试的并发数。
  3. -t 即timelimit,等待响应的最大时间(单位:秒)。
  4. -b 即windowsize,TCP发送/接收的缓冲大小(单位:字节)。
  5. -p 即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。
  6. -u 即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。
  7. -T 即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。
  8. -v 即verbosity,指定打印帮助信息的冗余级别。
  9. -w 以HTML表格形式打印结果。
  10. -i 使用HEAD请求代替GET请求。
  11. -x 插入字符串作为table标签的属性。
  12. -y 插入字符串作为tr标签的属性。
  13. -z 插入字符串作为td标签的属性。
  14. -C 添加cookie信息,例如:"Apache=1234"(可以重复该参数选项以添加多个)。
  15. -H 添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
  16. -A 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
  17. -P 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。
  18. -X 指定使用的代理服务器和端口号,例如:"126.10.10.3:88"。
  19. -V 打印版本号并退出。
  20. -k 使用HTTP的KeepAlive特性。
  21. -k 使用HTTP的KeepAlive特性。
  22. -d 不显示百分比。
  23. -S 不显示预估和警告信息。
  24. -g 输出结果信息到gnuplot格式的文件中。
  25. -e 输出结果信息到CSV格式的文件中。
  26. -r 指定接收到错误信息时不退出程序。
  27. -h 显示用法信息,其实就是ab -help。
复制代码

一般用-n,-c,-t就好了
-n请求总数
-c并发数
-t指定秒数内的并发数

-t的用法说一下,下面的意思是在20秒内并发数10个访问

  1. D:\phpStudy\Apache\bin> ab -c 10 -t 20 http://example.com/phpinfo.php
  2. This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking example.com (be patient)
  6. Finished 3011 requests
  7. Server Software: nginx/1.6.2
  8. Server Hostname: example.com
  9. Server Port: 80
  10. Document Path: /phpinfo.php
  11. Document Length: 94804 bytes
  12. Concurrency Level: 10
  13. Time taken for tests: 20.006 seconds
  14. Complete requests: 3011
  15. Failed requests: 495
  16. (Connect: 0, Receive: 0, Length: 495, Exceptions: 0)
  17. Non-2xx responses: 54
  18. Total transferred: 280896464 bytes
  19. HTML transferred: 280344222 bytes
  20. Requests per second: 150.50 [#/sec] (mean)
  21. Time per request: 66.444 [ms] (mean)
  22. Time per request: 6.644 [ms] (mean, across all concurrent requests)
  23. Transfer rate: 13711.44 [Kbytes/sec] received
  24. Connection Times (ms)
  25. min mean[+/-sd] median max
  26. Connect: 0 0 0.5 0 10
  27. Processing: 6 66 17.6 63 203
  28. Waiting: 5 64 17.3 61 202
  29. Total: 6 66 17.6 63 203
  30. Percentage of the requests served within a certain time (ms)
  31. 50% 63
  32. 66% 69
  33. 75% 72
  34. 80% 75
  35. 90% 82
  36. 95% 92
  37. 98% 113
  38. 99% 125
  39. 100% 203 (longest request)
复制代码

特别说明:如果访问http://www.example.com/,没有指定特定文件,记得要加最后的那个/,一定要加!!!否则可能会出现访问不到的情况!!!

可能会影响基准测试的情况

地理位置和网络问题

响应文件大小

代码的复杂度

浏览器解析

Web服务器的配置

地理位置和网络问题

如果Web服务器存放在境外,那么我们从本地访问境外的网站,可想而知,经过的路由器结点,服务器结点,再通过太平洋海底的光缆,而后再访问到Web服务器,Web服务器处理后,再继续海底光缆,各个服务器、路由结点的跳转返回,这样的话肯定会影响到测试结果(我的建议是,直接在服务器上进行ab测试)

响应文件大小

假如发送一个3MB的页面,服务器网卡会把3MB的数据拆分成单个很小的数据包,传输过程中,只有一个数据包损坏或丢失,则会重新发送所有的数据包,所以发送的包越小越好,同时,越小的数据传输,越快传输到用户的机器上。

代码的复杂度

代码的复杂度即是对业务逻辑复杂的处理,还有对文件调用、数据库访问、远端API接口的调用等等都会影响处理时间

浏览器解析

每个浏览器都有自己对js、css、html的处理方法,想想IE8以下和chrome的区别即可知道

Web服务器设置

一般Web服务器安装后,简单设置,即可实现Web服务(开箱即用),但是这样的设置没有发挥Web服务器的最大性能,需要资深的工程师对服务器进行相关配置,使之发挥最大性能,这里简单实用Keep-Alive说明。
Keep-Alive的作用就是,Web服务器打开特定数量的连接,让这些连接处于打开状态,使之能快速处理传入的请求,这样就不会为每一个传入的请求打开一个连接然后处理请求,减少服务器的处理请求的进程数,从而增加并发数。
在ab中使用-k进行测试,例如下面这句

  1. #-k,允许服务器保存5个并发连接处于打开并允许的状态,让其他请求快速处理,减少创建新请求的处理时间
  2. ab -c 5 -t 100 -k http://www.example.com/
复制代码
测试工具


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:iWebShop 二次开发,创建自己的控制器,模型,视图 下一条:iOS程序猿如何快速掌握 PHP,化身&quot;全栈攻城狮&quot;?

相关文章

查看更多