最近有一个使用ab测试服务器并发量的需求,顺便鼓捣了一下ab的入门操作。发现在网上的内容有些千篇一律。在此记录下我测试的过程。
我使用的ab测试命令如下
ab -n 100 -c 10 -T 'application/x-www-form-urlencoded' -p postfile http://172.25.24.221:8880/project
此服务使用Tornado开发,ip为172.25.24.221,端口8880, 参数有两个:用户名user_id和查询语句query。
ab的参数有以下几个:
-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
-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)
在此主要用到的是-n
、-c
、-T
、-p
四个参数,分别表示总请求数、并发数、Content-type、请求参数文件。
请求参数文件格式为:
user_id=haonima&query=%e4%b8%9c%e6%96%b9%e7%ba%a2%e5%a4%aa%e9%98%b3%e5%8d%87
中文需要做url编码。此步可以使用python转换
from urllib import quote
print quote('东方红太阳升')
更简单一点的方法可以直接去http://tool.chinaz.com/tools/urlencode.aspx转换。
然后就可以进行测试了
[haonima@server02 ab_test]$ ab -n 1000 -c 50 -T 'application/x-www-form-urlencoded' -p postfile http://172.25.24.221:8880/project
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 172.25.24.221 (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: TornadoServer/4.4.1
Server Hostname: 172.25.24.221
Server Port: 8880
Document Path: /project
Document Length: 292 bytes
Concurrency Level: 50
Time taken for tests: 370.201 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 512000 bytes
Total body sent: 387000
HTML transferred: 292000 bytes
Requests per second: 2.70 [#/sec] (mean)
Time per request: 18510.060 [ms] (mean)
Time per request: 370.201 [ms] (mean, across all concurrent requests)
Transfer rate: 1.35 [Kbytes/sec] received
1.02 kb/s sent
2.37 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 1
Processing: 378 18470 1042.0 18284 20271
Waiting: 377 18470 1042.0 18284 20271
Total: 379 18470 1041.9 18284 20271
Percentage of the requests served within a certain time (ms)
50% 18284
66% 18712
75% 18994
80% 19116
90% 19652
95% 20135
98% 20257
99% 20267
100% 20271 (longest request)