ApacheBenchをDocker Composeでお手軽に利用してみた。

最近いくつかウェブサイトを公開したのだけど、パフォーマンスはどんな感じなのかなと気になったので、計測してみようと思った。

アクセスが殺到して落ちるタイプのサイトではないので、サクッとお手軽にDockerでApache Bench走らせて計測したい。

まずは、時間で借りられるVPS(僕はここ数年ずっとLinode。conohaの半額くらいでお値打ちなので)に、Docker Composeをインストールして、以下のような Docker Compose ファイルを配置する。

version: '3'

services:
  apachebench:
    image: jordi/ab
    entrypoint: /bin/sh

以下のコマンドで起動。

docker-compose run apachebench

Apache Aenchのマンドを入力する。

ab -n 100 -c 3 'https://hogehoge.com/'

-n の引数にはリクエストの回数を、-cは同時に発行するリクエストの回数を指定する。

先日構築したサーバのAPIを実際にテストしてみる。
特に理由はないけど、同時アクセス5、総リクエスト数100としてみた。

/# ab -c 5 -n 100 'https://api.xxxxxxx.net/v1/xxxxxxxxxx'

Server Software:        
Server Hostname:        api.xxxxxxx.net
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,4096,128
TLS Server Name:        api.xxxxxxx.net

Document Path:          /v1/xxxxxxxxxx
Document Length:        79387 bytes

Concurrency Level:      5
Time taken for tests:   2.781 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      7960300 bytes
HTML transferred:       7938700 bytes
Requests per second:    35.96 [#/sec] (mean)
Time per request:       139.055 [ms] (mean)
Time per request:       27.811 [ms] (mean, across all concurrent requests)
Transfer rate:          2795.20 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       21   55  24.7     50     145
Processing:    29   78  32.7     69     176
Waiting:       21   52  22.3     47     146
Total:         60  133  47.6    121     259

Percentage of the requests served within a certain time (ms)
  50%    121
  66%    137
  75%    152
  80%    167
  90%    212
  95%    254
  98%    258
  99%    259
 100%    259 (longest request)

このAPIは、100回すべてのリクエストをエラーを吐くことなく完了し、1秒間に35.96件のリクエストをさばき、1リクエストあたりの処理時間は27.811msecであることが分かった。

負荷をぐっと上げてみよう。
同時リクエスト数100、総リクエスト数500ではどうか。

/ # ab -c 100 -n 500 'https://api.xxxxxxx.net/v1/xxxxxxxxxx'

Concurrency Level:      100
Time taken for tests:   8.679 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      39801500 bytes
HTML transferred:       39693500 bytes
Requests per second:    57.61 [#/sec] (mean)
Time per request:       1735.739 [ms] (mean)
Time per request:       17.357 [ms] (mean, across all concurrent requests)
Transfer rate:          4478.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       23  245 336.6     85    1041
Processing:    83 1251 410.4   1234    3665
Waiting:       30 1201 409.4   1196    3659
Total:        125 1496 465.4   1399    3773

Percentage of the requests served within a certain time (ms)
  50%   1399
  66%   1522
  75%   1638
  80%   1725
  90%   2191
  95%   2493
  98%   2788
  99%   2912
 100%   3773 (longest request)

おお、すべてのリクエストを無事に処理できた。
1秒間に57.61件のリクエストをさばき、1リクエストあたりの処理時間は17.357msecという結果。うん、悪くない。

今回のテストは、Apahce2.4 + PHP7.3 + OPcache + Mongodbで構成されたサーバに、同じリクエストを繰り返し送る形になるので、 OPcache がしっかり働いているみたい。

ものすごい負荷を掛けてテストする場合は、負荷を掛ける側のパフォーマンスを高く保つ必要が出てくるけど、マイクロサービスのテストならこれでも十分参考になるかなと。

いろいろ計測してみたら、node.js + Nuxt.js + SSRのサーバが、思ったりもパフォーマンスが上がらなくて、どうしたものかと思案中。

関係ないけど、KUSANAGIで構築したWoedpress、化け物かというほど速くてカッコいいな。

シェアする

  • このエントリーをはてなブックマークに追加