Sunday, December 11, 2005

增強 PHP 程式碼執行速度的模組:

增強 PHP 程式碼執行速度的模組:

MM Cache 增強速度模組:
 
我們上面的介紹都是在於安裝與架設 LAMP 伺服器(Linux + Apache + MySQL + PHP ),不過,如果您曾經瀏覽過 PHP 的網頁時,或許會發現:『咦!怎麼 PHP 的速度慢慢的.....』,這是怎麼一回事啊!?PHP 不是號稱速度上面的反應是很快速的嗎?怎麼會慢慢的呢?雖然 PHP 的程式已經很快了,但是因為電腦僅認識 0 與 1 的 binary file 來執行,而由於 PHP 程式不需要編譯即可透過 PHP 核心與其相關函式庫來執行,不過,如此一來還是多了一道手續,導致執行效能還是不比傳統的經過編譯的程式語言來的快(例如C程式語言之類的!)。那怎麼辦?有辦法加快 PHP 程式的速度嗎?嗯!讓我們先換個角度想,如果我們可以將 PHP 程式預先轉換成為可直接執行的 binary file ,那麼不就可以直接讀取,進而加快速度嗎?沒錯!是這樣~這東西稱為預編器~其中, MM Cache 就是一個很不錯的 PHP 預編器。 MM Cache 可以將您的 PHP 程式與 PHP 核心及相關函式庫預先編譯後暫存下來,以提供未來使用時,可以直接執行,加上他可以優化您的 PHP 程式,因此,可以讓您的 PHP 網頁速度增快不少喔! MM Cache 囉!他的官方網站在:http://turck-mmcache.sourceforge.net/index_old.html。目前 MM Cache 已經出到了2.3.9 ( 2003/04/10 釋出),不過,他僅在 PHP 4.1 以上版本以及 Apache 1.3 與 2.xx 版本測試過,如果你的 Apache 與 PHP 不是這些版本,那就抱歉啦!不一定可以使用的!不過,呵呵!我們上面提供的 Tarball 的安裝方法本來就是這樣的版本,所以您可以輕易的安裝好 MM Cache 喔!廢話不多說,趕緊來安裝吧!
 
o 下載:

你可以在官方網站下載最新的版本(http://turck-mmcache.sourceforge.net/index_old.html),也可以在我們網站下載鳥哥試過的版本:
http://linux.vbird.org/download/index.php#www_mmcache
o   安裝:

安裝 MM Cache 真的是很簡單喔!趕緊來安裝吧!
[root@test root]# cd /usr/local/src
[root@test src]# tar -zxvf /完整路徑/turck-mmcache-2.3.9.tar.gz
# 會產生一個名為 turck-mmcache-2.3.9 的目錄
 
[root@test src]# cd turck-mmcache-2.3.9
[root@test turck-mmcache-2.3.9]# phpize
# 這個指令是 PHP 套件所提供的!可以建置好你的 mmcache 原始碼
 
[root@test turck-mmcache-2.3.9]# ./configure --enable-mmcache=shared
[root@test turck-mmcache-2.3.9]# make && make install
# 這個動作會編譯一個名為 mmcache.so 的動態函式庫模組,
# 並且會主動的將他安裝在 /usr/lib/php4 這個目錄當中!
# 這樣就安裝完畢了!很簡單吧!
o   設定:

在 MM Cache 的設定方面需要更動兩個地方,第一個是動態函式庫載入的設定,第二個則是 PHP 的設定!
1. 設定主動載入動態函式庫模組:
[root@test root]# vi /etc/ld.so.conf
# 在這個檔案內加入底下這一行:
/usr/lib/php4
 
[root@test root]# ldconfig
# 上面這兩個步驟比較有趣一點,在 ldconfig 這個指令的功能是:
# 『載入動態函式庫到記憶體當中做為快取』之用,至於載入的動態函式庫則是
# 根據 /etc/ld.so.conf 這個檔案的設定為準!這的動作只要第一次設定時進行
# 即可,未來在開機完成之後,系統會主動的載入動態函式庫的!
# 另外請注意, ld.so.conf 裡面只要寫『目錄』即可!
 
2. 修改 php.ini
# 請注意,由於每個人的 php.ini 都不相同,例如使用 RPM 安裝者,應該是
# /etc/php.ini ,但是我上面的安裝設定卻是 /usr/local/php4/php.ini
# 請依照您的主機來設定喔!
[root@test root]# vi /完整路徑/php.ini
# 在這個檔案的最後一行加入底下這幾行:
;;;;;;;;;;;;
; MM Cache ;
;;;;;;;;;;;;
extension="mmcache.so"
mmcache.shm_size="16"
mmcache.cache_dir="/tmp/mmcache"
mmcache.enable="1"
mmcache.optimizer="1"
mmcache.check_mtime="1"
mmcache.debug="0"
mmcache.filter=""
; end of mmcache
 
3. 建立快取目錄:
[root@test root]# mkdir /tmp/mmcache
[root@test root]# chmod 0777 /tmp/mmcache
 
4.重新啟動 Apache
[root@test root]# /etc/rc.d/init.d/httpd restart
# 或
[root@test root]# /usr/local/apache2/bin/apachectl restart
這樣一個簡單的小步驟,嘿嘿!您的 PHP 程式碼的反應性~啊~增快很多很多喔! ^_^  
Apache 的效能測試
 
事實上,安裝 Apache 的時候,Apache 就已經提供了一個效能測試 ( benchmark ) 的軟體了!那就是 ab 這個程式!怎麼用呢?就直接用啊!
 
[root@test root]# /usr/sbin/ab [-dSk] [-c number] [-n number] 網頁.php
參數說明:
-d :不要顯示 saved table 的百分比資料;通常不要那個資料,所以會加 -d
-k :還記得上面的 KeepAlive 吧!加入 -k 才會以這樣的功能測試;
-S :不顯示長訊息,僅顯示類似 min/avg/max 的簡短易懂訊息!
-c :同時有多少個『同時連線』的設定(可想成同時連線的 IP )
-n :同一個連線建立幾個要求通道!(可想成同一個 IP 要求的幾條連線)
更多的訊息請自行 man ab 喔!
範例:
[root@test root]# /usr/sbin/ab -dSk -c100 -n100 > http://linux.vbird.org/home.php
This is ApacheBench, Version 1.3d <$Revision: 1.67 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking linux.vbird.org (be patient).....done
Server Software: Apache/1.3.27
Server Hostname: linux.vbird.org
Server Port: 80

Document Path: /home.php
Document Length: 51736 bytes

Concurrency Level: 100
Time taken for tests: 1.648 seconds
Complete requests: 100
Failed requests: 0
Broken pipe errors: 0
Keep-Alive requests: 0
Total transferred: 5795454 bytes
HTML transferred: 5775070 bytes
Requests per second: 60.68 [#/sec] (mean)
Time per request: 1648.00 [ms] (mean)
Time per request: 16.48 [ms] (mean, across all concurrent requests)
Transfer rate: 3516.66 [Kbytes/sec] received

Connnection Times (ms)
min avg max
Connect: 52 309 369
Processing: 467 687 1079
Total: 467 996 1405
 
這樣就能夠瞭解您的 Apache 效能了!因為我是在本機上面測試的,所以速度上面當然是很快囉!建議可以到遠端同樣使用 ab 來測試一下你的 Apache 效能,尤其是加上了 mm cache 之後,看看能不能增快速度呢? ( 註:這個 ab 程式對於讀取 MySQL 之類的網頁似乎沒有辦法成功的完成測試的樣子,所以請不要使用 phpBB2 的網頁來測試喔!盡量直接以 PHP 的網頁來測試! ) 嘿嘿!

1 Comments:

Blogger HAVE ONE`S WAY said...

如果找不到"phpize"
請安裝"libtool*.rpm"和"php-devel*.rpm"
就可以了

3/25/2006 06:37:00 PM  

Post a Comment

<< Home