1. 程式人生 > >Life is short, not float

Life is short, not float

最近有一個使用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('東方紅太陽升')

然後就可以進行測試了

[[email protected] 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)