zoukankan      html  css  js  c++  java
  • xphrof性能分析线上部署实践

    说明

    将xhprof部署在线上环境,在特定情况下进行性能分析,方便快捷的排查线上性能问题。

    通过参数指定及添加代码行触发进入性能分析,并将结果保存入MongoDB。

    因为xhprof对性能的影响,只部署在一台机子上。

    环境配置

    PHP 5.5.25
    xhprof-0.9.4

    xhprof:git@github.com:phacility/xhprof.git

    核心入口文件

    /PATH/xhprof/config/inject.php

    
    <?php
    
    //根据条件是否进入性能分析
    if( !function_exists('xphrof_checkEnable') ){
        //返回值,ture-进入, false-不进入
        function xphrof_checkEnable($cmdName=''){
            //如果是脚本,直接进
            if( !empty($cmdName) ){
                return true;
            }
    
            //方法一:根据参数进
            $enable = !empty($_GET['showyourbug']) || !empty($_POST['showyourbug']);
            //方法二:根据百分比或随机
            //$enable = rand(0, 100) === 42;
    
            //方法三:使用唯一性数据(iphostop_user)
    
            return $enable;
        }
    }
    
    //获取标识请求的唯一性数据
    if( !function_exists('xphrof_getUniqSign') ){
    
        function xphrof_getUniqSign($cmdName=''){
            //请求的标识-ip
            $clientIp = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : php_uname('n');
    
            //请求的标识-路由
            $path = 'unknow';
            if( isset($_SERVER["REQUEST_URI"]) ){
                $purl = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
                $path = 'http-' . (!empty($purl) ? str_replace('/','-', trim($purl, ' /')) : 'unknow');
            }else{
                $path = 'cmd-' . ( !empty($cmdName) ? str_replace('\','-', trim($cmdName, ' \')): 'unknow');
            }
    
            //尝试从cookie中取用户名
    
            //执行分析的服务器
            $hostName = php_uname('n');
    
            return [
                'ip' => $clientIp,
                'path' => $path,
                'hostname' => $hostName
            ];
        }
    }
    
    
    //主处理过程
    if( !function_exists('xphrof_mainProcess') ){
    
        function xphrof_mainProcess($cmdName=''){
    
            if ( extension_loaded('xhprof') ) {
    
                //xhprof功能统一开关,紧急情况下使用
                $xhprofMainSwitch = true; //true-开, false-关
    
                //判断是否进入分析
                $enable = xphrof_checkEnable($cmdName);
    
                if ( $xhprofMainSwitch && $enable ) {
    
                    $pathData = xphrof_getUniqSign($cmdName);
    
                    //开始性能分析
                    // XHPROF_FLAGS_NO_BUILTINS - 使得跳过所有内置(内部)函数
                    // XHPROF_FLAGS_CPU - 使输出的性能数据中添加 CPU 数据
                    // XHPROF_FLAGS_MEMORY - 使输出的性能数据中添加内存数据
                    xhprof_enable( XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU );
    
                    //注册,脚本执行完或exit后执行
                    register_shutdown_function(function() use ($pathData){
    
                        //xhprof的数据
                        $xhprofData = xhprof_disable();
    
                        //相应给客户端并结束请求
                        $returnClient = -1;
                        if (function_exists('fastcgi_finish_request')) {
                            $returnClient = fastcgi_finish_request();
                        }
    
                        //当前环境数据及post、get数据
                        $envData = [
                            'env' => $_SERVER,
                            'get' => $_GET,
                            'post' => $_POST,
                            'return_client' => intval($returnClient)
                        ];
    
                        $XHPROF_ROOT = realpath(dirname(__FILE__) .'/..');
                        include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
                        include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs_mongo.php";
                        include_once $XHPROF_ROOT . "/xhprof_lib/service/MongoBaseSvc.php";
                        //加载配置文件
                        $xhprofConfig = include_once $XHPROF_ROOT . '/config/config.php';
    
                        $xhprofRuns = new XHProfRuns_Mongo($xhprofConfig);
                        
                        $xhprofRuns->save_run($xhprofData, $envData, $pathData);
    
                    });
                }
            }
        }
    }
    
    //默认启动一次-http,指定服务器才可执行
    if( function_exists('php_uname') && in_array(php_uname('n'), [
        '10.10.10.10', //测试开发服务器
        '11.11.11.11' //线上执行服务器
        ], true) ){
        xphrof_mainProcess();
    }
    
    
    //comand进入分析
    if( !function_exists('xphrof_startProcess') ){
        function xphrof_startProcess($cmdName=''){
            xphrof_mainProcess($cmdName);
        }
    }
    
    

    加载入口文件方案

    1. 在项目启动的配置文件中加载

    
    //例如:bootstrap/autoload.php
    /*
    |--------------------------------------------------------------------------
    | add xhprof config
    |--------------------------------------------------------------------------
    |
    | inject xhprof config.
    |
    */
    $xhprofFile = __DIR__.'/../xhprof/config/inject.php';
    if (file_exists($xhprofFile)) {
        require_once $xhprofFile;
    }
    

    2. 在项目nginx的配置中加载

    
    //在 nginx 的fastcgi配置处
    set $xhprofconf "auto_prepend_file=/PATH/xhprof/config/inject.php";
    fastcgi_param PHP_VALUE $xhprofconf;
    

    触发进入

    1. 对HTTP请求访问时增加参数:showyourbug=1
    2. 对CLI脚本代码中增加:xphrof_startProcess(__CLASS__);

    MongoDB保存结构

    解析保存xhprof_disable()返回的数据、请求的环境变量及数据。可设置过期索引保存指定时长日志。

    
    {
        "ip" : "10.10.10.10", //访问客户端ip
        "path" : "http-", //资源标识,http-接口请求,cmd-artisan脚本执行
        "hostname" : "machine_host_name", //执行机器名称
        "run_id" : "5afe795c96a83", //唯一run_id
        "main" : { //整体数据
            "ct" : NumberLong(1),
            "wt" : NumberLong(36),
            "cpu" : NumberLong(0),
            "mu" : NumberLong(1856),
            "pmu" : NumberLong(672)
        },
        "xhprof_raw_data" : "a:2:{s:23:"main()==>xhprof_disable";a:5:{s:2:"ct";i:1;s:2:"wt";i:8;s:3:"cpu";i:0;s:2:"mu";i:840;s:3:"pmu";i:96;}s:6:"main()";a:5:{s:2:"ct";i:1;s:2:"wt";i:36;s:3:"cpu";i:0;s:2:"mu";i:1856;s:3:"pmu";i:672;}}", //原始数据体
        "env_data" : { //相关环境日志
            "env" : { //$_SERVER信息
                "USER" : "nobody",
                "HOME" : "/",
                "FCGI_ROLE" : "RESPONDER",
                "SCRIPT_FILENAME" : "/PATH/public/index.php",
                ......
                "REQUEST_TIME" : NumberLong(1526626652)
            },
            "get" : { //$_GET信息
                "showyourbug" : "1"
            },
            "post" : [], //$_POST信息
            "return_client" : NumberLong(-1) //fastcgi_finish_request相应客户端结果
        },
        "index_time" : ISODate("2018-05-18T06:57:32.000Z"), //过期索引,保留2天
        "create_time" : "2018-05-18 14:57:32" //创建时间
    }
    

    对线上性能的影响分析

    测试接口

    
    public function getHello(){
        $ret = [
            'errno' => 0,
            'errmsg'    => 'success'
        ];
        return json_encode($ret);
    }
    

    使用ab请求

    
    ab -n 1000 -c 100 -X 10.10.10.10:80 http://hostname/hello
    ab -n 1000 -c 100 -X 10.10.10.10:80 http://hostname/hello?showyourbug=1
    

    测试结果
    批量请求对服务器影响:
    | 测试类别 | Requests per second (#/sec) | 平均 | Time per request (ms) | 平均 |
    | 仅测试接口 | 49.23,50.02,49.41 | 49.55 | 20.312,19.992,20.239 | 20.181 |
    | 加载配置(未进入) | 46.74,45.48,45.21 | 45.81 (-8.76%) | 21.397,21.987,22.118 | 21.834 (-8.19%) |
    | 加载配置(进入) | 10.72,10.48,11.46 | 10.88 (-78%) | 93.281,95.452,87.259 | 91.99 (-355.82%) |

    对单个请求影响:
    | 测试类别 | Time per request (ms) | 平均 |
    | 仅测试接口 | 66.972,87.029,72.605 | 75.54 |
    | 加载配置(未进入) | 79.183,80.728,83.101 | 81.004 (-7.23%) |
    | 加载配置(进入) | 275.186,287.293,291.312 | 284.597 (-271.45%) |

    结论

    xhprof会影响线上性能,所以只部署到一台线上机进行监控。

    原文地址:https://segmentfault.com/a/1190000015591262

  • 相关阅读:
    Win7 SP1 安装SQL Server 2012时提示“此计算机上的操作系统不符合 SQL Server 2012的最低要求”
    ajax jsonp跨域
    Caused by: Unable to locate parent package [json-package] for [class com.you.action.ColumnAction]
    PHP MVC自己主动RBAC自己主动生成的访问路由
    Service与Activity与交流AIDL
    SVN常见错误两项纪录
    EL表达式语言
    oracle11g ASM(修复损坏的磁盘组头asm修复2)
    如何使用iOS 8 指纹识别,代码、示例
    EXCEL Pivot table manipulate
  • 原文地址:https://www.cnblogs.com/lalalagq/p/9980095.html
Copyright © 2011-2022 走看看