结合php xdebug webGrind一图查看调用链路及性能(适合老项目梳理与维护)

背景:

由于近期维护老项目,各种factory类,各种parentparent。各种abstract extends,而且依赖项目外的许多common和中间件等。IDE内无法直接跳转,而且急需缕清方法的调用链路。调研后有了以下组件的组合。
此文不涉及具体安装步骤,核心就是文末的链接地址^_^

涉及基本组件

  • python3
  • gprof2dot.py script
  • php7.*
  • xdebug

    • php的extension,没有安装的话 访问这个 手把手安装
  • graphviz(dot)
  • webgrind

    • 主要用来web图形操作,shell_exec dot和python的,注意clone完之后需要根据自身机器安装的python的位置,去更改config.php

效果

结合php xdebug webGrind一图查看调用链路及性能(适合老项目梳理与维护)_第1张图片

点击上图中的 Show Call Graph,且选择你需要追踪的接口(上图下拉框中列表),即可有以下图
结合php xdebug webGrind一图查看调用链路及性能(适合老项目梳理与维护)_第2张图片

可选组件

openssl (主要是解决 pip3 install gprof2dot的'SSLError'问题,此文章不涉及解决此问题)

如何使用安装

使用时在请求的url里添加参数?XDEBUG_PROFILE,即可生成分析文件, 比如 xx.com?XDEBUG_PROFILE
结合php xdebug webGrind一图查看调用链路及性能(适合老项目梳理与维护)_第3张图片

xdebug配置,涉及主要是profiler相关选项

[xdebug]
zend_extension = /home/r/php7.13/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so
xdebug.idekey = "PHPSTORM"
xdebug.default_enable = On
#xdebug.remote_connect_back = 1
xdebug.remote_port = 9001
xdebug.remote_enable=on
xdebug.remote_host = 10.2.23.200
xdebug.remote_port = 9001
xdebug.remote_handler = "dbgp"
xdebug.mode=profile,trace,debug
xdebug.remote_autostart="On"
xdebug.auto_trace=1
xdebug.trace_output_dir="/tmp/xdebug"
xdebug.profiler_output_dir="/tmp/xdebug"
xdebug.profile_output_name="script.%t-%s"
xdebug.profiler_output_dir = "/home/r/php7/xdebug/profiler"
#xdebug.trace_output_dir = "/home/r/www/xdebug_trace"  
#xdebug.trace_output_dir = "/home/r/www/xdebug_trace"  
#xdebug.trace_output_name= "%R.html"
#xdebug.collect_params=2
#xdebug.collect_return=1
#xdebug.show_mem_delta=1
#xdebug.trace_format=2
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_enable=1
xdebug.profiler_append=0
xdebug.profiler_aggregare=0

webgrind 项目的 nginx 配置

server{
   listen       80;
   server_name  p.webgrind.com;
   root /home/r/www/webgrind/webgrind;
   index index.php;


access_log /home/r/nginx/logs/webgrind.access.log;
error_log /home/r/nginx/logs/webgrind.error.log;

    location / {
        index index.php;
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .*\.(php|php7)?$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
        client_max_body_size 30M;
        client_body_temp_path /data;
    }
}

centos 6*版本

下载阿里云的Centos-6.repo文件
wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

重新加载yum
yum clean all
yum makecache
yum install graphviz

windows版本

windows上相对更简单,可以先通过脚本测试xdebug收集的信息是否能用py脚本生成图

python gprof2dot.py -n 10 -f callgrind .\cacheGrind\cachegrind.out.1657783826.106186 | dot -T png -o my.png

核心文章:

https://github.com/jrfonseca/...
https://github.com/jokkedk/we...
https://github.com/jokkedk/we...

相关下载链接:

http://www.openssl.org/source...
https://www.python.org/ftp/py...

https://raw.githubusercontent...

http://slproweb.com/download/...
https://gitlab.com/api/v4/pro...

其他性能扩展工具

oneAPM
xhprof+xhGui
workerman-statistics
Blackfire
newRelic

你可能感兴趣的:(结合php xdebug webGrind一图查看调用链路及性能(适合老项目梳理与维护))