apache+mod_php VS nginx+php-fpm(パフォーマンス比較)

前回の続きです → apache+mod_php VS nginx+php-fpm(環境構築)

やりたいことは、webサーバー経由でphpがDBからデータ取ってきて返すのをどこまでwebサーバーが耐えられるかな?ってところです。
ようはwebサーバーがさばけるアクセス数ですね。
ついでだから、サーバーの負荷も一緒に覗いてみようかな?って感じです。

といっても検証環境は、VM上のゲスト1サーバー全部入りです。
純粋にwebサーバーだけの負荷じゃーないです。

前置きはこのへんでいってみましょう。
負荷ツールは、apachebenchを使います。
apachebenchでwget投げて、vmstatでリソースを見てる感じです。

検証内容

    • 多重度1で100回リクエス
    • 多重度10で20秒リクエス
    • 多重度100で20秒リクエス
    • 多重度100で180秒リクエス
    • 多重度300で180秒リクエス

まずは、何も無い時のvmstat

多重度1で100回リクエス

これは単体でのパフォーマンスでベースになるものとします。

[nginx-test]# date; ab -c 1 -n 100 -g apache.1-100.ab -H "Host: localhost" http://localhost:80/select.php; date
2012年  2月 12日 日曜日 22:51:59 JST
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 localhost (be patient).....done


Server Software:        Apache/2.2.22
Server Hostname:        localhost
Server Port:            80

Document Path:          /select.php
Document Length:        145 bytes

Concurrency Level:      1
Time taken for tests:   5.913505 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      37800 bytes
HTML transferred:       14500 bytes
Requests per second:    16.91 [#/sec] (mean)
Time per request:       59.135 [ms] (mean)
Time per request:       59.135 [ms] (mean, across all concurrent requests)
Transfer rate:          6.09 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:    54   58   9.9     55     103
Waiting:       54   58   9.9     55     103
Total:         54   58   9.9     55     103

Percentage of the requests served within a certain time (ms)
  50%     55
  66%     56
  75%     57
  80%     57
  90%     66
  95%     87
  98%     99
  99%    103
 100%    103 (longest request)
2012年  2月 12日 日曜日 22:52:05 JST
      • vmstat

    • nginx
      • ab結果
[nginx-test]# date; ab -c 1 -n 100 -g nginx.1-100.ab -H "Host: localhost" http://localhost:81/select.php; date
2012年  2月 12日 日曜日 22:52:23 JST
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 localhost (be patient).....done


Server Software:        nginx/1.0.12
Server Hostname:        localhost
Server Port:            81

Document Path:          /select.php
Document Length:        80 bytes

Concurrency Level:      1
Time taken for tests:   6.24247 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      22700 bytes
HTML transferred:       8000 bytes
Requests per second:    16.60 [#/sec] (mean)
Time per request:       60.242 [ms] (mean)
Time per request:       60.242 [ms] (mean, across all concurrent requests)
Transfer rate:          3.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:    54   59  11.4     56     113
Waiting:       54   59  11.4     56     113
Total:         54   59  11.4     56     113

Percentage of the requests served within a certain time (ms)
  50%     56
  66%     57
  75%     58
  80%     58
  90%     66
  95%     96
  98%    107
  99%    113
 100%    113 (longest request)
2012年  2月 12日 日曜日 22:52:29 JST
      • vmstat


多重度10で20秒リクエス

[nginx-test]# date; ab -c 10 -t 20 -g apache.10-20.ab -H "Host: localhost" http://localhost:80/select.php; date
2012年  2月 12日 日曜日 22:54:22 JST
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 localhost (be patient)
Finished 850 requests


Server Software:        Apache/2.2.22
Server Hostname:        localhost
Server Port:            80

Document Path:          /select.php
Document Length:        145 bytes

Concurrency Level:      10
Time taken for tests:   20.31376 seconds
Complete requests:      850
Failed requests:        0
Write errors:           0
Total transferred:      321678 bytes
HTML transferred:       123395 bytes
Requests per second:    42.43 [#/sec] (mean)
Time per request:       235.663 [ms] (mean)
Time per request:       23.566 [ms] (mean, across all concurrent requests)
Transfer rate:          15.68 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:    87  233  83.8    228     629
Waiting:       87  233  83.8    228     629
Total:         87  233  83.8    228     629

Percentage of the requests served within a certain time (ms)
  50%    228
  66%    265
  75%    280
  80%    296
  90%    339
  95%    383
  98%    425
  99%    483
 100%    629 (longest request)
2012年  2月 12日 日曜日 22:54:43 JST
      • vmstat

    • nginx
      • ab結果
[nginx-test]# date; ab -c 10 -t 20 -g nginx.10-20.ab -H "Host: localhost" http://localhost:81/select.php; date
2012年  2月 12日 日曜日 22:55:00 JST
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 localhost (be patient)
Finished 838 requests


Server Software:        nginx/1.0.12
Server Hostname:        localhost
Server Port:            81

Document Path:          /select.php
Document Length:        80 bytes

Concurrency Level:      10
Time taken for tests:   20.2343 seconds
Complete requests:      838
Failed requests:        0
Write errors:           0
Total transferred:      190453 bytes
HTML transferred:       67120 bytes
Requests per second:    41.90 [#/sec] (mean)
Time per request:       238.691 [ms] (mean)
Time per request:       23.869 [ms] (mean, across all concurrent requests)
Transfer rate:          9.25 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       8
Processing:    68  236  37.7    232     386
Waiting:       68  236  37.7    232     386
Total:         68  236  37.7    232     386

Percentage of the requests served within a certain time (ms)
  50%    232
  66%    249
  75%    261
  80%    268
  90%    287
  95%    299
  98%    330
  99%    343
 100%    386 (longest request)
2012年  2月 12日 日曜日 22:55:20 JST
      • vmstat


多重度100で20秒リクエス

[nginx-test]# date; ab -c 100 -t 20 -g apache.100-20.ab -H "Host: localhost" http://localhost:80/select.php; date
2012年  2月 12日 日曜日 22:55:41 JST
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 localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Finished 17310 requests


Server Software:        Apache/2.2.22
Server Hostname:        localhost
Server Port:            80

Document Path:          /select.php
Document Length:        145 bytes

Concurrency Level:      100
Time taken for tests:   20.27582 seconds
Complete requests:      17310
Failed requests:        16575
   (Connect: 0, Length: 16575, Exceptions: 0)
Write errors:           0
Total transferred:      10836483 bytes
HTML transferred:       6803020 bytes
Requests per second:    864.31 [#/sec] (mean)
Time per request:       115.699 [ms] (mean)
Time per request:       1.157 [ms] (mean, across all concurrent requests)
Transfer rate:          528.37 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       4
Processing:     0  107 486.3      6    4398
Waiting:        0  107 486.3      5    4397
Total:          0  107 486.3      6    4398

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      8
  75%     11
  80%     13
  90%     30
  95%    101
  98%   2408
  99%   2727
 100%   4398 (longest request)
2012年  2月 12日 日曜日 22:56:01 JST
      • vmstat

    • nginx
      • ab結果
[nginx-test]# date; ab -c 100 -t 20 -g nginx.100-20.ab -H "Host: localhost" http://localhost:81/select.php; date
2012年  2月 12日 日曜日 22:56:21 JST
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 localhost (be patient)
Finished 837 requests


Server Software:        nginx/1.0.12
Server Hostname:        localhost
Server Port:            81

Document Path:          /select.php
Document Length:        80 bytes

Concurrency Level:      100
Time taken for tests:   20.35639 seconds
Complete requests:      837
Failed requests:        0
Write errors:           0
Total transferred:      190226 bytes
HTML transferred:       67040 bytes
Requests per second:    41.78 [#/sec] (mean)
Time per request:       2393.744 [ms] (mean)
Time per request:       23.937 [ms] (mean, across all concurrent requests)
Transfer rate:          9.23 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4  13.9      0      52
Processing:    94 2243 441.0   2369    2631
Waiting:       92 2243 441.3   2369    2631
Total:         94 2247 433.1   2369    2631

Percentage of the requests served within a certain time (ms)
  50%   2369
  66%   2387
  75%   2400
  80%   2407
  90%   2431
  95%   2453
  98%   2471
  99%   2480
 100%   2631 (longest request)
2012年  2月 12日 日曜日 22:56:42 JST
      • vmstat


多重度100で180秒リクエス

[nginx-test]# date; ab -c 100 -t 180 -g apache.100-180.ab -H "Host: localhost" http://localhost:80/select.php; date
2012年  2月 12日 日曜日 22:57:09 JST
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 localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Finished 50000 requests


Server Software:        Apache/2.2.22
Server Hostname:        localhost
Server Port:            80

Document Path:          /select.php
Document Length:        145 bytes

Concurrency Level:      100
Time taken for tests:   72.610223 seconds
Complete requests:      50000
Failed requests:        47185
   (Connect: 0, Length: 47185, Exceptions: 0)
Write errors:           0
Total transferred:      31120915 bytes
HTML transferred:       19470915 bytes
Requests per second:    688.61 [#/sec] (mean)
Time per request:       145.220 [ms] (mean)
Time per request:       1.452 [ms] (mean, across all concurrent requests)
Transfer rate:          418.55 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       5
Processing:     0  142 572.3      6    6452
Waiting:        0  142 572.3      6    6452
Total:          0  142 572.3      6    6452

Percentage of the requests served within a certain time (ms)
  50%      6
  66%     11
  75%     14
  80%     17
  90%     36
  95%   1509
  98%   2592
  99%   2949
 100%   6452 (longest request)
2012年  2月 12日 日曜日 22:58:22 JST
      • vmstat

    • nginx
      • ab結果
[nginx-test]# date; ab -c 100 -t 180 -g nginx.100-180.ab -H "Host: localhost" http://localhost:81/select.php; date
2012年  2月 12日 日曜日 22:58:43 JST
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 localhost (be patient)
Completed 5000 requests
Finished 7553 requests


Server Software:        nginx/1.0.12
Server Hostname:        localhost
Server Port:            81

Document Path:          /select.php
Document Length:        80 bytes

Concurrency Level:      100
Time taken for tests:   180.34281 seconds
Complete requests:      7553
Failed requests:        0
Write errors:           0
Total transferred:      1714758 bytes
HTML transferred:       604320 bytes
Requests per second:    41.95 [#/sec] (mean)
Time per request:       2383.613 [ms] (mean)
Time per request:       23.836 [ms] (mean, across all concurrent requests)
Transfer rate:          9.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.5      0      18
Processing:   114 2366 147.6   2375    2680
Waiting:      113 2366 147.6   2374    2680
Total:        114 2366 146.6   2375    2680

Percentage of the requests served within a certain time (ms)
  50%   2375
  66%   2392
  75%   2404
  80%   2412
  90%   2433
  95%   2451
  98%   2473
  99%   2489
 100%   2680 (longest request)
2012年  2月 12日 日曜日 23:01:43 JST
      • vmstat


多重度300で180秒リクエス

[nginx-test]# date; ab -c 300 -t 180 -g apache.300-180.ab -H "Host: localhost" http://localhost:80/select.php; date
2012年  2月 12日 日曜日 23:02:17 JST
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 localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Finished 50000 requests


Server Software:        Apache/2.2.22
Server Hostname:        localhost
Server Port:            80

Document Path:          /select.php
Document Length:        145 bytes

Concurrency Level:      300
Time taken for tests:   50.202221 seconds
Complete requests:      50000
Failed requests:        48181
   (Connect: 0, Length: 48181, Exceptions: 0)
Write errors:           0
Total transferred:      31259311 bytes
HTML transferred:       19609311 bytes
Requests per second:    995.97 [#/sec] (mean)
Time per request:       301.213 [ms] (mean)
Time per request:       1.004 [ms] (mean, across all concurrent requests)
Transfer rate:          608.06 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   24 357.8      0   21003
Processing:     3  241 995.7     60   27077
Waiting:        3  240 995.7     60   27076
Total:          3  265 1069.5     60   30908

Percentage of the requests served within a certain time (ms)
  50%     60
  66%     73
  75%     84
  80%     95
  90%    178
  95%   1141
  98%   3384
  99%   4414
 100%  30908 (longest request)
2012年  2月 12日 日曜日 23:03:08 JST
      • vmstat


この時点でswap大量発生してたので、on/offしてみたり

[nginx-test]# sudo swapoff -a
[nginx-test]# sudo swapon -a
    • nginx
      • ab結果
[nginx-test]# date; ab -c 300 -t 180 -g nginx.300-180.ab -H "Host: localhost" http://localhost:81/select.php; date
2012年  2月 12日 日曜日 23:56:24 JST
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 localhost (be patient)
Completed 5000 requests
Finished 6583 requests


Server Software:        nginx/1.0.12
Server Hostname:        localhost
Server Port:            81

Document Path:          /select.php
Document Length:        80 bytes

Concurrency Level:      300
Time taken for tests:   180.74615 seconds
Complete requests:      6583
Failed requests:        271
   (Connect: 0, Length: 271, Exceptions: 0)
Write errors:           0
Non-2xx responses:      271
Total transferred:      1579538 bytes
HTML transferred:       608913 bytes
Requests per second:    36.56 [#/sec] (mean)
Time per request:       8206.347 [ms] (mean)
Time per request:       27.354 [ms] (mean, across all concurrent requests)
Transfer rate:          8.56 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2  15.6      0     114
Processing:   122 7330 13920.8   3263  105004
Waiting:      122 7330 13920.7   3263  105003
Total:        122 7332 13930.7   3263  105113

Percentage of the requests served within a certain time (ms)
  50%   3263
  66%   3647
  75%   4790
  80%   5843
  90%   8090
  95%  27193
  98%  60116
  99%  69017
 100%  105113 (longest request)
2012年  2月 12日 日曜日 23:59:24 JST
      • vmstat

あれ?"軽量"ってのが売りじゃなかったっけ?
と、少ないアクセスならapacheも同等に戦えてます。

が、お腹いっぱいになるのが早い。
すでに「ab -c 100 -t 20」からはapacheは「Failed requests」を返してるので正常にさばけてない。
しかし、nginxは「ab -c 100 -t 180」まで正常にさばいてますね。
「ab -c 300 -t 180」ここまでいっちゃうとnginxもさばけなくなってしました。
この時のアクセスログのhttp statusは「499」になってました。最初見たときはちょっとびっくりした。

…"GET /select.php HTTP/1.0" 499 0 "-" "ApacheBench/2.0.40-dev"

と、まぁすでに世間で出てる通りの結果になりました。
さくっとしたものならapacheでいいんじゃね?
がんばりたいの?nginx使っちゃいなよ!!
もちろん、チューニング次第じゃ両者ともにまだまだ伸びると思いますが、参考値程度にはなったかなーと思います。

で、次回は、nginx+php-fpm(複数)でやってみようと思います。
php-fpmを何個か上げればさらにさばけるよね?って感じです。