實戰 FastCGI(轉)四

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 實戰 FastCGI(轉)四 (http://www.webasp.net/article/8/7731.htm)
-- 作者:未知
-- 發佈日期: 2003-09-13
4. FastCGI 有多快?

看完安裝 FastCGI 的阿帕契模塊,以及無聊的程序設計注意事項後,我們來看看一些可以讓人振奮精神的數據,效能比較 (bench mark) 總是計算機玩家的最愛 :-)

4.1 評比工具 - ApacheBench
在阿帕契服務器的套件中,有一個叫做 ab (ApacheBench) 的工具。ApacheBench 主要是用來測試阿帕契服務器執行效率用的,我們就以 ApacheBench 做為 CGI vs. FastCGI 的評比工具。

ApacheBench 可以針對某個特定的 URL 仿真出連續的聯機請求,同時還可以仿真出同時間點數個相同的聯機請求,因此利用 ApacheBench 可幫助我們在網站開發期間仿真實際上線可能的情況,利用仿真出來的數據做為調整服務器設定或程序的依據。 ApacheBench 的用法如下:

Usage: /usr/local/apache/bin/ab [options] [http://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make
-t timelimit Seconds to max. wait for responses
-p postfile File containg data to POST
-T content-type Content-type header for POSTing
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-V Print version number and exit
-k Use HTTP KeepAlive feature
-h Display usage information (this message)

假設我們要對 echo.fcg 做測試,仿真 1000 次的聯機請求,而且同一時間有 20 個並行的 (concurrent) 聯機請求的情況,只要在命令列模式下執行

$ ab -n 1000 -c 20 http://localhost/fcgi-bin/echo.fcg
稍等一會,ApacheBench 會把結果秀出來,

This is ApacheBench, Version 1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-1999 The Apache Group, http://www.apache.org/

Benchmarking localhost (be patient)... Server Software: Apache/1.3.6
Server Hostname: localhost
Server Port: 80
Document Path: /fcgi-bin/echo.fcg
Document Length: 995 bytes
Concurrency Level: 20
Time taken for tests: 6.859 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 1142000 bytes
HTML transferred: 995000 bytes
Requests per second: 145.79
Transfer rate: 166.50 kb/s received

Connnection Times (ms)
min avg max

Connect: 0 4 61
Processing: 62 128 771
Total: 62 132 832

以上結果指出,在同時間 20 個聯機請求 (Concurrency Level) 的情況下,完成 1000 次的聯機請求,共花了 6.859 秒 (Time taken for tests),因此這個程序每秒平均可以處理 (Requests per second) 145.79 個聯機請求。
在接下來的評比測試中,我們就以每秒可以處理的聯機請求數目來做為效能評比的依據。

4.2 CGI vs. FastCGI
前面提過利用 fcgi_stdio.h 函式庫編譯出來的 FastCGI 程序也兼容於 CGI 模式,因此我們只要把 fcgi-devkit-2.1 套件附的範例程序 echo.fcg 複製到 /cgi-bin 目錄下,並且把文件名改成 echo.cgi,這支範例程序就可分別以 CGI 模式和 FastCGI 模式來執行,並且做比較。

首先分別對 CGI模式執行的 http://localhost/cgi-bin/echo.cgi 以及 FastCGI 模式的 http://localhost/fcgi-bin/echo.fcg 連續送出 10, 100, 1000, 10000 次的聯機請求,得到的平均每秒可處理的請求 (Requests per second) 結果為:

聯機數目 10 100 1000 10000
CGI 52.63 53.08 52.24 51.49
FastCGI (static mode) 204.08 224.22 146.78 207.14

接下來再分別以 Concurrency Level 為 10, 50, 100 的情況下做測試,得到 Requests per second 結果為:

Concurrency 聯機數目 10 100 1000 10000
10 CGI 38.31 46.55 53.61 55.09
10 FastCGI 185.19 208.33 162.63 177.14
50 CGI 27.25 33.16 50.72 53.99
50 FastCGI 92.59 176.37 196.58 196.88
100 CGI 17.92 24.84 48.14 52.84
100 FastCGI 86.21 187.27 195.54 193.17

由上述數據看來,對同一支程序 (echo.c) 而言,使用 FastCGI 模式來執行,速度提升了 3-4 倍。可見得 FastCGI 對網站程序的效能提升上具有相當大的助益,尤其當使用量很大時,其效益更加明顯。

以上的測試條件並不是十分嚴謹,主要在讓你瞭解使用 FastCGI 之後對於效能及速度上一個概括的比較,也提供一個可供網站開發者可以評量的依據。

實際上,對一個初始化動作複雜,例如要先和數據庫建立聯機,或是配置內存,做變量初始化的程序來說,使用 FastCGI 可以比原先 CGI 在效能上增加更多,速度更快。而對一個有數據庫可聯機數目限制的系統來說,使用 FastCGI 就好像一個 Application Server 一樣,不用擔心 CGI 一次 fork 太多,超過聯機數目上限 (FastCGI 可以設定一次跑幾支)。

4.3 找出 Memory Leak
善用 ApacheBench 這個工具,還可以幫助網站程序發展人員找出在 FastCGI 程序中隱藏的 Memory Leak 臭蟲。

每一支 FastCGI 程序在處理完一個聯機請求後的狀況都應該相同,我們利用 ApacheBench 對欲測試的程序送出上百次或上千次的聯機請求以仿真實際上線的狀況,如果發現程序佔用的內存愈來愈多而且不會減少的話,表示程序有 Memory Leak 的問題。

雖然對 FastCGI 程序而言,當它一直不斷吃掉系統資源到資料耗盡後會自動退出 (core dump 或是 exit) ,釋放所佔用的資源然後再重新激活,但這將會影響系統其它功能的正常運作,所以撰寫 FastCGI 程序一定特別小心。

webasp.net