1. 程式人生 > >使用XHProf分析PHP性能瓶頸

使用XHProf分析PHP性能瓶頸

flags info append put 上下 PE ash 例如 ini

安裝xhprof

wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension/
sudo phpize
./configure
sudo make
sudo make install
cd ../

配置php.ini

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

註:xhprof不支持php7。需要在github上下載 https://github.com/phacility/xhprof.git。

配置xhprof環境

需要把xhprof壓縮包裏的兩個目錄復制到指定目錄(假設定義到 /work/xhprof/

):

mkdir /work/xhprof/
cp -a xhprof_html/ /work/xhprof/
cp -a xhprof_lib/ /work/xhprof/

然後在應用框架的入口文件添加:

xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
register_shutdown_function(function() {
    $xhprof_data = xhprof_disable();
    if (function_exists('fastcgi_finish_request')){
        fastcgi_finish_request();
} include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php"; include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php"; $xhprof_runs = new XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof'); });

xhprof_enablexhprof_disable是成對出現的,一個是代碼運行最前面,一個是最後面。中間是要分析的代碼。

經過上面的配置後,我們後續請求項目的接口,xhprof就會分析請求過程中的CPU、內存、耗時等內容。日誌保存在xhprof.output_dir目錄。

配置web

配置好了,怎麽查看日誌呢?我們可以搭建一個簡單的server:
xhprof.test.com.conf

server {
    listen       80;
    server_name  xhprof.test.com;

    root /work/xhprof/xhprof_html;
    index  index.html index.php;


    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

然後配置虛擬主機xhprof.test.com。重啟nginx,打開 xhprof.test.com就可以看到效果了:

技術分享圖片
技術分享圖片

非侵入式引入xhprof

前面我們是通過在入口文件添加代碼實現了分析的功能。更優雅的方式是新建一個額外的文件 xhprof.inc.php,保存在/work/xhprof/目錄下:

xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
register_shutdown_function(function() {
    $xhprof_data = xhprof_disable();
    if (function_exists('fastcgi_finish_request')){
        fastcgi_finish_request();
    }
    include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";
    include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');
});

利用PHP的自動加載功能,在執行代碼前註入此文件,編輯php.ini:

auto_prepend_file = /work/xhprof/xhprof.inc.php

然後重啟PHP服務。這樣所有使用該php環境的都會生效。

或者寫到指定項目的nginx配置裏也行:

location ~ \.php$ {
        
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param PHP_VALUE "auto_prepend_file=/work/xhprof/xhprof.inc.php";
        include        fastcgi_params;
    }

然後重啟nginx服務。這樣僅該項目生效。

通過 auto_prepend_file 和 auto_append_file包含的文件在此模式下會被解析,但有些限制,例如函數必須在被調用之前定義。

修改采樣頻率

默認情況下,xhprof每次都會運行,線上環境如果這麽設置,會對性能有影響。

xhprof.inc.php

<?php
$profiling = !(mt_rand()%9); 
if($profiling) xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
register_shutdown_function(function() use($profiling) {
    if($profiling){
        $xhprof_data = xhprof_disable();
        if (function_exists('fastcgi_finish_request')){
            fastcgi_finish_request();
        }
        include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";
        include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";
        $xhprof_runs = new XHProfRuns_Default();
        $xhprof_runs->save_run($xhprof_data, 'xhprof'); 
    }
});

參考

1、Xhprof 的配置和使用方法 - 簡書
https://www.jianshu.com/p/38e3ae81970c
2、使用XHProf查找PHP性能瓶頸 - 程序猿成長計劃 - SegmentFault 思否
https://segmentfault.com/a/1190000003509917
3、PHP性能追蹤及分析工具xhprof的安裝與使用 - 馬新才的技術博客 - SegmentFault 思否
https://segmentfault.com/a/1190000007288664
4、Tideways和xhgui打造PHP非侵入式監控平臺 | 我是大熊
http://blog.it2048.cn/article-tideways-xhgui/

使用XHProf分析PHP性能瓶頸