当前位置: 首页 > 后端技术 > PHP

PHP+xdebug分析代码性能瓶颈

时间:2023-03-30 03:31:53 PHP

通常会开启xdebug插件,性能测试输出文件会随之生成,一般在cachegrind.out.xxxx文件中。该文件可用于第三方工具进行代码性能分析。但是如果本地有多个项目/网站,所有的profile都输出到一个文件中,不方便后面的性能分析。自定义profile文件的名称可以通过配置xdebug.profiler_output_name参数来设置输出文件名。部分参数如下:symbol含义configurationsamplesamplefilename%c当前工作目录的crc32checksumcachegrind.out.%ccachegrind.out。1258863198%ppidcachegrind.out.%pcachegrind.out.9685%r随机数cachegrind.out.%rcachegrind.out.072db0%s当前服务器进程的脚本文件名(注)cachegrind.out.%scachegrind.out._home_httpd_html_test_xdebug_test_php%tUnix时间戳(秒)cachegrind.out.%tcachegrind.out.1179434742%uUnix时间戳(微秒)cachegrind.out.%ucachegrind.out.1179434749_642382%H$_SERVER['HTTP_HOST']cachegrind.out.%Hcachegrind.out.localhost%R$_SERVER['REQUEST_URI']cachegrind.out.%Rcachegrind.out._test_xdebug_test_php_var=1_var2%Ssession_id(来自$_COOKIE,如果已设置)cachegrind.out.%Scachegrind.out.c70c1ec2375af58f74b390bbdd2a679din%ind%%charcachegrind。out.%%cachegrind.out.%%编辑php.ini配置文件:xdebug.profiler_output_name=cachegrind.out.%H然后重启php服务器。在Mac上,配置文件存储在/var/tmp/目录中。Mac上的性能分析可以使用MacCallGrind和qcachegrind,但前者收费,可直接通过AppleStore下载,后者免费。需要手动安装。安装graphviz调用Graph函数:$brewinstallgraphviz安装qcachegrind:$brewinstallqcachegrind安装完成后,即可打开qcachegrind应用。图形界面如下:其他,但需要注意。打开profile文件输出后,如果本地project太多的话,很容易占用大片的磁盘空间。下图是我半年左右没有清理的状态:可以使用命令清理:$sudorm-fr/private/var/tmp/cachegrind.out.*参考链接Usexdebugtophp执行profile输出php+xdebug+qcachegrind(mac)性能分析