來自: http://blog.csdn.net//clh604/article/details/21934453
當php程式邏輯執行很複雜的時候,可能會帶來效能上的問題,為了有效找到影響效能的代碼,推薦大家使用PHP新能分析工具XHProf,該工具能有效分析每段代碼的執行情況,非常好用
1、安裝配置PHP的擴充XHProf
$ wget https://github.com/facebook/xhprof/tarball/master -O xhprof.tar.gz$ tar zxf xhprof.tar.gz$ cd facebook-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 profilingxhprof_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:///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:///index.php?run=49bafaa3a3f66&source=xhprof_foo