PHP效能分析工具XHProf安裝使用教程_php技巧

來源:互聯網
上載者:User

HProf是facebook開源出來的一個php輕量級的效能分析工具,跟Xdebug類似,但效能開銷更低,還可以用在生產環境中,也可以由程式開關來控制是否進行profile。基於瀏覽

器的效能分析使用者介面能更容易查看,或是與同行們分享成果。也能繪製呼叫歷程圖。在資料收集階段,它記錄調用次數的追蹤和包容性的指標弧在動態callgraph的一個程式。

它專屬的資料計算的報告/後處理階段。在資料收集時,XHProfd通過檢測迴圈來處理遞迴的函數調用,並通過給遞迴調用中每個深度的調用一個有用的命名來避開死迴圈。

XHProf的輕量級性質和匯聚功能,使得它非常適合用於收集“生產環境”的效能統計資料的統計。

1. 安裝XHProf

複製代碼 代碼如下:

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib <directory_for_htdocs>
cd extension
phpize
./configure
make
make install

2. 配置 php.ini 檔案

複製代碼 代碼如下:

[xhprof]
extension=xhprof.so
;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
; 記得<directory_for_storing_xhprof_runs>WEB要有寫入許可權
xhprof.output_dir=<directory_for_storing_xhprof_runs>

重啟服務讓修改生效,現在就可以使用XHProf了,不過為了顯示效果更炫,最好繼續安裝Graphviz。

3. 安裝Graphviz

複製代碼 代碼如下:

wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz  
tar zxf graphviz-2.26.3.tar.gz   
cd graphviz-2.26.3  
./configure
make   
make install

安裝完成後,會產生/usr/local/bin/dot檔案,你應該確保路徑在PATH環境變數裡,以便XHProf能找到它。

4. 應用XHProf

複製代碼 代碼如下:

xhprof_enable();//開啟xhprof

/******程式邏輯 Start******/
function test1(){
 sleep(3);
 return;
}
function test2(){
 test1();
}
function test3(){
 test2();
}
function p(){
 echo '<h3>xhprof test</h3>';
}
p();
test3();
/******程式邏輯 End******/

$xhprof_data = xhprof_disable();//關閉xhprof

//儲存xhprof資料
include_once '../xhprof_lib/utils/xhprof_lib.php';
include_once '../xhprof_lib/utils/xhprof_runs.php';

$xhprof_runs = new XHProfRuns_Default();
$xhprof_source = 'xhprof_test';
$run_id = $xhprof_runs->save_run($xhprof_data, $xhprof_source);
$report_url = 'http://xhprof.rebill.info/index.php?run='.$run_id.'&source='.$xhprof_source;
echo '<br>';
echo 'view the performance report:<a href="'.$report_url.'" target="_blank">'.$report_url.'</a>';

如此一來,會在上面設定的xhprof.output_dir目錄裡產生名字類似4c236583ef490.xhprof_test的資料檔案,可以很方便的通過Web方式瀏覽效果:
http://xhprof.rebill.info/index.php?run=4c236583ef490&source=xhprof_test

目前顯示的是表格形式的顯示,點擊頁面上的[View Full Callgraph],就能看到精美的圖片顯示了。

線上測試體驗地址:http://xhprof.rebill.info/test.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.