一. 下载xdebug扩展
http://xdebug.org/download.php
二. php.ini配置
;基本调试配置
xdebug.auto_trace = on
xdebug.collect_params = on
xdebug.collect_return = on
xdebug.profiler_enable = on
xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug"
xdebug.trace_output_dir="D:\phpStudy\tmp\xdebug"
;远程调试设置
xdebug.remote_enable = onxdebug.remote_host = localhost
xdebug.remote_port = 9000
xdebug.remote_autostart = on
xdebug.default_enable=on
xdebug.remote_autostart = on
xdebug.remote_connect_back = On //如果开启此,将忽略下面的 xdebug.remote_host 的参数。 <一台 webserver有多个开发者的工作目录的时候使用,如:p1.xx.com,p2.xx.com,p3.xx.com 。。。等。 >
xdebug.remote_host = 192.168.59.104 //注意这里是,客户端的ip<即IDE的机器的ip,不是你的web server>
xdebug.remote_port = 9900 // 注意这里是,客户端的端口<即IDE的机器的ip,不是你的web server>
配置说明如下:
;显示默认的错误信息
xdebug.auto_trace=on
;自动打开“监测函数调用过程”的功模。该功能可以在你指定的目录中将函数调用的监测信息以文件的形式输出。此配置 项的默认值为off。
xdebug.collect_params=on
;打开收集“函数参数”的功能。将函数调用的参数值列入函数过程调用的监测信息中。此配置项的默认值为off。
xdebug.collect_return=on
;打开收集“函数返回值”的功能。将函数的返回值列入函数过程调用的监测信息中。此配置项的默认值为off。
xdebug.max_nesting_level=100
xdebug.profiler_enable=on
;打开效能监测器。
xdebug.remote_enable=on
;是否调试
xdebug.remote_host=localhost
xdebug.remote_port=9000
;调试端口
xdebug.remote_handler=dbgp
;选择协议
xdebug.trace_output_dir="d:\Temp"
;设定函数调用监测信息的输出文件的路径。
xdebug.profiler_output_dir="d:\Temp"
;设定效能监测信息输出文件的路径。
xdebug.auto_trace = 1
;是否允许Xdebug跟踪函数调用,跟踪信息以文件形式存储,默认值为0
collect_params = 1
;是否允许Xdebug跟踪函数参数,默认值为0
xdebug.collect_return = 1
;是否允许Xdebug跟踪函数返回值,默认值为0
xdebug.profiler_enable = 1
;打开xdebug的性能分析器,以文件形式存储,这项配置是不能以ini_set()函数配置的,默认值为0
xdebug.profiler_output_dir
;性能分析文件的存放位置,默认值为/tmp
xdebug.profiler_output_name
;性能分析文件的命名规则,默认值为cachegrind.out.%p
xdebug.trace_output_dir
;函数调用跟踪信息输出文件目录,默认值为/tmp
xdebug.trace_output_name
;函数调用跟踪信息输出文件命名规则,默认为trace.%c
三. phpstorm配置
1. 配置php执行php.exe
2. 9000端口与php.ini中xdebug.remote_port=9000一致
3.
四.xdebug调试
五. 性能分析
1. 脚本执行时间
php自带:microtime
<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
// Sleep for a while
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
xdebug: xdebug_time_index()
2. 脚本运行占用内存
php自带:memory_get_usage(), PHP编译时使用了-enable-memory-limit参数时才有效
xdebug: xdebug_memory_usage()
3. 检测代码不足,性能分析
a. php.ini 中加入配置如下
[Xdebug]
xdebug.profiler_enable=on
xdebug.trace_output_dir="I:\Projects\xdebug"
xdebug.profiler_output_dir="I:\Projects\xdebug"
b. 执行情况的分析文件写入到”../Projects/xdebug”目录中
c. windows 安装wincachegrind https://sourceforge.net/projects/wincachegrind/?source=typ_redirect
d.linux 安装webgrind
e.参考:http://www.linuxeye.com/Linux/2913.html
https://blog.csdn.net/Alex_Best/article/details/6003784
六. 注意事项
1、避免生产环境开启profiler和trace,只需开启远程调试;
2、尽量使用xdebug.profiler_enable_trigger替代xdebug.profiler_enable;
3、如果使用webgrind分析profiler,建议不要放入生产环境,因为其没有安全限制,任何人都可以访问;
4、Xdebug的功能虽然强大,但是要均衡性能开销;
七. 相关资料
https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+Web+Applications+with+Run+Debug+Configurations
https://xdebug.org/docs/remote
ssh -R 9001:localhost:9001 mox@120.92.142.115