【Linux】ApacheBench(ab)压力测试工具
AB的简介
ab是apachebench命令的缩写。
ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等
ab的原理
ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。
ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机
ab的安装
安装命令:
yum -y install httpd-tools
查看版本命令:
ab -V
查看参数说明:命令行帮助
ab --help
$ 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
-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加密
ab的使用
语法:
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
常用方式:
ab -c 500 -n 5000 http://localhost/
[root@H__D /]# ab -c 500 -n 5000 http://localhost/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> 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 500 requests Completed 1000 requests Completed 1500 requests Completed 2000 requests Completed 2500 requests Completed 3000 requests Completed 3500 requests Completed 4000 requests Completed 4500 requests Completed 5000 requests Finished 5000 requests Server Software: nginx/1.15.3 #测试服务器的名字 Server Hostname: localhost #请求的URL主机名 Server Port: 80 #请求端口 Document Path: / #请求路径 Document Length: 613 bytes #HTTP响应数据的正文长度 Concurrency Level: 500 #并发用户数,这是我们设置的参数之一 Time taken for tests: 0.407 seconds #所有这些请求被处理完成所花费的总时间 单位秒 Complete requests: 5000 #总请求数量,这是我们设置的参数之一 Failed requests: 0 #表示失败的请求数量 Write errors: 0 Total transferred: 4230000 bytes #所有请求的响应数据长度总和。包括每个HTTP响应数据的头信息和正文数据的长度 HTML transferred: 3065000 bytes #所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度 Requests per second: 12284.44 [#/sec] (mean) #吞吐量,计算公式:Complete requests/Time taken for tests 总请求数/处理完成这些请求数所花费的时间 Time per request: 40.702 [ms] (mean) #用户平均请求等待时间,计算公式:Time token for tests/(Complete requests/Concurrency Level)。处理完成所有请求数所花费的时间/(总请求数/并发用户数) Time per request: 0.081 [ms] (mean, across all concurrent requests) #服务器平均请求等待时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。也可以这么统计:Time per request/Concurrency Level Transfer rate: 10149.06 [Kbytes/sec] received。 #表示这些请求在单位时间内从服务器获取的数据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量。 Connection Times (ms) min mean[+/-sd] median max Connect: 0 2 0.8 1 6 Processing: 3 5 7.9 5 399 Waiting: 0 4 7.9 4 399 Total: 4 7 8.0 7 404 WARNING: The median and mean for the initial connection time are not within a normal deviation These results are probably not that reliable. Percentage of the requests served within a certain time (ms) 50% 7 #50%用户请求在7ms内返回 66% 7 #60%用户请求在7ms内返回 75% 7 80% 7 90% 7 95% 7 98% 12 #98%用户请求在12ms内返回 99% 12 100% 404 (longest request)
使用注意
1、MAC中应该是自带了Apache。
2、在使用ab命令时,并发了过高会出现错误:Too many open files,由于系统打开文件数量限制了。
查看系统打开文件数量,命令:
ulimit -a
修改打开文件数量,修改成1024,命令:
ulimit -n 1024
查看修改后情况,命令:
ulimit -n