介紹
xhprof是一款facebook 開源的函數(shù)級別的非侵入式php性能分析工具羡榴。
它報告函數(shù)級別的請求次數(shù)和各種指標(biāo)兴溜,包括阻塞時間却妨,CPU時間和內(nèi)存使用情況。
相關(guān)鏈接:
http://pecl.php.net/package/xhprof
https://www.php.net/manual/zh/book.xhprof.php
安裝
由于xhprof很久未更新盯质,目前原版本只支持php5袁串。
若要在php7上安裝xhprof,需要使用他人修改的repo呼巷。
放上github倉庫地址:https://github.com/longxinH/xhprof
git clone https://github.com/longxinH/xhprof.git
cd xhprof/extension/
/path/to/php7/bin/phpize #phpize地址
./configure --with-php-config=/path/to/php7/bin/php-config
make && sudo make install
xhprof/extension/modules/xhprof.so #文件地址
重啟php-fpm
找到php.ini文件囱修,添加xhprof擴展
[xhprof]
extension = xhprof.so
xhprof.output_dir = /tmp/xhprof
這里的xhprof文件目錄最好設(shè)置為777權(quán)限,否則運行時會出錯王悍。
配置成功以后破镰,將xhprof文件下的xhprof_html和xhprof_lib復(fù)制到網(wǎng)站根目錄(若已存在請忽略)。
使用方法
將需要檢查性能的代碼包裹起來配名,如下:
<?php
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); #開啟xhprof
#要檢查性能的代碼
dosomethings();
$xhprof_data = xhprof_disable(); #關(guān)閉xhprof
include_once '/webroot/xhprof/xhprof_lib/utils/xhprof_lib.php';
include_once '/webroot/xhprof/xhprof_lib/utils/xhprof_runs.php';
$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, 'your_project');
查看性能分析
在瀏覽器中打開http://localhost/xhprof/xhprof_html/index.php頁面啤咽。
選擇查看的文件,即可查看渠脉。
為了方便我們更好的查看性能分析宇整,可以添加圖形化支持。
安裝graphviz
sudo yum install graphviz # centos
sudo apt-get install graphviz #ubuntu
注意芋膘,此時需要在php.ini中的禁用函數(shù)中移除proc_open函數(shù)鳞青。
然后點擊[View Full Callgraph]進(jìn)入圖形化查看界面霸饲,即可查看。
xhprof字段解釋
Function Name:函數(shù)名
Calls:調(diào)用次數(shù)
Calls%:方法調(diào)用次數(shù)在同級方法總調(diào)用次數(shù)的百分比
Incl.Wall Time(microsec):方法執(zhí)行花費的時間臂拓,包括子方法的執(zhí)行時間(微秒)
IWall%:方法執(zhí)行花費的時間百分比
Excl. Wall Time(microsec):方法本身執(zhí)行花費的時間厚脉,不包括子方法的執(zhí)行時間(微秒)
EWall%:方法本身執(zhí)行花費的時間百分比