美文网首页程序猿的进阶屋
PHP7 下安装使用 xhprof

PHP7 下安装使用 xhprof

作者: 躁动的石头 | 来源:发表于2017-03-23 19:44 被阅读5363次

    xhprof 的安装

    该 xhprof 版本是从 https://github.com/longxinH/xhprof 获取,如有更好的选择,烦请联系我。

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

    安装 xhprof

    cd xhprof/extension/
    /opt/php-7.0.14/bin/phpize
    ./configure --with-php-config=/opt/php-7.0.14/bin/php-config --enable-xhprof
    make
    make install
    

    出现

    Installing shared extensions:     /opt/php-7.0.14/lib/php/extensions/no-debug-non-zts-20151012/
    

    代表编译成功

    修改 php.ini 文件

    /opt/php-7.0.14/bin/php -i | grep php.ini //命令查找php.ini文件的位置
    

    /etc/php.ini中增加如下配置

    [xhprof]
    extension=xhprof.so
    xhprof.output_dir=/data/www/xhprof/save_output_dir //该目录自由定义即可,用来保存xhprof生成的源文件
    

    保存好之后,重启php-fpm

    kill -USR2 `cat /opt/php-7.0.14/var/run/php-fpm.pid`
    

    将相关文件移动到项目中

    //切换到下载的 xhprof 目录
    cp -r xhprof/xhprof_html  ROOT_PATH/
    cp -r xhprof/xhprof_lib ROOT_PATH/
    

    使用

    xhprof_enable();
    
    //你需要分析的代码
    
    $xhprof_data = xhprof_disable();
    include_once ROOT_PATH.'/xhprof_lib/utils/xhprof_lib.php';
    include_once ROOT_PATH . '/xhprof_lib/utils/xhprof_runs.php';
    
    $xhprof_runs = new XHProfRuns_Default();
    
    $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test");
    //将run_id保存起来或者随代码一起输出
    

    查看数据

    访问$host_url/xhprof_html/index.php?run=58d3b28b521f6&source=xhprof_test来查看结果

    图形化结果

    点击[View Full Callgraph]可以看图形化结果

    报错

    failed to execute cmd:" dot -Tpng". stderr:sh: dot:command not found。

    //解决方案
    yum install graphviz
    

    相关文章

      网友评论

        本文标题:PHP7 下安装使用 xhprof

        本文链接:https://www.haomeiwen.com/subject/wvetottx.html