• 如何在本地配置php分析工具xhprof


    测试环境:

    linuxMint + nginx1.4.6+mysql5.5+php5.5

    什么是xhprof?

    XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情况。一个函数的开销,可细分成调用者和被调用者的开销,XHProf数据收集阶段,它记录调用次数的追踪和包容性的指标弧在动态callgraph的一个程序。

    一,如何安装?

    1,下载最新的xhprof包

    官网地址:https://pecl.php.net/package/xhprof

    下载下来:

    wget https://pecl.php.net/get/xhprof-0.9.4.tgz

    解压:

    tar -xvf xhprof-0.9.4.tgz

    2,编译安装pecl扩展库

    参考地址:http://php.net/manual/zh/install.pecl.phpize.php

    cd xhprof-0.9.4/extension/
    phpize
    ./configure
    make
    sudo make install

    编译成功后,在扩展库目录中会有一个xhprof.so文件

    扩展库目录地址:/usr/lib/php5/20121212/

    3,修改php配置文件

    由于我的电脑用的是php-cgi,需要添加一个配置。

    cd /etc/php5/cgi/conf.d

    新建:xhprof.ini文件

    extension=xhprof.so

    4,重启php,重启php-cgi

    重启php:

    sudo service php5-fpm restart

    重启php-cgi

    kill -9 17445

    php-cgi -b 127.0.0.1:9000&

    5,复制xhprof到网站根目录

    比如我的根目录为:/data/webroot/

    复制两个目录即可:

    cp -r xhprof_html/ /data/webroot/
    cp -r xhprof_lib/ /data/webroot/

    6,查看本地环境是否正确安装了xhprof

    新建文件:info.php

    <?php
    echo phpinfo();

    如果能在页面中搜索到xhprof,表示已经正确安装了xhprof:

    xhprof
    
    xhprof    0.9.4
    CPU num    4

     二、如何使用?

    xhprof使用比较简单。

    在下载的目录里面有一个文件 examples/sample.php 文件

    <?php                                                                                                                                                                        
     
    function bar($x) {
      if ($x > 0) {
        bar($x - 1);
      }
    }
     
    function foo() {
      for ($idx = 0; $idx < 5; $idx++) {
        bar($idx);
        $x = strlen("abc");
      }
    }
     
    // start profiling
    xhprof_enable();
     
    // run program
    foo();
     
    // stop profiler
    $xhprof_data = xhprof_disable();
     
    // display raw xhprof data for the profiler run
    print_r($xhprof_data);
     
     
    $XHPROF_ROOT = realpath(dirname(__FILE__) .'/..');
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
     
    // save raw data for this profiler run using default
    // implementation of iXHProfRuns.
    $xhprof_runs = new XHProfRuns_Default();
     
    // save the run under a namespace "xhprof_foo"
    $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

    echo "--------------- ".
    "Assuming you have set up the http based UI for ".
    "XHProf at some address, you can view run at ".
    "http://localhost/xhprof/index.php?run=$run_id&source=xhprof_foo ".
    "--------------- ";

     

    在浏览器查看这个文件会显示下面的代码:

    --------------- Assuming you have set up the http based UI for XHProf at some address, you can view run at http://localhost/xhprof/index.php?run=562614ece2e2d&source=xhprof_foo ---------------

    这时访问:http://localhost/xhprof/index.php 会出现几个文件:

    No XHProf runs specified in the URL.
    
    Existing runs:
    
    562614ece2e2d.xhprof_foo.xhprof 2015-10-20 18:18:20
    562614e0b76b2.xhprof_foo.xhprof 2015-10-20 18:18:08
    562614a33f6ec.xhprof_foo.xhprof 2015-10-20 18:17:07
    5626146be4aaf.xhprof_foo.xhprof 2015-10-20 18:16:11
    5626146324cdd.xhprof_foo.xhprof 2015-10-20 18:16:03

    点击进去一个:

    http://localhost/xhprof/index.php?run=562614ece2e2d&source=xhprof_foo

    点击[View Full Callgraph]  可能会报错

    failed to execute cmd: " dot -Tpng". stderr: `sh: 1: dot: not found '

    是因为没有安装图形化工具

    sudo apt-get install graphviz

    安装完后,再打开,就可以看到视图了。

    名词:
    1. Inclusive Time :包括子函数所有执行时间。
    2. Exclusive Time/Self Time:函数执行本身花费的时间,不包括子树执行时间。
    3. Wall Time:花去了的时间或挂钟时间。
    4. CPU Time:用户耗的时间+内核耗的时间
    5.Inclusive CPU:包括子函数一起所占用的CPU
    6.Exclusive CPU:函数自身所占用的CPU

     

  • 相关阅读:
    【BZOJ】【1833】【ZJOI2010】count 数字计数
    bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)
    NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day2题解
    NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解
    51nod 1962 区间计数(单调栈+二分)
    51nod 1486 大大走格子(DP+组合数学)
    bzoj2276: [Poi2011]Temperature(单调队列/堆)
    5028: 小Z的加油店(线段树)
    bzoj2216: [Poi2011]Lightning Conductor(分治决策单调性优化)
    bzoj1057: [ZJOI2007]棋盘制作(悬线法)
  • 原文地址:https://www.cnblogs.com/wangkongming/p/4896000.html
Copyright © 2020-2023  润新知