PHP性能被动分析工具之xhgui加tideways的安装实践

PHP性能被动分析工具之xhgui加tideways的安装实践

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

前言

最近一直想做个接口信能分析,但是手动打log实在能把人给累死。怎么办呢?想到之前有写过一篇我所知道的PHP相关调优汇总,里面有一个Xdebug + kcachegrind的调优方式。 但是呢,每次都需要将它产生的cachegrind.out.*文件下到本地,再用kcachegrind打开做分析,而且体验感也不是特别好(原谅我英语不过三的渣渣...)

性能分析的UI组合

  • uprofiler 点击下载
  • xhprof + xhprof.io 【年久失修用uprofiler替换即可或用修复板,但支持保存分析数据至mysql,函数调用记录完整,内核级别函数都能显示,支持域名索引】修复板下载
  • xhprof or uprofiler or tideways + xhgui 【推荐,保存数据至MongoDB,UI界面友好,有中文UI版,不支持域名索引对于线上调试支持较差】中文版下载,英文原版下载
  • tideways【推荐,这个最绚的而且一直在持续维护。但是使用它酷炫的UI需要付费,扩展则不需要。】tideways下载地址

安装

  • 环境

    • 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

见该安装地址,选择系统安装方式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";
  1. ngxin应用配置

    应用

    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设置

    'profiler.enable' => function() {
        // url 中包含debug=1则百分百捕获
        if(!empty($_GET['debug'])){
            return True;
        }else{
            // 1%采样
            return rand(1, 100) === 42;
        }
    },
    
  • 在xhgui的config/config.default.php中,可设置采样命中次数;

    • return rand(1, 100) === 42;1%的采样率,改成return True;则标识每次都采样
  • 分析参数过多则清除mongodb数据

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

终极特效

1.png
PHP性能被动分析工具之xhgui加tideways的安装实践_第1张图片
2.png
PHP性能被动分析工具之xhgui加tideways的安装实践_第2张图片
3.png

参考

  • 使用xhprof进行线上PHP性能追踪及分析

你可能感兴趣的:(PHP性能被动分析工具之xhgui加tideways的安装实践)