AnsibleでWEBサーバの負荷テスト

Ansibleといえば構成管理ツールの一種という位置付けですが、複数のサーバを同時に制御できるという特徴から、サーバの負荷テストにも使うことができます。

#負荷テスト用のツール

WEBサーバのベンチマークツールとして、[Apach Benchmark](http://httpd.apache.org/docs/2.2/en/programs/ab.html) か [weighttp](http://www.iij.ad.jp/company/development/tech/activities/weighttp/) を用意します。Apach Benchmarkを使う場合はhttpd-toolsパッケージをインストールします。weightpは自分で[ビルドする](http://qiita.com/tanuki-project/items/3d50acb6871d29123224)必要があります。
これらのツールをAnsibleで複数のノードから同時かつ一斉に実行することで、WEBサーバの負荷テストを行うことができます。

#Ansibleの設定

イベントリファイルにベンチマークツールを実行するホストを記述します。

    [clients]
    host1
    host1
    host3
    ・
    ・
    host9
    host10

また、並列処理数の設定 /etc/ansible/ansible.cfg でforks(平行して生成するプロセス数)にクライアント数以上の値を設定します。

    [defaults]
    inventory               = /etc/ansible/hosts
    forks                   = 10


#負荷テストの実行

ansibleコマンドでベンチマークツールを一斉に実行します。

【Apach Benchmarkの場合】

    $ ansible clients -k -a "ab -c 100 -n 1000 http://192.168.10.1/"
    SSH password: 
    host1 | success | rc=0 >>
    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 192.168.10.1 (be patient)
    
    
    Server Software:        Boa/0.94.14rc21
    Server Hostname:        192.168.10.1
    Server Port:            80
    
    Document Path:          /
    Document Length:        702 bytes
    
    Concurrency Level:      100
    Time taken for tests:   8.167 seconds
    Complete requests:      1000
    Failed requests:        0
    Write errors:           0
    Non-2xx responses:      1000
    Total transferred:      921000 bytes
    HTML transferred:       702000 bytes
    Requests per second:    122.44 [#/sec] (mean)
    Time per request:       816.748 [ms] (mean)
    Time per request:       8.167 [ms] (mean, across all concurrent requests)
    Transfer rate:          110.12 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        2  215 1035.5      5    7032
    Processing:     4  120 233.1     30    2060
    Waiting:        4  119 233.1     30    2060
    Total:          7  334 1087.9     39    7299
    
    Percentage of the requests served within a certain time (ms)
      50%     39
      66%    142
      75%    189
      80%    233
      90%    356
      95%   1117
      98%   7128
      99%   7269
     100%   7299 (longest request)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

    ・
    ・
    ・
    
    host10 | success | rc=0 >>
    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 192.168.10.1 (be patient)
    
    
    Server Software:        Boa/0.94.14rc21
    Server Hostname:        192.168.10.1
    Server Port:            80
    
    Document Path:          /
    Document Length:        702 bytes
    
    Concurrency Level:      100
    Time taken for tests:   8.025 seconds
    Complete requests:      1000
    Failed requests:        0
    Write errors:           0
    Non-2xx responses:      1000
    Total transferred:      921000 bytes
    HTML transferred:       702000 bytes
    Requests per second:    124.61 [#/sec] (mean)
    Time per request:       802.535 [ms] (mean)
    Time per request:       8.025 [ms] (mean, across all concurrent requests)
    Transfer rate:          112.07 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        1  612 1858.0      3    7025
    Processing:     7  180 133.5    162     627
    Waiting:        7  179 133.5    161     627
    Total:         15  792 1888.9    175    7532
    
    Percentage of the requests served within a certain time (ms)
      50%    175
      66%    227
      75%    276
      80%    343
      90%   4140
      95%   7231
      98%   7483
      99%   7508
     100%   7532 (longest request)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

【weighttpの場合】

    $ ansible clients -k -a "weighttp -c 100 -n 1000 http://192.168.10.1/"
    SSH password: 
    host1 | success | rc=0 >>
    weighttp - a lightweight and simple webserver benchmarking tool
    
    starting benchmark...
    spawning thread #1: 100 concurrent requests, 1000 total requests
    progress:  10% done
    progress:  20% done
    progress:  30% done
    progress:  40% done
    progress:  50% done
    progress:  60% done
    progress:  70% done
    progress:  80% done
    progress:  90% done
    progress: 100% done
    
    finished in 4 sec, 696 millisec and 192 microsec, 212 req/s, 191 kbyte/s
    requests: 1000 total, 1000 started, 1000 done, 0 succeeded, 1000 failed, 0 errored
    status codes: 0 2xx, 0 3xx, 1000 4xx, 0 5xx
    traffic: 921000 bytes total, 921000 bytes http, 0 bytes data
    
    ・
    ・
    ・
    
    host10 | success | rc=0 >>
    weighttp - a lightweight and simple webserver benchmarking tool
    
    starting benchmark...
    spawning thread #1: 100 concurrent requests, 1000 total requests
    progress:  10% done
    progress:  20% done
    progress:  30% done
    progress:  40% done
    progress:  50% done
    progress:  60% done
    progress:  70% done
    progress:  80% done
    progress:  90% done
    progress: 100% done
    
    finished in 7 sec, 251 millisec and 565 microsec, 137 req/s, 124 kbyte/s
    requests: 1000 total, 1000 started, 1000 done, 0 succeeded, 1000 failed, 0 errored
    status codes: 0 2xx, 0 3xx, 1000 4xx, 0 5xx
    traffic: 921000 bytes total, 921000 bytes http, 0 bytes data

実行結果に関しては、Apache Benchmark よりも weighttp の方がすっきりして見やすいかな。