• PHP性能分析工具xhprof的安装使用与注意事项


    前言

    xhprof由facebook开源出来的一个PHP性能监控工具,占用资源很少,甚至能够在生产环境中进行部署。

    它可以结合graphviz使用,能够以图片的形式很直观的展示代码执行耗时。

    下面主要讲一下安装和使用过程

    1、安装

    (1)下载和解压

    1
    2
    wget http: //pecl .php.net /get/xhprof-0 .9.4.tgz
    tar  zxvf xhprof-0.9.4.tgz

    (2)编译和运行

    1
    2
    3
    4
    5
    cd  xhprof-0.9.4/extension/
    phpize  // 此语句编译PHP扩展的工具,主要是根据系统信息生成对应的configure文件,一般存放在 /usr/local/php/bin/ 目录下
    . /configure  --with-php-config=/usr/local/php/bin/php-config
    make  && make install
    mkdir  /tmp/xhprof

    (3)编辑php.ini:

    1
    2
    3
    [xhprof]
    extension = xhprof.so
    xhprof.output_dir= /tmp/xhprof

    xhprof.output_dir是分析生成日志的保存路径

    (4)安装插件

    最后返回数组,就表示安装好了。具体哪些值是什么意思先别管,因为下面有UI的配置。会很直观!

    1
    yum -y  install  libjpeg freetype freetype-devel libjpeg-devel liberation-sans-fonts.noarch

    自动安装

    1
    yum -y  install  graphviz

    (5)插入代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // 找到你要分析的代码,在代码开始处添加,start profiling,将会统计内存占用情况
    xhprof_enable(XHPROF_FLAGS_MEMORY);
    // 具体代码
    // 在代码结束位置添加
    $xhprof_data = xhprof_disable();  //  stop profiler, display raw xhprof data for the profiler run
    include_once ( "/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php" );  # 请注意设置站点 include_path 权限
    include_once ( "/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php" );
    $xhprof_runs = new \XHProfRuns_Default();
    //  Save the run under a namespace "xhprof_foo".
    //  **NOTE**:
    //  By default save_run() will automatically generate a unique
    //  run id for you. [You can override that behavior by passing
    //  a run id (optional arg) to the save_run() method instead.]
    $xhprof_runs->save_run($xhprof_data,  "xhprof_foo" );

    (6)查看

    给(2)中的xhprof-0.9.4/xhprof_html 配置一个可以访问的站点,可以简洁的使用php内置的server

    1
    2
    cd  xhprof-0.9.4/xhprof_html
    php -S 0.0.0.0:8990

    然后访问ip+端口就可以报告了。

    2、使用说明

    • Function Name:方法名称。

    • Calls:方法被调用的次数。

    • Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。

    • Incl.Wall Time(microsec):方法执行花费的时间,包括子方法的执行时间。(单位:微秒)

    • IWall%:方法执行花费的时间百分比。

    • Excl. Wall Time(microsec):方法本身执行花费的时间,不包括子方法的执行时间。(单位:微秒)

    • EWall%:方法本身执行花费的时间百分比。

    • Incl. CPU(microsecs):方法执行花费的CPU时间,包括子方法的执行时间。(单位:微秒)

    • ICpu%:方法执行花费的CPU时间百分比。

    • Excl. CPU(microsec):方法本身执行花费的CPU时间,不包括子方法的执行时间。(单位:微秒)

    • ECPU%:方法本身执行花费的CPU时间百分比。

    • Incl.MemUse(bytes):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)

    • IMemUse%:方法执行占用的内存百分比。

    • Excl.MemUse(bytes):方法本身执行占用的内存,不包括子方法执行占用的内存。(单位:字节)

    • EMemUse%:方法本身执行占用的内存百分比。

    • Incl.PeakMemUse(bytes):Incl.MemUse峰值。(单位:字节)

    • IPeakMemUse%:Incl.MemUse峰值百分比。

    • Excl.PeakMemUse(bytes):Excl.MemUse峰值。单位:(字节)

    • EPeakMemUse%:Excl.MemUse峰值百分比。

    注意:

          1、在正式启用前,一定要确认不会影响正常的数据输出。确认输出内容无异后,再上线。

          2、每个url的max_time不要设置的过小。

          3、xhprof会影响线上服务的性能,因此最好只在一台机器上进行监控,或者 修改xhprof.php代码,对请求进行随机监控。

    原文链接:https://segmentfault.com/a/1190000012478668

  • 相关阅读:
    Elasticsearch节点下线(退役)and unassigned shards
    详解Hbase架构原理
    docker安装(yum方式)
    矩形的逆时针蛇形填数
    CentOS7安装iptables防火墙
    Linux:-bash: ***: command not found
    两个大数相乘JAVA版
    SQL Server 2012时出现命名管道提供程序: 无法打开与 SQL Server 的连接
    jstl函数库及自定义函数库
    This Android SDK requires Android Developer Toolkit version 23.0.0 or above
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15454691.html
Copyright © 2020-2023  润新知