PHP 7.1安裝xhprof進行性能分析


安裝擴展
該 xhprof擴展版本是從 https://github.com/longxinH/xhprof 獲取的(第三方的一個庫,官方版本不支持php7)php

下載並編譯xhprof擴展
在web的html目錄下操做:
git clone https://github.com/longxinH/xhprofhtml


編譯擴展git

cd xhprof/extension/
phpize
./configure 
make
make install

 

修改php.ini配置github

[xhprof]
extension=xhprof.so;
xhprof.output_dir=/tmp/xhprof

 

其中 xhprof.output_dir 是 xhprof 的輸出目錄,每次執行 xhprof 的 save_run 方法時都會生成一個 run_id.project_name.xhprof 文件。這個目錄在哪裏並不重要。注意此路徑的權限要可讀寫!!不然文件沒法生成成功web


重啓 php-fpm
sudo service php7.1-fpm restartphp7


添加測試代碼php-fpm

<?php
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

// 要檢查性能的代碼

$xhprof_data = xhprof_disable();
include_once '/var/www/html/xhprof/xhprof_lib/utils/xhprof_lib.php';
include_once '/var/www/html/xhprof/xhprof_lib/utils/xhprof_runs.php';
$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, 'your_project');

測試代碼中要引入xhprof_lib.php和xhprof_runs.php兩個文件post

 

查看生成報告
須要訪問:xhprof/xhprof_html/index.php文件查看:
http://localhost/xhprof/xhprof_html/index.php?run=5b35d3dfa8c29&source=your_project
run後的參數爲$run_id,source參數爲your_project配置的名字性能

若是圖表生成錯誤,須要安裝插件:
sudo apt-get install graphviz測試

實際演示代碼

<?php
function test1(){
for($i=0;$i<10;$i++){
echo 'aaa'.$i.'<br>';
}
}

// start profiling
xhprof_enable();

test1();

// 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_test"
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test");
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_test\n".
"---------------\n";

 

參考網站:https://juejin.im/post/5a1d507751882531ba10b0e9

相關文章
相關標籤/搜索