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

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

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

前言

最近一直想作個接口性能分析,可是手打log實在能把人給累死。怎麼辦呢?想到以前有寫過一篇我所知道的PHP相關調優彙總,裏面有一個Xdebug + kcachegrind的調優方式。 可是呢,每次都須要將它產生的cachegrind.out.*文件下到本地,再用kcachegrind打開作分析,並且體驗感也不是特別好(原諒我英語不過三的渣渣...)html

性能分析的UI組合

  • uprofiler 點擊下載
  • xhprof + xhprof.io 【因年久失修,用uprofiler替換便可或用修復板。支持保存分析數據至mysql,函數調用記錄完整,內核級別函數都能顯示,支持域名索引】修復板下載
  • xhprof or uprofiler or tideways + xhgui 【推薦,保存數據至MongoDB,UI界面友好,有中文UI版,不支持域名索引對於線上調試支持較差】中文版下載英文原版下載
  • tideways【推薦,這個最絢的並且一直在持續維護。可是使用它酷炫的UI須要付費,擴展則不須要。】tideways下載地址

安裝

  • 環境mysql

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

    前置需安裝php-dev`sudo apt-get install php5-dev`
    
    ```shell
    $ 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
    ```
  2. 安裝xhguigit

    $ 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 } )
  3. 安裝tidewaysgithub

    見該安裝地址,選擇系統安裝方式`https://tideways.io/profiler/docs/setup/installation`
    • 須要在nginx應用中加入fastcgi_param TIDEWAYS_SAMPLERATE "25";
    • 須要在nginx應用中加入fastcgi_param PHP_VALUE "auto_prepend_file=/home/0x584A/www/xhgui/external/header.php";
  4. ngxin應用配置web

    應用
    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;
        }
    }

    訪問配置好的頁面便可。sql

注意

  • 分析方式請自行更具url設置mongodb

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

    • return rand(1, 100) === 42;1%的採樣率,改爲return True;則標識每次都採樣
  • 分析參數過多則清除mongodb數據

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

終極特效

1.png

2.png

3.png

參考

相關文章
相關標籤/搜索