PHP 效能分析工具XHProf使用

來源:互聯網
上載者:User
來自: 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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.