• 技术文章 >后端开发 >php教程

    使用XHProf分析PHP性能瓶颈的方法一

    不言不言2018-07-07 15:34:53原创513

    安装xhprof扩展

    wget http://pecl.php.net/get/xhprof-0.9.4.tgz
    tar zxf xhprof-0.9.4.tgz
    cd xhprof-0.9.4/extension/
    sudo phpize
    ./configure
    sudo make
    sudo make install
    cd ../

    配置php.ini

    [xhprof]
    extension=xhprof.so
    xhprof.output_dir=/tmp
    注:xhprof已经很久没有更新过了,截至目前还不支持php7,php7可以使用 https://github.com/phacility/...。

    配置xhprof环境

    需要把xhprof压缩包里的两个目录复制到指定目录(假设定义到 /work/xhprof/):

    mkdir /work/xhprof/
    cp -a xhprof_html/ /work/xhprof/
    cp -a xhprof_lib/ /work/xhprof/

    然后在项目框架的入口文件添加:

    xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
    register_shutdown_function(function() {
        $xhprof_data = xhprof_disable();
        if (function_exists('fastcgi_finish_request')){
            fastcgi_finish_request();
        }
        include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";
        include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";
        $xhprof_runs = new XHProfRuns_Default();
        $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');
    });

    代码解析:
    $xhprof_data中记录了程序运行过程中所有的函数调用时间及CPU内存消耗,具体记录哪些指标可以通过xhprof_enable的参数控制,目前支持的参数有:

    之后的处理已经与xhprof扩展无关,大致是编写一个存储类XHProfRuns_Default,将$xhprof_data序列化并保存到某个目录,可以通过XHProfRuns_Default(__DIR__)将结果输出到当前目录,如果不指定则会读取php.ini配置文件中的xhprof.output_dir,仍然没有指定则会输出到/tmp

    xhprof_enablexhprof_disable是成对出现的,一个是代码运行最前面,一个是最后面。中间是要分析的代码。

    经过上面的配置后,我们后续请求项目的接口,xhprof就会分析请求过程中的CPU、内存、耗时等内容。日志保存在xhprof.output_dir目录。

    配置web

    配置好了,怎么查看日志呢?我们可以搭建一个简单的web server:

    xhprof.test.com.conf

    server {
        listen       80;
        server_name  xhprof.test.com;
    
        root /work/xhprof/xhprof_html;
        index  index.html index.php;
    
    
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    然后配置虚拟主机xhprof.test.com。重启nginx,打开 xhprof.test.com就可以看到效果了:


    1599082213-5b27c405e6c95_articlex[1].png

    默认的UI里列出了:

    在web中还可以看到 [View Full Callgraph] 链接,点击后可以绘制出一张可视化的性能分析图,如果点击后报错的话,可能是缺少依赖graphviz。graphviz是一个绘制图形的工具,可以更为直观的让你查看性能的瓶颈。如果需要可以安装:

    yum install -y libpng
    yum install -y graphviz

    效果:
    2522854532-5b27c405e2d35_articlex[1].png

    非侵入式引入xhprof

    前面我们是通过在项目入口文件添加代码实现了分析的功能。更优雅的方式是新建一个额外的文件 xhprof.inc.php,保存在/work/xhprof/目录下:

    xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
    register_shutdown_function(function() {
        $xhprof_data = xhprof_disable();
        if (function_exists('fastcgi_finish_request')){
            fastcgi_finish_request();
        }
        include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";
        include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";
        $xhprof_runs = new XHProfRuns_Default();
        $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');
    });

    利用PHP的自动加载功能,在执行代码前注入此文件,编辑php.ini:

    auto_prepend_file = /work/xhprof/xhprof.inc.php

    然后重启PHP服务。这样所有使用该php环境的都会生效。

    或者写到指定项目的nginx配置里也行:
    jifen.cc.conf

    location ~ \.php$ {
            
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "auto_prepend_file=/work/xhprof/xhprof.inc.php";
            include        fastcgi_params;
        }

    然后重启nginx服务。这样仅该项目生效。

    通过 auto_prepend_file 和 auto_append_file包含的文件在此模式下会被解析,但有些限制,例如函数必须在被调用之前定义。

    修改采样频率

    默认情况下,xhprof每次都会运行,线上环境如果这么设置,会对性能有影响。

    xhprof.inc.php

    <?php
    $profiling = !(mt_rand()%9); 
    if($profiling) xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
    register_shutdown_function(function() use($profiling) {
        if($profiling){
            $xhprof_data = xhprof_disable();
            if (function_exists('fastcgi_finish_request')){
                fastcgi_finish_request();
            }
            include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php";
            include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php";
            $xhprof_runs = new XHProfRuns_Default();
            $xhprof_runs->save_run($xhprof_data, 'xhprof');    
        }
    });

    总结

    本篇文章里,我们介绍了如何基于xhprof扩展来分析PHP性能,并记录到日志里,最后使用xhprof扩展自带的UI在web里展示出来。主要知识点:

    以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

    相关推荐:

    php7+的php-fpm参数配置的注意事项

    解决laravel-admin中select在form编辑时不能自动选中当前的值的问题

    以上就是使用XHProf分析PHP性能瓶颈的方法一的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php xhprof
    上一篇:PHP数据结构基础之递归 下一篇:使用XHProf分析PHP性能瓶颈的方法二
    大前端线上培训班

    相关文章推荐

    • PHP数据库学习之怎样一次执行多条SQL命令?• PHP中获取SQL的查询结果的常用函数整理(实例详解)• PHP中怎样去连接MySQL数据库?• PHP中mysqli_select_db与mysqli_query函数的用法• 你必须了解PHP中什么是抽象类和抽象方法

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网