Tuesday, 18 August 2015

Mac上用Siege做Web服务器压测

老衲电灯

安装

在mac上,可以使用brew来直接安装。 $ brew install siege

$ siege –help

SIEGE 3.1.3
Usage: siege [options]
       siege [options] URL
       siege -g URL
Options:
  -V, --version             VERSION, prints the version number.
  -h, --help                HELP, prints this section.
  -C, --config              CONFIGURATION, show the current config.
  -v, --verbose             VERBOSE, prints notification to screen.
  -q, --quiet               QUIET turns verbose off and suppresses output.
  -g, --get                 GET, pull down HTTP headers and display the
                            transaction. Great for application debugging.
  -c, --concurrent=NUM      CONCURRENT users, default is 10
  -i, --internet            INTERNET user simulation, hits URLs randomly.
  -b, --benchmark           BENCHMARK: no delays between requests.
  -t, --time=NUMm           TIMED testing where "m" is modifier S, M, or H
                            ex: --time=1H, one hour test.
  -r, --reps=NUM            REPS, number of times to run the test.
  -f, --file=FILE           FILE, select a specific URLS FILE.
  -R, --rc=FILE             RC, specify an siegerc file
  -l, --log[=FILE]          LOG to FILE. If FILE is not specified, the
                            default is used: PREFIX/var/siege.log
  -m, --mark="text"         MARK, mark the log file with a string.
  -d, --delay=NUM           Time DELAY, random delay before each requst
                            between .001 and NUM. (NOT COUNTED IN STATS)
  -H, --header="text"       Add a header to request (can be many)
  -A, --user-agent="text"   Sets User-Agent in request
  -T, --content-type="text" Sets Content-Type in request

Copyright (C) 2015 by Jeffrey Fulmer, et al.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.

使用方法

比如,对某个url进行简单的http访问压测(表示启动100个用户在10秒内并发访问url网址):

$ siege -c100 -t10s -b http://www.bai.com 

并发测试完毕后,打印出结果信息:

Transactions:                318 hits
Availability:              99.69 %
Elapsed time:               9.84 secs
Data transferred:          10.12 MB
Response time:              2.22 secs
Transaction rate:          32.32 trans/sec
Throughput:             1.03 MB/sec
Concurrency:               71.77
Successful transactions:         415
Failed transactions:               1
Longest transaction:            9.54
Shortest transaction:           0.07

或者启动200个用户在10秒内并发访问url网址:

$ siege -c200 -t10s -b http://www.bai.com 

并发测试完毕后,打印出结果信息:

Transactions:                326 hits
Availability:              99.39 %
Elapsed time:               9.86 secs
Data transferred:          10.39 MB
Response time:              2.86 secs
Transaction rate:          33.06 trans/sec
Throughput:             1.05 MB/sec
Concurrency:               94.65
Successful transactions:         525
Failed transactions:               2
Longest transaction:            9.68
Shortest transaction:           0.06

如果在并发的过程中,被访问的页面打开出错或及其缓慢,表示在当前并发条件下,被访问网站是不能承受的,也就是抗并发能力弱。另外,在并发的过程中最好通过top命令来查看CPU和Memory的占用情况。

使用方法2: 对多个页面进行并发访问

新建一个文件,命名为jdUrls,里面的内容为(只是例子,任何url都可以):

http://www.jd.com
http://www.jd.com/2017/12/16/12211/
http://www.jd.com/2017/12/14/12026/

执行命令:

$ siege -f jdUrls -c200 -t5

表示启动200个用户在5分钟内并发访问以上的url网址。

使用方法3

在使用http post方式时,结合http body类型。

siege -T 'application/json' -c 500 -r 1 'http://some.url./post-service/ POST <./postfile' 

服务器端接收到body为postfile文件内容,但是content_type依旧是默认值"application/x-www-form-urlencoded",致使服务器端不能正确处理。

将postfile文件名修改为postfile.json

siege -c 500 -r 1 'http://some.url./post-service/ POST <./postfile.json'

content_type被正确设置为application/json,服务器正常处理返回。 原来siege只能通过文件的扩展名来确定content type格式