【Apache+php】モジュール、CGI、fcgi比較(単純に速度編)
phpのモジュール版は速い、fastCGIも負けてはいない、遅いけどCGIで安全な運用?自分の目で確認したいのでテスト。
単純な速度比較@CentOS5
サーバーはメモリ512MB、CPUシングルコア。crondとhttpd(apache2)が動いている程度の環境。httpdはyumでインストールして特にチューニングせず。
phpはモジュール版はyumでインストール。fcgi版、cgi版はソースからコンパイル。
次のコマンドを実行 → ab -c 10 -n 1000 http://192.168.xxx.xxx/test.php または test.cgi
test.phpの内容は、画面出力時によくあるhtmlspecialchars()を100回ループで実行したもの。その他の影響は排除したいので、データベース接続、別のプログラムの起動、ファイルの処理などはしていない。
phpモジュール版
速い。速いときは1000アクセスが2秒台で終わる。すごくスムーズ。
httpdのプロセスの1つ1つが、モジュールを組み込んだ分だけ比較的大きい。
Server Software: Apache/2.2.3
Server Hostname: 192.168.130.147
Server Port: 80
Document Path: /test.php
Document Length: 400 bytes
Concurrency Level: 10
Time taken for tests: 3.310 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 592000 bytes
HTML transferred: 400000 bytes
Requests per second: 302.11 [#/sec] (mean)
Time per request: 33.100 [ms] (mean)
Time per request: 3.310 [ms] (mean, across all concurrent requests)
Transfer rate: 174.66 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 3
Processing: 3 32 17.7 27 122
Waiting: 3 32 17.5 27 122
Total: 4 33 17.7 28 122
Percentage of the requests served within a certain time (ms)
50% 28
66% 38
75% 50
80% 52
90% 55
95% 58
98% 62
99% 74
100% 122 (longest request)
実行中のプロセスの様子
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6751 48 15 0 17680 4376 1864 S 1.6 0.9 0:01.11 httpd 6752 48 15 0 17680 4396 1860 S 1.6 0.9 0:01.18 httpd 6754 48 15 0 17680 4372 1860 S 1.6 0.8 0:01.21 httpd 31265 48 15 0 17680 4376 1864 S 1.6 0.9 0:01.15 httpd 31266 48 15 0 17680 4376 1864 S 1.6 0.9 0:01.06 httpd
FastCGIのphp
速い。モジュール版と大差ない。90%以上のアクセスはモジュール版と同じように0.1秒以内に応答している。
httpdのプロセスはスリム。軽そう。
仕組み上、ブラウザ → httpd → FastCGIのやり取りが発生するので、ファイルのアップロード、ダウンロードなどを試すとまた結果が違うかもしれない。
Server Software: Apache/2.2.3
Server Hostname: 192.168.130.146
Server Port: 80
Document Path: /test.php
Document Length: 400 bytes
Concurrency Level: 10
Time taken for tests: 3.281 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 571000 bytes
HTML transferred: 400000 bytes
Requests per second: 304.79 [#/sec] (mean)
Time per request: 32.810 [ms] (mean)
Time per request: 3.281 [ms] (mean, across all concurrent requests)
Transfer rate: 169.95 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 2
Processing: 3 26 125.5 10 1227
Waiting: 3 26 125.5 9 1227
Total: 4 26 125.5 10 1228
Percentage of the requests served within a certain time (ms)
50% 10
66% 12
75% 14
80% 16
90% 22
95% 30
98% 56
99% 1131
100% 1228 (longest request)
実行中のプロセスの様子
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6662 48 15 0 9732 2276 1104 S 1.7 0.4 0:00.94 httpd 12570 48 16 0 20652 5684 3816 S 1.7 1.1 0:00.06 php-cgi 12571 48 18 0 20652 5672 3816 S 1.7 1.1 0:00.06 php-cgi 31272 48 15 0 9732 2300 1120 S 1.4 0.4 0:00.82 httpd 6658 48 15 0 9732 2280 1108 S 1.1 0.4 0:00.73 httpd 31274 48 15 0 9732 2268 1092 S 1.1 0.4 0:00.60 httpd
CGIとしてphpを実行
遅い。1000アクセスさばくのに30秒ぐらいかかる。CGIがアクセスの都度起動するとこうなるのか。
しかし、1000アクセスも集中した場合の問題で、半分ぐらいのアクセスならほぼ即座に応答している。
Server Software: Apache/2.2.3
Server Hostname: 192.168.130.148
Server Port: 80
Document Path: /test.cgi
Document Length: 400 bytes
Concurrency Level: 10
Time taken for tests: 25.211 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 580576 bytes
HTML transferred: 400000 bytes
Requests per second: 39.67 [#/sec] (mean)
Time per request: 252.110 [ms] (mean)
Time per request: 25.211 [ms] (mean, across all concurrent requests)
Transfer rate: 22.49 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 2
Processing: 23 251 661.1 26 8066
Waiting: 20 239 652.4 25 8066
Total: 24 251 661.1 27 8066
Percentage of the requests served within a certain time (ms)
50% 27
66% 36
75% 66
80% 127
90% 991
95% 1522
98% 2083
99% 3182
100% 8066 (longest request)
実行中のプロセスの様子
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 820 48 15 0 10068 2676 1204 S 0.3 0.5 0:00.39 httpd 12440 48 18 0 20100 4796 3256 R 0.3 0.9 0:00.01 test.cgi 12448 48 18 0 20240 5088 3400 R 0.3 1.0 0:00.01 test.cgi 12465 48 17 0 20668 5656 3816 R 0.3 1.1 0:00.01 test.cgi 27022 48 15 0 10068 2684 1204 S 0.3 0.5 0:00.49 httpd
で、どれがいいのか
単純なサーバー側の処理をするときの、1000アクセスが殺到したときの速度だけを見た。速度だけ言えば、アクセスが少なければどれであっても大差ない。その他の特性も考慮して決めるべし。