PHP 7.1安裝xhprof進行性能分析


PHP 7.1安裝xhprof進行性能分析

 


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

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


編譯擴展

cd xhprof/extension/
phpize
./configure 
#如果不行就運行 ./configure --with-php-config=/usr/local/php7/bin/php-config
make make install

 

修改php.ini配置

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

 

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


重啟 php-fpm
sudo service php7.1-fpm restart


添加測試代碼

復制代碼
<?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兩個文件

 

查看生成報告
需要訪問: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM