Tideways & Xhgui 性能监控

本教程基于php7运行环境进行编写

前言

由于版本迭代速度过快导致各种性能问题未能及时解决,遗留下一堆技术债。目前急需解决接口请求超过运行时间0.8s指标的接口,如果人工一句一句代码去排查,不但要耗费大量时间还会增加异常bug的产生可能性。所以Tideways(非侵入式性能分析工具)就从此诞生。

安装教程

1.安装mongodb拓展

pecl install mongodb

2.安装tideways拓展

wget --no-check-certificate https://github.com/tideways/php-xhprof-extension/archive/v4.1.7.tar.gz
tar zxvf v4.1.7.tar.gz
cd php-xhprof-extension-4.1.7
phpize && ./configure && make && make install

3.修改php配置文件,增加拓展配置

#php.ini
# mongodb 拓展加载
extension=mongodb.so
# tideways 拓展加载
extension=tideways.so
# tideways 设置不需要自动加载
tideways.auto_prepend_library=0

4.重启php-fpm

5.下载xhgui可视化工具

git clone https://github.com/yiqiang3344/xhgui-branch.git
cd xhgui-branch && php install

6.配置xhgui配置文件

<?php  //config/config.default.php ?>
<?php
/**
 * Default configuration for Xhgui
 */
return array(
    'debug' => false,
    'mode' => 'development',
    /*
     * support extension: uprofiler, tideways_xhprof, tideways, xhprof
     * default: xhprof
     */
    'extension' => 'tideways',

    // Can be either mongodb or file.
    /*
    'save.handler' => 'file',
    'save.handler.filename' => dirname(__DIR__) . '/cache/' . 'xhgui.data.' . microtime(true) . '_' . substr(md5($url), 0, 6),
    */
    'save.handler' => 'mongodb',

    // Needed for file save handler. Beware of file locking. You can adujst this file path
    // to reduce locking problems (eg uniqid, time ...)
    //'save.handler.filename' => __DIR__.'/../data/xhgui_'.date('Ymd').'.dat',
    //mongodb://root:root@192.168.109.139:27017
    'db.host' => 'mongodb链接uri',
    //db name
    'db.db' => 'xhprof',

    // Allows you to pass additional options like replicaSet to MongoClient.
    // 'username', 'password' and 'db' (where the user is added)
    'db.options' => array(),
    'templates.path' => dirname(__DIR__) . '/src/templates',
    'date.format' => 'Y-m-d H:i:s',
    'detail.count' => 6,
    'page.limit' => 25,

    // Profile 1 in 100 requests.
    // You can return true to profile every request.
    'profiler.enable' => function() {
        return true;//rand(1, 100) === 42;
    },

    'profiler.simple_url' => function($url) {
        return preg_replace('/\=\d+/', '', $url);
    },

    'profiler.filter_path' => array(
      //xhgui运行目录,如不配置则会一起监控性能
      //  '/var/www/xhgui-branch/webroot'
    )

);

7.配置xhgui nginx配置

# php_monitor.conf

server {
    listen 80;
    server_name php_monitor.dev.cn;
    root /var/www/xhgui-branch/webroot;
    index index.php index.html index.htm;


    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

}

8.配置需要监听性能目标网站nginx配置

# 目标nginx配置文件
fastcgi_param PHP_VALUE "auto_prepend_file=/var/www/xhgui-branch/external/header.php";

使用教程

查看历史请求接口

查看某个接口请求性能情况

查看某个接口火焰图(可高效分析出运行慢原因)

添加新评论 取消回复

文章状态:已收录~