由于一些原因要进行一些程序代码的压力测试,
webbench 和 apache自带的一款插件ab
webbench可以测试get,head等请求,但不支持post请求,由于我正好是post请求测试,所以只好舍弃它了,改用了apache自带的插件ab(apache-benchmark)。这个插件支持post请求程序测试。
所以我们要使用这个插件,必须先要安装apache,然后就可以使用了
如果你不想安装apache,单独安装ab也可以,对于ubuntu,安装命令如下:
~$ sudo apt-get install apache2-utils
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
//等待response响应的最大秒时间
-b windowsize Size of TCP send/receive buffer, in bytes
-p postfile File containing data to POST. Remember also to set -T
//post请求时,post需要传输数据的文件路径和名称。同时别忘了设置-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'
//POST数据所使用的Content-type头信息。
-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 10 -c 10 http://www.google.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 www.google.com (be patient).....done
/*请求服务器名称*/
Server Software: gws
/*请求域名*/
Server Hostname: www.google.com
/*请求的端口号*/
Server Port: 80
Document Path: /
Document Length: 458 bytes
Concurrency Level: 10
/*整个测试持续的时间*/
Time taken for tests: 0.265 seconds
/*完成的请求数量*/
Complete requests: 10
/*失败的请求数量*/
Failed requests: 0
Write errors: 0
Non-2xx responses: 10
Total transferred: 12890 bytes
HTML transferred: 4580 bytes
//每秒事务数(mean)代表平均值
Requests per second: 37.71 [#/sec] (mean)
//平均事务响应时间(mean)表示是平均值
Time per request: 265.207 [ms] (mean)
//每个请求实际运行时间的平均值
Time per request: 26.521 [ms] (mean, across all concurrent requests)
Transfer rate: 47.46 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 83 93 7.3 95 104
Processing: 125 145 10.7 145 166
Waiting: 125 145 10.8 145 166
Total: 208 239 16.9 242 265
//50%的请求响应时间小于242毫秒,66%请求响应时间小于246毫秒,......
Percentage of the requests served within a certain time (ms)
50% 242
66% 246
75% 252
80% 255
90% 265
95% 265
98% 265
99% 265
100% 265 (longest request)