PHP性能被動分析工具之xhgui加tideways的安裝實踐

PHP性能被動分析工具之xhgui加tideways的安裝實踐

By:0x584A Date:2016-11-23 17:55:42

前言

最近一直想做個接口信能分析,但是手動打log實在能把人給累死。怎么辦呢?想到之前有寫過一篇我所知道的PHP相關(guān)調(diào)優(yōu)匯總,里面有一個Xdebug + kcachegrind的調(diào)優(yōu)方式。 但是呢,每次都需要將它產(chǎn)生的cachegrind.out.*文件下到本地,再用kcachegrind打開做分析,而且體驗感也不是特別好(原諒我英語不過三的渣渣...)

性能分析的UI組合

  • uprofiler 點擊下載
  • xhprof + xhprof.io 【年久失修用uprofiler替換即可或用修復(fù)板,但支持保存分析數(shù)據(jù)至mysql,函數(shù)調(diào)用記錄完整,內(nèi)核級別函數(shù)都能顯示,支持域名索引】修復(fù)板下載
  • xhprof or uprofiler or tideways + xhgui 【推薦,保存數(shù)據(jù)至MongoDB,UI界面友好,有中文UI版,不支持域名索引對于線上調(diào)試支持較差】中文版下載,英文原版下載
  • tideways【推薦,這個最絢的而且一直在持續(xù)維護(hù)。但是使用它酷炫的UI需要付費,擴(kuò)展則不需要?!?a target="_blank" rel="nofollow">tideways下載地址

安裝

  • 環(huán)境

    • tideways + xhgui
    • php>5.5
    • mongodb
    • php5-mcrypt
    • apt-get install libcurl4-openssl-dev libpcre3-dev
  1. 安裝mongodb

前置需安裝php-devsudo apt-get install php5-dev

$ sudo pecl install mongodb
 $ cd /etc/php5/mods-available
 $ sudo sh -c "echo 'extension=mongodb.so' > /etc/php5/mods-available/mongodb.ini"
 [sudo] 0x584A 的密碼:
 $ cd ../fpm/conf.d
 $ sudo ln -s ../../mods-available/mongodb.ini 20-mongodb.ini
 $ sudo service php5-fpm restart
 $ sudo apt-get install mongodb -y
  1. 安裝xhgui
$ git clone https://github.com/maxincai/xhgui.git
 $ cd xhgui
 $ php install.php

加索引

$ mongo
 > use xhprof
 > db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )
 > db.results.ensureIndex( { 'profile.main().wt' : -1 } )
 > db.results.ensureIndex( { 'profile.main().mu' : -1 } )
 > db.results.ensureIndex( { 'profile.main().cpu' : -1 } )
 > db.results.ensureIndex( { 'meta.url' : 1 } )
  1. 安裝tideways

見該安裝地址,選擇系統(tǒng)安裝方式https://tideways.io/profiler/docs/setup/installation

  • 需要在nginx應(yīng)用中加入fastcgi_param TIDEWAYS_SAMPLERATE "25";
  • 需要在nginx應(yīng)用中加入fastcgi_param PHP_VALUE "auto_prepend_file=/home/0x584A/www/xhgui/external/header.php";
  1. ngxin應(yīng)用配置

    應(yīng)用

    server {
        listen 127.0.10.1:80;
        server_name  app.com;
        root   /home/0x584A/www/app;
        index  index.html index.htm index.php;
    
        location / {
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?$1 last ;
                break;
            }
        }
    
        location ~ ^(.+\.php)(.*)$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_split_path_info         ^(.+\.php)(.*)$;
            fastcgi_param TIDEWAYS_SAMPLERATE "25";
             fastcgi_param PHP_VALUE "auto_prepend_file=/home/0x584A/www/xhgui/external/header.php";
            fastcgi_param       PATH_INFO                $fastcgi_path_info;
            fastcgi_param       PATH_TRANSLATED        $DOCUMENT_ROOT$fastcgi_path_info;
            fastcgi_param       SCRIPT_FILENAME  $DOCUMENT_ROOT/$fastcgi_script_name;
            include             fastcgi_params;
        }
    }
    

    xhgui

    server {
            listen 127.0.10.2:80;
            server_name  debug.com;
            root   /home/0x584A/www/xhgui/webroot;
            index  index.html index.htm index.php;
    
        location / {
            try_files $uri $uri/ /index.php?$uri&$args;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
    
    

    訪問配置好的頁面即可。

注意

  • 分析方式請自行更具url設(shè)置

    'profiler.enable' => function() {
        // url 中包含debug=1則百分百捕獲
        if(!empty($_GET['debug'])){
            return True;
        }else{
            // 1%采樣
            return rand(1, 100) === 42;
        }
    },
    
  • 在xhgui的config/config.default.php中,可設(shè)置采樣命中次數(shù);

    • return rand(1, 100) === 42;1%的采樣率,改成return True;則標(biāo)識每次都采樣
  • 分析參數(shù)過多則清除mongodb數(shù)據(jù)

    $ mongo
      $ use xhprof;
      $ db.dropDatabase();
    

終極特效

1.png
2.png
3.png

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容