PHP性能分析工具XHProf安裝使用教程
HProf是facebook開(kāi)源出來(lái)的一個(gè)php輕量級(jí)的性能分析工具,跟Xdebug類似,但性能開(kāi)銷更低,還可以用在生產(chǎn)環(huán)境中,也可以由程序開(kāi)關(guān)來(lái)控制是否進(jìn)行profile。基于瀏覽
器的性能分析用戶界面能更容易查看,或是與同行們分享成果。也能繪制調(diào)用關(guān)系圖。在數(shù)據(jù)收集階段,它記錄調(diào)用次數(shù)的追蹤和包容性的指標(biāo)弧在動(dòng)態(tài)callgraph的一個(gè)程序。
它獨(dú)有的數(shù)據(jù)計(jì)算的報(bào)告/后處理階段。在數(shù)據(jù)收集時(shí),XHProfd通過(guò)檢測(cè)循環(huán)來(lái)處理遞歸的函數(shù)調(diào)用,并通過(guò)給遞歸調(diào)用中每個(gè)深度的調(diào)用一個(gè)有用的命名來(lái)避開(kāi)死循環(huán)。
XHProf的輕量級(jí)性質(zhì)和匯聚功能,使得它非常適合用于收集“生產(chǎn)環(huán)境”的性能統(tǒng)計(jì)數(shù)據(jù)的統(tǒng)計(jì)。
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要有寫入權(quán)限
xhprof.output_dir=<directory_for_storing_xhprof_runs>
重啟服務(wù)讓修改生效,現(xiàn)在就可以使用XHProf了,不過(guò)為了顯示效果更炫,最好繼續(xù)安裝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
安裝完成后,會(huì)生成/usr/local/bin/dot文件,你應(yīng)該確保路徑在PATH環(huán)境變量里,以便XHProf能找到它。
4. 應(yīng)用XHProf
xhprof_enable();//打開(kāi)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();//關(guān)閉xhprof
//保存xhprof數(shù)據(jù)
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>';
如此一來(lái),會(huì)在上面設(shè)定的xhprof.output_dir目錄里生成名字類似4c236583ef490.xhprof_test的數(shù)據(jù)文件,可以很方便的通過(guò)Web方式瀏覽效果:
http://xhprof.rebill.info/index.php?run=4c236583ef490&source=xhprof_test
目前顯示的是表格形式的顯示,點(diǎn)擊頁(yè)面上的[View Full Callgraph],就能看到精美的圖片顯示了。
在線測(cè)試體驗(yàn)地址:http://xhprof.rebill.info/test.php
相關(guān)文章
PHP學(xué)習(xí)記錄之面向?qū)ο螅∣bject-oriented programming,OOP)基礎(chǔ)【類、對(duì)象、繼承等】
這篇文章主要介紹了PHP學(xué)習(xí)記錄之面向?qū)ο螅∣bject-oriented programming,OOP)基礎(chǔ),結(jié)合實(shí)例形式分析了PHP類、對(duì)象、繼承等相關(guān)概念、原理、使用技巧及操作注意事項(xiàng),需要的朋友可以參考下2019-12-12PHP將兩個(gè)關(guān)聯(lián)數(shù)組合并函數(shù)提高函數(shù)效率
在foreach中循環(huán)查詢數(shù)據(jù)代碼量比較少,但是性能比較低,使用下面的字節(jié)寫的函數(shù)可以解決2014-03-03PHP實(shí)現(xiàn)PDF轉(zhuǎn)圖片的方法詳解
這篇文章主要給大家介紹了關(guān)于PHP將PDF轉(zhuǎn)圖片的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-12-12