PHP 7.1安裝xhprof進行效能分析
阿新 • • 發佈:2020-10-14
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";