2007年02月24日
CとPerlでCGI、FastCGIのベンチマークを取ってみました
たたみラボさんのかなり古いエントリなのですが、
GDなどの外部ライブラリを使わずに、全部Cで書いているCGIです。なのでたぶん高速。よく「CGIはプロセスが毎回立ち上がるから遅い」なんてことが言われますが、プロセスそのものが小さければ速いはず。だったらCで書けばいい。
とあったので、ちょっと試してみました。
前々職の同僚が「FastCGI馬鹿一代!」という感じの職人然とした人で、常々「CのCGIよりLightweight LanguageのFastCGIの方が速い!」と叩き込まれてきたので...。
条件は、環境変数の一覧を表示するだけのCとPerlのページを、CGI及びFastCGIで動かしてみて、Apache Benchで100回リクエストして比較してみました。
結果は、
| C-CGI | C-FastCGI | Perl-CGI | Perl-FastCGI | |
|
100回試行にかかる時間 |
3.47秒 | 0.24秒 | 5.28秒 | 0.29秒 |
| 秒あたり実行可能回数 | 28.8回 | 421.9回 | 19.9回 | 350.5回 |
という感じになりました。
やっぱり、プロセスを常駐させた方が大分早いようです。
CGIでのCとPerlの実行速度差に比べ、PerlのFastCGIがCのFastCGIと比べて遜色ないのは、Perlはプロセス起動時に中間コードにコンパイルされるらしいのですが、FastCGIだとその中間コードがプロセスが落ちないため保持されるので、リクエスト毎のコンパイル時間が不要になるためらしいです。
FastCGI職人からの聞きかじりですが...。
C-CGIとPerl-FastCGIの間の比較は、所詮環境変数表示だけのプログラムですので、複雑なプログラムになると実ロジック部分のCとPerlの実行速度差でどうなるかは判りませんが、今回は環境変数表示だけのプログラムでの結果なので、プロセスは小さいんじゃないかと思います。
なので、プロセスが小さければCGIでも...というのは、?がつきました。
(とか書きつつ、元記事の意図するところが、元々プロセスの小さいプログラム同士の比較ではなく、「同じ機能」のプログラムなら、外部ライブラリ等を使わずCで書いた方がプロセスは小さくなる、という意味かもしれないと今書きながら思った。環境変数表示みたいな差の出ようのないプログラムならともかく、ある程度のロジックがあれば確かに作り方によって劇的にプロセスのサイズ差は出るかもしれませんね...。)
Apache Benchの生出力は、追記に。
C-CGI:
Server Software: Apache/2.2.0
Server Hostname: kokogiko.net
Server Port: 80
Document Path: /echo.cgi
Document Length: 2499 bytes
Concurrency Level: 1
Time taken for tests: 3.474726 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 277906 bytes
HTML transferred: 249900 bytes
Requests per second: 28.78 [#/sec] (mean)
Time per request: 34.747 [ms] (mean)
Time per request: 34.747 [ms] (mean, across all concurrent requests)
Transfer rate: 77.99 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 34 34 0.4 34 36
Waiting: 33 33 0.8 34 36
Total: 34 34 0.4 34 36
WARNING: The median and mean for the waiting time are not within a normal deviation
These results are probably not that reliable.
Percentage of the requests served within a certain time (ms)
50% 34
66% 34
75% 34
80% 34
90% 34
95% 35
98% 36
99% 36
100% 36 (longest request)
C-FastCGI:
Server Software: Apache/2.2.0
Server Hostname: kokogiko.net
Server Port: 80
Document Path: /echo.fcgi
Document Length: 1413 bytes
Concurrency Level: 1
Time taken for tests: 0.237027 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 168800 bytes
HTML transferred: 141300 bytes
Requests per second: 421.89 [#/sec] (mean)
Time per request: 2.370 [ms] (mean)
Time per request: 2.370 [ms] (mean, across all concurrent requests)
Transfer rate: 691.90 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 2 2 0.0 2 2
Waiting: 0 1 0.9 2 2
Total: 2 2 0.0 2 2
WARNING: The median and mean for the waiting time are not within a normal deviat ion
These results are probably not that reliable.
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 2
95% 2
98% 2
99% 2
100% 2 (longest request)
Perl-CGI:
Server Software: Apache/2.2.0
Server Hostname: kokogiko.net
Server Port: 80
Document Path: /pecho.cgi
Document Length: 1337 bytes
Concurrency Level: 1
Time taken for tests: 5.28124 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 161772 bytes
HTML transferred: 133700 bytes
Requests per second: 19.89 [#/sec] (mean)
Time per request: 50.281 [ms] (mean)
Time per request: 50.281 [ms] (mean, across all concurrent requests)
Transfer rate: 31.22 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 45 49 21.8 47 258
Waiting: 44 48 21.8 46 257
Total: 45 49 21.8 47 258
Percentage of the requests served within a certain time (ms)
50% 47
66% 47
75% 48
80% 48
90% 50
95% 52
98% 98
99% 258
100% 258 (longest request)
Perl-FastCGI:
Server Software: Apache/2.2.0
Server Hostname: kokogiko.net
Server Port: 80
Document Path: /pecho.fcgi
Document Length: 1365 bytes
Concurrency Level: 1
Time taken for tests: 0.285330 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 164000 bytes
HTML transferred: 136500 bytes
Requests per second: 350.47 [#/sec] (mean)
Time per request: 2.853 [ms] (mean)
Time per request: 2.853 [ms] (mean, across all concurrent requests)
Transfer rate: 560.75 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 2 2 0.3 2 5
Waiting: 1 1 1.1 2 5
Total: 2 2 0.3 2 5
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 2
95% 2
98% 3
99% 5
100% 5 (longest request)
![[ここギコ!]](http://kokogiko.net/logo.png)





・MovableType 3.2、MT::App::Trackback.pmの修正(selvirremdor)
・MovableType 3.2、MT::App::Trackback.pmの修正(antulaseesi)
・3D PaPaGO! 登場(pereezdkv)
・MovableType 3.2、MT::App::Trackback.pmの修正(spezinstr)
・MovableType 3.2、MT::App::Trackback.pmの修正(dimdimov)
・MovableType 3.2、MT::App::Trackback.pmの修正(deanteywee)
・MovableType 3.2、MT::App::Trackback.pmの修正(keyjiolso)
・MovableType 3.2、MT::App::Trackback.pmの修正(leyliautumfe)
・MovableType 3.2、MT::App::Trackback.pmの修正(selvirremdor)