01月31, 2017

PHP性能被动分析工具之xhgui加tideways

众所周知,xhprof 已经不兼容新版 php 了,目前看来,tideways是最好的继任者,如果不仔细看,你可能会以为 tideways 是收费的,实际上它收费的是 UI 服务,扩展则不需要。

安装

环境

tideways + xhgui
php>5.5
mongodb
php5-mcrypt
apt-get install libcurl4-openssl-dev libpcre3-dev

安装mongodb

前置需安装php-dev`sudo 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

安装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 } )

安装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";

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();
    

效果

原文 xhgui

本文链接:https://blog.hijs.cc/post/tideways.html

-- EOF --

Comments