Skip to main content

用Xdebug和qcachegrind进行性能分析

php本地如何监控分析函数调用次数和耗时,从而优化代码逻辑

xdebug配置 qcachegrind 安装 用qcachegrind 分析xdebug日志

xdebug配置

[xdebug]
; zend_extension="xdebug.so"
; zend_extension="/usr/local/opt/php70-xdebug/xdebug.so"
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host= localhost
xdebug.remote_port = 9001
xdebug.idekey = PHPSTORM

xdebug.trace_format = 0
xdebug.auto_trace = on ;on=开启trace,off=关闭trace
xdebug.auto_profile = on ;打开性能分析
xdebug.collect_params = on
xdebug.collect_return = on
xdebug.profiler_enable = 1 ;1=打开性能分析,0=关闭性能分析
xdebug.profiler_output_dir=/Users/zhoujh/s/logs/xdebug ;性能分析生成的文件目录
xdebug.profiler_output_name=cachegrind.%R
xdebug.trace_output_dir = /Users/zhoujh/s/logs/xdebug
xdebug.trace_output_name = trace.%R
; xdebug.show_exception_trace = On ;开启异常跟踪
xdebug.show_local_vars = 0

; xdebug.dump.GET = *
; xdebug.dump.POST = *
; xdebug.dump.COOKIE = *
; xdebug.dump.SESSION = *
; xdebug.var_display_max_data = 4056
; xdebug.var_display_max_depth = 5

Qcachegrind安装

#1 查看已安装的组件 是否有qcachegrind
brew list
#2 搜索acachegrind
brew search qcachegrind
#3 安装qcachegrind
brew install qcachegrind
#4 启动qcachegrind
qcachegrind

重启php-fpm

#1 强制结束php-fpm
sudo pkill -9 php-fpm
#2 再次启动php-fpm
sudo php-fpm -D

用Qcachegrind分析访问日志

  • 启动qcachegrind

    qcachegrind
  • 打开xdebug文件

    image-20200429141018505

  • 查看调用时间 顺序 和次数

    image-20200429141119907