ApacheBench简介
ApacheBench 是一个用来衡量http服务器性能的单线程命令行工具。原本针对Apache http服务器,但是也适用于其他http服务器。
ab工具与标准 Apache源码一起发布,免费,开源,基于Apache License。
ApacheBench安装
ubuntu 执行“apt-get install apache2-utils“即可。
ApacheBench快速入门
发送1000个http get请求到[http://192.168.10.232:8000/blog,并发为10]:
$ ab -n1000 -c10 http://localhost/This is ApacheBench, Version 2.3 <$Revision: 1706008 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking localhost (be patient)Completed 100 requestsCompleted 200 requestsCompleted 300 requestsCompleted 400 requestsCompleted 500 requestsCompleted 600 requestsCompleted 700 requestsCompleted 800 requestsCompleted 900 requestsCompleted 1000 requestsFinished 1000 requestsServer Software: nginx/1.10.3Server Hostname: localhostServer Port: 80Document Path: /Document Length: 11321 bytesConcurrency Level: 10Time taken for tests: 0.066 secondsComplete requests: 1000Failed requests: 0Total transferred: 11566000 bytesHTML transferred: 11321000 bytesRequests per second: 15207.74 [#/sec] (mean)Time per request: 0.658 [ms] (mean)Time per request: 0.066 [ms] (mean, across all concurrent requests)Transfer rate: 171770.21 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 0.1 0 0Processing: 0 0 0.1 0 1Waiting: 0 0 0.2 0 1Total: 0 1 0.1 1 1Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 1 90% 1 95% 1 98% 1 99% 1 100% 1 (longest request)
注意Time per request: 0.658 [ms] (mean)是平均每批请求的完成时间,这里时10个请求完成的时间。Time per request: 0.066 [ms] (mean, across all concurrent requests)是平均每个请求的完成时间。
ApacheBench将只使用一个操作系统线程。很多情况下ApacheBench本身是瓶颈。当在硬件具有多个处理器核心使用ApacheBench,建议一个cpu核执行一个ab线程,但是现在cpu核数很多的情况下,比如48核,手工执行48个命令也是个体力活。
整体来说ab因为不能利用到多核、不能进行参数化、不支持http和https以外的协议等原因,只适合单个组件的粗略测试,比如Apache http。不合适业务级别的测试,如一旦后台有数据库等,ab的测试是几乎没有意义的。
ApacheBench参考
- 命令行帮助
$ 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 Default is 30 seconds -b windowsize Size of TCP send/receive buffer, in bytes -B address Address to bind to when making outgoing connections -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 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 -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. -q Do not show progress when doing more than 150 requests -l Accept variable document length (use this for dynamic pages) -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 (SSL3, TLS1, TLS1.1, TLS1.2 or ALL
- 语法:
ab [ -A auth-username:password ] [ -b windowsize ] [ -c concurrency ] [ -C cookie-name=value ] [ -d ] [ -e csv-file ] [ -f protocol ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ -n requests ] [ -p POST-file ] [ -P proxy-auth-username:password ] [ -q ] [ -r ] [ -s ] [ -S ] [ -t timelimit ] [ -T content-type ] [ -u PUT-file ] [ -v verbosity] [ -V ] [ -w ] [ -x <table>-attributes ] [ -X proxy[:port] ] [ -y <tr>-attributes ] [ -z <td>-attributes ] [ -Z ciphersuite ] [http[s]://]hostname[:port]/path
- 命令行选项
-A auth-username:password 向服务器提供基本认证信息。用户名和密码之间":"分割,以base64编码形式发送。无论服务器是否需要(即是否发送了401)都发送。 -b windowsize TCP发送/接收缓冲区大小,以字节为单位。-c concurrency 并发数,默认为1。-C cookie-name=value 添加Cookie。典型形式是name=value对。name参数可以重复。 -d不显示"percentage served within XX [ms] table"消息(兼容以前的版本)。 -e csv-file 输出百分率和对应的时间,格式为逗号份额的csv。由于这种格式已经"二进制化",所以比"gnuplot"格式更有用。-f protocol SSL/TLS protocol (SSL2, SSL3, TLS1, 或ALL).-g gnuplot-file 把所有测试结果写入"gnuplot"或者TSV(以Tab分隔)文件。该文件可以方便地导入到Gnuplot, IDL, Mathematica甚至Excel中,第一行为标题。-h 显示使用方法。-H custom-header 附加额外头信息。典型形式有效的头信息行,包含冒号分隔的字段和值(如:"Accept-Encoding: zip/zop;8bit")。-i 执行HEAD请求,而不是GET 。-k 启用KeepAlive功能,即在HTTP会话中执行多个请求。默认关闭。-n requests 会话执行的请求数。默认为1。 -p POST-file 附加包含POST数据的文件。注意和-T一起使用。-P proxy-auth-username:password 代理认证。用户名和密码之间":"分割,以base64编码形式发送。无论服务器是否需要(即是否发送了407)都发送。-q quiet,静默模式。不在stderr输出进度条。-r 套接字接收错误时不退出。-s timeout 超时,默认为30秒。-S 不显示中值和标准偏差值,而且在均值和中值为标准偏差值的1到2倍时,也不显示警告或出错信息。默认显示最小值/均值/最大值。(兼容以前的版本)-t timelimit 测试进行的最大秒数。内部隐含值是"-n 50000"。默认没有时间限制。-T content-type POST/PUT的"Content-type"头信息。比如“application/x-www-form-urlencoded”,默认“text/plain”。-v verbosity 详细模式,4以上会显示头信息,3以上显示响应代码(404,200等),2以上显示告警和info。-V 显示版本号并退出。-w 以HTML表格形式输出。默认是白色背景的两列。-x <table>-attributes 设置<table>属性。此属性填入<table 这里 > 。-X proxy[:port] 使用代理服务器。-y <tr>-attributes 设置<tr>属性。-z <td>-attributes 设置<td>属性。 -Z ciphersuite 设置SSL/TLS加密
- 结果分析字段
Server Software 返回的第一次成功的服务器响应的HTTP头。Server Hostname 命令行中给出的域名或IP地址Server Port 命令行中给出端口。如果没有80(HTTP)和443(HTTPS)。SSL/TLS Protocol 使用SSL打印。Document Path 命令行请求的路径。Document Length 第一次成功地返回文档的字节大小。后面接受的文档长度变化时,会认为是错误。Concurrency Level 并发数Time taken for tests 测试耗时Complete requests 收到成功响应数Failed requests 失败请求数。如果有会打印错误原因Write errors 写错误数 (broken pipe)Non-2xx responses 非2**响应数量。如果有打印。Keep-Alive requests Keep-Alive请求的连接数Total body sent: 传输的body的数据量,比如POST的数据。Total transferred: 总传输数据量HTML transferred: 累计html传输数据量Time per request: 每批平均请求时间Time per request: 每次平均请求时间。计算公式:Time per request/Concurrency Level。Transfer rate 数据传输速率。计算公式:otalread / 1024 / timetaken。
参考资料
- 讨论qq群144081101 591302926 567351477 钉钉免费群21745728
- 本文涉及的python测试开发库 请在github上点赞,谢谢!
- 本文相关书籍下载
- ab