xhprof安装,配置,使用

第一阶段:生成profile文件

安装

  • pecl install channel://pecl.php.net/xhprof-0.9.2 (目前是Beta版),
  • 如果不能直接install,则可以 pecl download下来后,phpize && ./configure && make && make install
  • 编辑php.ini 添加如下
    extension=xhprof.so
    ;Path to store xhprof file,
    ;需要定期清理
    ;注意创建路径和权限
    xhprof.output_dir="/logs/xhprof"

在生产环境中启用

  • /home/xLight/php/lib/prepend.php:
    <?php
    //* 启动xhprof,万分之一概率
    if(mt_rand(110000)==1 && function_exists('xhprof_enable') )
    {
            xhprof_enable(XHPROF_FLAGS_MEMORY);//XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY  ,官方不建议在生产环境开启XHPROF_FLAGS_CPU
            define('XHPROF_ON',true);//标记
            //定义输出profile文件的规则
            define('XHPROF_TIMELIMIT'10);//记录执行超过这个时间的。单位:秒
            define('XHPROF_MEMLIMIT'50);//记录内存使用超过这个量的。单位:M
            //define('XHPROF_DIR', '/'.date('Ymd_H') );  //开启本行则启用自己的按时间拆分路径逻辑
            register_shutdown_function('xhprof_save'); //die强行终止的程序也会有记录
    }
    //*/
  • /home/xLight/php/lib/append.php:
    function xhprof_save(){
    //* 结束 xhprof并保存
    if(!XHPROF_ON) return;
    // stop profiler
    $xhprof_data = xhprof_disable();
    //记录执行时间大于t秒 或内存占用大于m兆
    if($xhprof_data['main()']['wt'] < XHPROF_TIMELIMIT *1000000 && $xhprof_data['main()']['mu'] < XHPROF_MEMLIMIT *1000000return;
    //parameter "source" for UI
    $source = $_SERVER['HTTP_HOST'].'__';//add host
    $source .= $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_FILENAME']; //add script
    $source = str_replace(array('/','.','&','?','='),'_',$source);
    //parameter "run" for UI
    $run = str_pad((int)($xhprof_data['main()']['wt']/1000000), 4"0", STR_PAD_LEFT);// 程序执行秒数,扩展成4位,前面补0
    $run .= '_'.str_pad((int)($xhprof_data['main()']['mu']/1000000), 4"0", STR_PAD_LEFT);// 程序占用内存,扩展成4位,前面补0
    // dir for save file
    $dir = $XHPROF_DIR ? $XHPROF_DIR : '';
    $FullDir = ini_get('xhprof.output_dir').'/'.$dir;
    if($dir && !file_exists($FullDir))
      @mkdir( $FullDir , 0777 ,true);
    $FullPath = $FullDir.'/'.$run.'.'.$source ;
    file_put_contents($FullPath , serialize($xhprof_data));
    }//*/
  • 编辑php.ini,添加如下内容
    auto_prepend_file = "/home/xLight/php/lib/prepend.php"

第二阶段:分析profile文件

配置UI界面

  • 只要让通过web访问xhprof-0.9.2/xhprof_html/index.php即可。
  • 实际上xhprof产出的就是个php数组,序列化之后保存到磁盘
  • xhprof UI就是读取这个序列化后的数组并分析。

使用UI分析profile文件

  • 正在coding的列表程序:
  • 首先要获得profile对应的source和run两个参数
  • source是对应程序调用
  • run对应该程序某一次调用,目前该数值为程序执行的秒数与占用内存的结合。比如:”0023_0015″ 代表执行23秒,占用内存15M
0002_0003.test_xlight_me___Goods06_BJBetIndex_php?GoodsType=Score_GoodsNo=090822
0002_0003.test_xlight_me___Goods06_BJBetIndex_php?GoodsType=TotalGoals
0002_0003.test_xlight_me___Goods06_BJBetIndex_php?GoodsType=TotalGoals_GoodsNo=090822
0002_0003.test_xlight_me___Goods06_BJBetIndex_php?GoodsType=WDL
0002_0015.___home_Html_Shell_Buy_AutoDispenseProject_php
0002_0015.___home_Html_Shell_Buy_DeductProjectMoney_php
0002_0015.liyq_xlight_me___Goods06_BJBetIndex_php?GoodsType=Score
0002_0015.liyq_xlight_me___Goods06_BJBetIndex_php?GoodsType=WDL
0003_0000.test_xlight_me___Remoting_gateway_php
0003_0015.___home_Html_Shell_Buy_ComputeAssure_php
0003_0015.liyq_xlight_me___Goods06_BJBetIndex_php?GoodsType=TotalGoals
0004_0000.test_xlight_me___Remoting_gateway_php
0004_0009.hejm_xlight_me___User_UserCenter_php
0004_0015.___home_Html_Shell_Buy_ComputeAssure_php
0005_0000.test_xlight_me___Remoting_gateway_php
0005_0003.test_xlight_me___
0005_0008.hejm_xlight_me___Goods06_P33D_P3New_php?GoodsType=P3
0005_0015.___home_Html_Shell_Buy_ComputeAssure_php
0005_0015.___home_Html_Shell_Buy_DeductProjectMoney_php
0006_0000.test_xlight_me___Remoting_gateway_php
0006_0010.hejm_xlight_me___
0006_0015.___home_Html_Shell_Buy_ComputeAssure_php
0006_0015.___home_Html_Shell_Buy_DeductProjectMoney_php
0007_0000.test_xlight_me___Remoting_gateway_php
0007_0010.hejm_xlight_me___
0007_0015.___home_Html_Shell_Buy_ComputeAssure_php
0008_0000.test_xlight_me___Remoting_gateway_php
0009_0000.test_xlight_me___Remoting_gateway_php
0009_0008.hejm_xlight_me___Goods06_SSQ7LC_
0010_0000.test_xlight_me___Remoting_gateway_php
0010_0001.test_xlight_me___Remoting_gateway_php
0011_0000.test_xlight_me___Remoting_gateway_php
0011_0004.liqh_xlight_me___
0011_0015.___home_Html_Shell_Buy_DeductProjectMoney_php
0013_0001.test_xlight_me___Remoting_gateway_php
0014_0001.test_xlight_me___Remoting_gateway_php
0016_0001.test_xlight_me___Remoting_gateway_php
0016_0018.huxl_xlight_me___
0017_0000.test_xlight_me___Remoting_gateway_php
0019_0000.test_xlight_me___Remoting_gateway_php
0024_0001.test_xlight_me___Remoting_gateway_php
0027_0001.test_xlight_me___Remoting_gateway_php
0029_0001.test_xlight_me___Remoting_gateway_php