【PHP】PHP7 下安裝並使用 xhprof 效能分析工具
阿新 • • 發佈:2019-01-03
git clone https://github.com/longxinH/xhprof
安裝 xhprof
cd xhprof/extension/
phpize
./configure
make
make install
然後在/etc/php.ini中根據情況加入
extension=xhprof.so
執行
php -m | grep xhprof
可以看見輸出,說明php擴充套件安裝成功,然後重啟Apache或者php-fpm
執行
可以直接執行從github上clone下來的檔案裡面example目錄下的那個例子
輸出如下
Array
(
[main()] => Array
(
[ct] => 1
[wt] => 9
)
)
---------------
Assuming you have set up the http based UI for
XHProf at some address, you can view run at
http://<xhprof-ui-address>/index.php?run=592567308784c&source=xhprof_foo
---------------
然後複製index.php後面的?run=592567308784c&source=xhprof_foo
訪問
xhprof_html/index.php?run=592567308784c&source=xhprof_foo
可看見輸出
點選中間的 View Full Callgraph 即可看見效能分析圖片
報錯
failed to execute cmd:" dot -Tpng". stderr:sh: dot:command not found。
//解決方案
yum install graphviz
隨機應變
比如想測試自己的專案,例如一款框架的效能分析。
複製xhprof_lib/utils/下的兩個檔案
xhprof_lib.php和xhprof_runs.php到入口檔案同級目錄,然後在入口檔案起始位置新增
// start profiling
xhprof_enable();
結束位置新增
// stop profiler
$xhprof_data = xhprof_disable();
// display raw xhprof data for the profiler run
print_r($xhprof_data);
include_once "xhprof_lib.php";
include_once "xhprof_runs.php";
// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();
// save the run under a namespace "xhprof_foo"
$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://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";
即可得到如上所示的那個url,然後再次去訪問
http://***/xhprof_html/index.php?run=*****&source=xhprof_foo
得到如下所示頁面
檢視圖片
圖中紅色的部分為效能比較低,耗時比較長的部分,我們可以根據根據哪些函式被標記為紅色對系統的程式碼進行優化
補充
Function Name:方法名稱。
Calls:方法被呼叫的次數。
Calls%:方法呼叫次數在同級方法總數呼叫次數中所佔的百分比。
Incl.Wall Time(microsec):方法執行花費的時間,包括子方法的執行時間。(單位:微秒)
IWall%:方法執行花費的時間百分比。
Excl. Wall Time(microsec):方法本身執行花費的時間,不包括子方法的執行時間。(單位:微秒)
EWall%:方法本身執行花費的時間百分比。
Incl. CPU(microsecs):方法執行花費的CPU時間,包括子方法的執行時間。(單位:微秒)
ICpu%:方法執行花費的CPU時間百分比。
Excl. CPU(microsec):方法本身執行花費的CPU時間,不包括子方法的執行時間。(單位:微秒)
ECPU%:方法本身執行花費的CPU時間百分比。
Incl.MemUse(bytes):方法執行佔用的記憶體,包括子方法執行佔用的記憶體。(單位:位元組)
IMemUse%:方法執行佔用的記憶體百分比。
Excl.MemUse(bytes):方法本身執行佔用的記憶體,不包括子方法執行佔用的記憶體。(單位:位元組)
EMemUse%:方法本身執行佔用的記憶體百分比。
Incl.PeakMemUse(bytes):Incl.MemUse峰值。(單位:位元組)
IPeakMemUse%:Incl.MemUse峰值百分比。
Excl.PeakMemUse(bytes):Excl.MemUse峰值。單位:(位元組)
EPeakMemUse%:Excl.MemUse峰值百分比。