使用xhprof對php7程序進行性能分析

Xhprof是facebook開源出來的一個php輕量級的性能分析工具,跟Xdebug相似,但性能開銷更低,還能夠用在生產環境中,也能夠由程序開關來控制是否進行profile。php

對於還在使用php5的朋友們,能夠安裝pecl的xhprof擴展html

http://pecl.php.net/package/xhprof

可是由於長時間不更新,針對php7已沒法正常安裝,可使用下的地址git

https://github.com/longxinH/xhprof/releases

  

1、安裝xhprofgithub

下載xhprof源碼php7

wget https://github.com/longxinH/xhprof/archive/v2.1.0.tar.gz

解壓源碼包svg

tar xf v2.1.0.tar.gz

進入目錄函數

cd xhprof-2.1.0/extension

運行phpize,請自行修改大家的phpize路徑工具

/data/nmp/php7/bin/phpize

運行configure,請自行修改大家的php-config路徑gitlab

./configure --with-php-config=/data/nmp/php7/bin/php-config

編譯安裝性能

make && make install

修改php.ini配置,若是extension_dir配置了就不用再配置了,若是沒配置,則把該目錄指向,擴展編譯安裝後顯示的路徑。

extension_dir = "/data/nmp/php7/lib/php/extensions/no-debug-zts-20170718"

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

  

2、配置xhprof

咱們把擴展目錄下的xhprof_html和xhprof_lib複製出來,單獨存放到一個目錄中。

cp -a xhprof_html /data/wwwroot/xhprof
cp -a xhprof_lib /data/wwwroot/xhprof

針對要分析的項目,能夠把以下代碼添加到入口文件中:

//開啓性能分析
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
//php停止時運行的函數
register_shutdown_function(function () {
    //中止性能分析
    $xhprof_data = xhprof_disable();
    if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    }
    //引入xhprof庫文件,路徑請自行修改
    $XHPROF_ROOT = realpath(dirname(__FILE__) . '/xhprof');
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
    $xhprof_runs = new XHProfRuns_Default();
    //導出性能分析數據,默認xhprof.output_dir指定的目錄
    $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');
});

數據默認會導出到xhprof.output_dir指定目錄,因此需建立該目錄,並給予相關權限  

mkdir /tmp/xhprof
chmod -R 777 /tmp/xhprof

這裏咱們寫一個簡單程序進行一下演示  

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'on');

//開啓性能分析
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
//php停止時運行的函數
register_shutdown_function(function () {
    //中止性能分析
    $xhprof_data = xhprof_disable();
    if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    }
    //引入xhprof庫文件,路徑請自行修改
    $XHPROF_ROOT = realpath(dirname(__FILE__) .'/xhprof');
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
    $xhprof_runs = new XHProfRuns_Default();
    //導出性能分析數據,默認xhprof.output_dir指定的目錄
    $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');
});


function a()
{
    echo 'aaa';
    sleep(1);
    b();
}

function b()
{
    echo 'bbb';
    sleep(3);
    c();
}

function c()
{
    echo 'ccc';
    sleep(5);
}

a();

  

3、配置虛擬主機,用來查看分析日誌

server {
    listen       80;
    server_name  xhprof.xxx.com;
    charset utf-8;
 
    root   /data/wwwroot/xhprof/xhprof_html;
    index  index.html index.htm 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.xxx.com來查看分析日誌了。

若是點擊 [View Full Callgraph] 沒法查看,則需裝以下工具:

yum install -y libpng
yum install -y graphviz

若是上面安裝完後,仍是沒法顯示,並報以下錯誤:

failed to execute cmd: " dot -Tpng". stderr: `Format: "png" not recognized. 
Use one of: canon cmap cmapx cmapx_np dot eps fig gv imap imap_np ismap pic plain plain-ext pov ps ps2 svg svgz tk vml vmlz xdot '

咱們須要手動安裝 graphviz

https://graphviz.gitlab.io/_pages/Download/Download_source.html

下載源碼包,並編譯安裝

tar xf graphviz.tar.gz
cd graphviz-2.40.1
./configure
make
make install
相關文章
相關標籤/搜索