PHP 性能分析工具XHProf使用

yu120623 8年前發布 | 9K 次閱讀 PHP開發

來自: http://blog.csdn.net//clh604/article/details/21934453


當php程序邏輯執行很復雜的時候,可能會帶來性能上的問題,為了有效的找到影響性能的代碼,推薦大家使用PHP新能分析工具XHProf,該工具能有效的分析每段代碼的執行情況,非常好用

1、安裝配置PHP的擴展XHProf

$ wget https://github.com/非死book/xhprof/tarball/master -O xhprof.tar.gz
$ tar zxf xhprof.tar.gz
$ cd 非死book-xhprof-b8c76ac/extension/
# phpize
# ./configure --with-php-config=`/path/to/php-config`
# make && make install
# make test


# vi /etc/php.d/xhprof.ini
; 內容為:
extension = xhprof.so
; 注意:output_dir 必須存在且可寫
xhprof.output_dir = /tmp/xhpro


然后重啟apache服務
# service php-fpm restart 或 service httpd restart 

2、使用XHProf

// start profiling
xhprof_enable();

// run program
....

// stop profiler
$xhprof_data = xhprof_disable();

//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
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();

// 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");

echo "---------------\n".
"Assuming you have set up the http based UI for \n".
"XHProf at some address, you can view run at \n".
"http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";

如此一來,會在上面設定的xhprof.output_dir目錄里生成名字類似49bafaa3a3f66.xhprof_foo的數據文件,可以很方便的通過Web方式瀏覽效果:

3、展示輸出結果

在 xhprof 源碼包中提供了xhprof_html 和 xhprof_lib 兩個文件夾,xhprof_lib是用于 PHP 開發,而xhprof_html用于顯示 xhprof 分析結果的 web 界面,訪問形式如下

http://<xhprof-ui-address>/index.php?run=49bafaa3a3f66&source=xhprof_foo


</div>

 本文由用戶 yu120623 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!