xhprof是什么
XHProf是facebook 開發(fā)的一個測試php性能的擴展垫释,本文記錄了在PHP應(yīng)用中使用XHProf對PHP進行性能優(yōu)化潘拨,查找性能瓶頸的方法。
安裝Xhprof擴展
$ wget http://pecl.php.net/get/xhprof-0.9.4.tgz
$ tar -zxvf xhprof-0.9.4.tgz
$ cd xhprof-0.9.4
$ cd extension/
$ phpize
$ ./configure
$ make
$ sudo make install
修改php.ini
[xhprof]
extension=xhprof.so
xhprof.output_dir=/tmp
配置中xhprof.output_dir指定了生成的profile文件存儲的位置饶号,我們將其指定為/tmp铁追。
參數(shù)
void xhprof_enable ([ int $flags = 0 [, array $options ]] )
- flags 該參數(shù)用于為剖析結(jié)果添加額外的信息,該參數(shù)的值使用以下宏茫船,如果需要提供多個值琅束,使用|進行分隔。
XHPROFFLAGSNO_BUILTINS 跳過所有的內(nèi)置函數(shù)
XHPROFFLAGSCPU 添加對CPU使用的分析
XHPROFFLAGSMEMORY 添加對內(nèi)存使用的分析 - options 數(shù)組形式提供可選參數(shù)算谈,在此處提供ignored_functions選項需要忽略的函數(shù)
安裝graphviz工具
sudo apt-get install graphviz
如何使用
假設(shè)你現(xiàn)在要看看看自己做的一個網(wǎng)站的首頁性能數(shù)據(jù)涩禀,那么你要找到這個網(wǎng)站的首頁入口文件,在核心文件加載之前和之后分別加上xhprof的性能測試代碼
#開啟然眼,具體參數(shù)說明可以查看官方文檔
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
#核心文件的執(zhí)行
...
require 'index.php'
...
#關(guān)閉
$xhprof_data = xhprof_disable();
#這里的路徑根據(jù)自己的站點來配置
$XHPROF_ROOT = realpath(dirname(__FILE__) .'/');
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$xhprof_runs->save_run($xhprof_data, "xhprof");
如何查看結(jié)果
- 把 xhprof-0.9.4 的可以通過瀏覽器訪問艾船,訪問里面的xhprof_html/index.php 就會看到id列表,點擊查看對應(yīng)的id查看結(jié)果