wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxvf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension/
/usr/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir /tmp/xhprof
chown -R www:www /usr/local/src/xhprof-0.9.4
# 編輯php.ini:
[xhprof]
extension = xhprof.so
xhprof.output_dir=/tmp/xhprof
重啓服務 service php-fpm restart
# 最後返回數組,就表示安裝好了。具體哪些值是什麼意思先別管,由於下面有UI的配置。會很直觀!
yum -y install libjpeg freetype freetype-devel libjpeg-devel liberation-sans-fonts.noarch
自動安裝 yum -y install graphviz
爲Xhprof配置一個訪問站點(虛擬主機)
好比作一個虛擬域名 dev.xhprof.com
綁定到xhprof的站點根目錄 /usr/local/xhprof-0.9.4/xhprof_html
# 找到你要分析的代碼,在代碼開始處添加,start profiling,將會統計內存佔用狀況
xhprof_enable(XHPROF_FLAGS_MEMORY);
# 在代碼結束位置添加
$xhprof_data = xhprof_disable(); // stop profiler, display raw xhprof data for the profiler run
include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php"); # 請注意設置站點 include_path 權限
include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php");
$xhprof_runs = new \XHProfRuns_Default();
// Save the run under a namespace "xhprof_foo".
// **NOTE**:
// By default save_run() will automatically generate a unique
// run id for you. [You can override that behavior by passing
// a run id (optional arg) to the save_run() method instead.]
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
$str = "\r\n------------------\r\n".
"Assuming you have set up the http based UI for \r\n".
"XHProf at some address, you can view run at \r\n".
"http://dev.xhprof.com/index.php?run=$run_id&source=xhprof_foo".
"\r\n------------------\r\n";
echo nl2br($str);
而後進入程序輸出的網址(好比 http://dev.xhprof.com/index.php?run=52c0ea0bef834&source=xhprof_foo ),進行查看
下面是一些參數說明
Inclusive Time 包括子函數全部執行時間。
Exclusive Time/Self Time 函數執行自己花費的時間,不包括子樹執行時間。
Wall Time 花去了的時間或掛鐘時間。
CPU Time 用戶耗的時間+內核耗的時間
Inclusive CPU 包括子函數一塊兒所佔用的CPU
Exclusive CPU 函數自身所佔用的CPU
點擊 [View Full Callgraph] 可以以圖文的形式查看,很方便
注意: 須要使用ctype這個擴展,Callgraph圖片生成依賴一些PHP系統級的函數,因此,最好去掉 php.ini 中的函數禁用