• 技术文章 >后端开发 >PHP7

    PHP7下如何安装并使用xhprof性能分析工具

    醉折花枝作酒筹醉折花枝作酒筹2021-07-28 15:05:00转载381
    本篇文章给大家介绍一下PHP7下安装并使用xhprof性能分析工具的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

    该 xhprof 版本是从 https://github.com/longxinH/xhprof 获取

    git clone https://github.com/longxinH/xhprof

    安装 xhprof

    cd xhprof/extension/
    phpize
    ./configure 
    make
    make install

    然后在/etc/php.ini中根据情况加入

    extension=xhprof.so

    执行

    php -m | grep xhprof

    可以看见输出,说明php扩展安装成功,然后重启Apache或者php-fpm

    运行

    可以直接运行从github上clone下来的文件里面example目录下的那个例子

    输出如下

    Array
    (
        [main()] => Array
            (
                [ct] => 1
                [wt] => 9
            )
    
    )
    ---------------
    Assuming you have set up the http based UI for 
    XHProf at some address, you can view run at 
    http://<xhprof-ui-address>/index.php?run=592567308784c&source=xhprof_foo
    ---------------

    然后复制index.php后面的?run=592567308784c&source=xhprof_foo

    访问

    xhprof_html/index.php?run=592567308784c&source=xhprof_foo

    可看见输出

    图示

    点击中间的 View Full Callgraph 即可看见性能分析图片

    报错

    failed to execute cmd:" dot -Tpng". stderr:sh: dot:command not found。
    //解决方案
    yum install graphviz

    随机应变

    比如想测试自己的项目,例如一款框架的性能分析。

    复制xhprof_lib/utils/下的两个文件

    xhprof_lib.php和xhprof_runs.php到入口文件同级目录,然后在入口文件起始位置添加

    // start profiling
    xhprof_enable();

    结束位置添加

    // stop profiler
    $xhprof_data = xhprof_disable();
    
    // display raw xhprof data for the profiler run
    print_r($xhprof_data);
    
    
    include_once "xhprof_lib.php";
    include_once "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 "---------------\n".
         "Assuming you have set up the http based UI for \n".
         "XHProf at some address, you can view run at \n".
         "http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
         "---------------\n";

    即可得到如上所示的那个url,然后再次去访问

    http://***/xhprof_html/index.php?run=*****&source=xhprof_foo

    得到如下所示页面

    图示

    查看图片

    图示

    图中红色的部分为性能比较低,耗时比较长的部分,我们可以根据根据哪些函数被标记为红色对系统的代码进行优化

    补充

    推荐学习:php视频教程

    以上就是PHP7下如何安装并使用xhprof性能分析工具的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:csdn,如有侵犯,请联系admin@php.cn删除
    专题推荐:php xhprof安装
    上一篇:PHP7如何能够连接数据库 下一篇:php7+中如何使用openssl替代mcrypt进行AES加密解密
    大前端线上培训班

    相关文章推荐

    • yum如何安装php7• php7连接MySQL如何制作简易查询程序• php7如何实现垃圾回收机制• Centos7下php7如何安装zip扩展• PHP7如何能够连接数据库

    全部评论我要评论

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

    PHP中文网