0.環境

  [CentOS]      5.6 (32bit版カーネル)
  [Apache]      2.2.3
  [ApacheBench] 2.0.40-dev

1.基本

  • (1-1) 実行方法

    • Apacheをインストールすると ab というコマンドで負荷テストができます。

    • 主に使うのは2つのオプションで、下記のように実行します。

    • 後述の “Requests per second” が十分な値になるよう、n と c の値を増やして負荷をかけます。

    • (n : リクエスト数 c : 同時接続数)

      ab -n 10000 -c 100 "http://test.vm/test.php"
      
    • abでテストできるのはサーバ側の処理だけなので、HTMLレンダリングやJavascript実行等、トータルの計測を行いたい場合は、下記のようなツールを検討して下さい。

  • (1-2) 結果の確認

    • 実行すると下記サンプルのような結果が表示されるので、主に2つの項目を確認します。

    • ① Failed requests (失敗したリクエスト数)

      • この項目が0であることを確認します。

      • 但し、表示毎にHTMLのバイト数が異なるページの場合、この項目と内訳を表す “Length:” がともにカウントされます。(1つ前の結果とHTMLのバイト数を比較し、異なる場合は Length がカウントされる仕様)

      • このようなページの場合、”Failed requests” に数字があっても、全て Length が原因ならば無視してかまいませんが、念のため、Apacheのアクセスログを確認し、HTTP Response Code が全て200になっていることを確認しましょう。

    • ② Requests per second (秒間に処理したリクエスト数)

      • 対象サイトのページビュー数(PV)などから基準値を算出し、それをクリアしているかチェックします。

      • (例) 月間 1億 PVのサイトの場合、アクセスが均等という前提で、秒間 38.5 リクエストは達成する必要がありそうです。

        100,000,000 / 30 / 24 / 60 / 60 ≒ 38.5

        (注意) 下記の結果は文字を表示するだけのページなので “Requests per second” はかなり高い数字が出ています。

    • ab 実行結果サンプル

      # ab -n 10000 -c 100 "http://test.vm/test.php"
              
      This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
      Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
      Copyright 2006 The Apache Software Foundation, http://www.apache.org/
              
      Benchmarking test.vm (be patient)
      Completed 1000 requests
      Completed 2000 requests
      Completed 3000 requests
      Completed 4000 requests
      Completed 5000 requests
      Completed 6000 requests
      Completed 7000 requests
      Completed 8000 requests
      Completed 9000 requests
      Finished 10000 requests
              
      Server Software:        Apache/2.2.3
      Server Hostname:        test.vm
      Server Port:            80
              
      Document Path:          /test.php
      Document Length:        80 bytes
              
      Concurrency Level:      100
      Time taken for tests:   5.772060 seconds
      Complete requests:      10000
      Failed requests:        9899
         (Connect: 0, Length: 9899, Exceptions: 0)
      Write errors:           0
      Total transferred:      2418216 bytes
      HTML transferred:       499081 bytes
      Requests per second:    1732.48 [#/sec] (mean)
      Time per request:       57.721 [ms] (mean)
      Time per request:       0.577 [ms] (mean, across all concurrent requests)
      Transfer rate:          409.04 [Kbytes/sec] received
              
      Connection Times (ms)
                    min  mean[+/-sd] median   max
      Connect:        0    0   0.3      0       6
      Processing:     5   56 112.2     55    5769
      Waiting:        5   54  77.3     55    4398
      Total:          5   57 112.2     55    5769
              
      Percentage of the requests served within a certain time (ms)
        50%     55
        66%     56
        75%     58
        80%     59
        90%     62
        95%     69
        98%     77
        99%    284
       100%   5769 (longest request)
      

2.応用

  • (2-1) コマンドサンプル

    • その他、個人的によく使うオプションを記載します。

    • Cookieを指定。(C : [Cookie名]=[値])

      ab -n 100 -c 10 -C 'id=test' "http://test.vm/test.php"
      
    • 複数のCookieを指定。

      ab -n 100 -c 10 -C 'id=test;flag=1' "http://test.vm/test.php"
      
  • (2-2) host名の指定が必要な場合

    • host名の指定が必要なサイトで、かつ /etc/hosts で名前解決ができない環境の場合、下記のようにヘッダオプション(-H) と Host を使います。

    • このオプションは Apache 2.2.22 以降の ab にあり、僕のApacheはそれより古かったため、別途ソースを落としてきてビルドしました。

    • (注意) ab を使うだけなので、Apacheのインストール(make install)は行いません。

      # cd /usr/local/src/
      # wget http://ftp.meisei-u.ac.jp/mirror/apache/dist//httpd/httpd-2.2.27.tar.gz
      # tar xzf httpd-2.2.27.tar.gz
      # cd httpd-2.2.27
      # ./configure
      # make
      # 
      # /usr/local/src/httpd-2.2.27/support/ab -n 100 -c 10 -H 'Host: test.vm' "http://192.168.130.130/test.php"
      #
      # /usr/local/src/httpd-2.2.27/support/ab -V
      #This is ApacheBench, Version 2.3 <$Revision: 655654 $>
      

      上記URLはIP指定ですが、http://test.vm/test.php でアクセスしたのと同じになります。