首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Xdebug 性能分析輸出文件名配置詳解:多項目多接口分離策略

     編輯:PHP 綜合知識

Xdebug 性能分析生成的 cachegrind.out.* 文件名可通過 xdebug.profiler_output_name 靈活配置。合理命名對多項目、多接口分析至關重要。

默認配置與痛點

xdebug.profiler_output_name = cachegrind.out.%p

僅包含進程 PID(如 cachegrind.out.1408),導致:

  • 無法區分不同腳本/接口
  • 多站點部署時文件混淆
  • 歷史文件難以關聯具體請求

官方格式符號完整表

符號含義配置示例生成文件名示例
%c當前工作目錄 CRC32trace.%ctrace.1258863198.xt
%p進程 PIDtrace.%ptrace.5174.xt
%r隨機數trace.%rtrace.072db0.xt
%s腳本文件名cachegrind.out.%scachegrind.out._home_httpd_html_test_php
%tUnix 時間戳(秒)trace.%ttrace.1179434742.xt
%uUnix 時間戳(微秒)trace.%utrace.1179434749_642382.xt
%H$_SERVER['HTTP_HOST']trace.%Htrace.kossu.xt
%R$_SERVER['REQUEST_URI']trace.%Rtrace._test_xdebug_test_php_var=1_var2=2.xt
%SSession IDtrace.%Strace.c70c1ec2375af58f74b390bbdd2a679d.xt
%%% 字符trace.%%trace.%.xt

註:%s 不適用於 trace 文件名,僅用於 profiler。

實戰推薦配置

單項目:按腳文件分離

xdebug.profiler_output_name = cachegrind.out.%s

生成:cachegrind.out._var_www_html_index_php,每個 PHP 文件獨立分析文件。

多站點/多域名:域名+請求+時間

xdebug.profiler_output_name = cachegrind.out.%H_%R_%u

生成:cachegrind.out.api.example.com_/v1_user_login_1699900000_123456,完美區分站點、接口、請求時間。

按用戶會話分析:Session ID

xdebug.profiler_output_name = cachegrind.out.%S_%t

適合分析特定用戶操作流程的性能特徵。

配合觸發器使用

xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 1
; 僅當請求攜帶 XDEBUG_PROFILE 參數時生成
; curl "https://example.com/api?XDEBUG_PROFILE=1"

文件管理建議

  1. 設置獨立輸出目錄:xdebug.profiler_output_dir = /var/log/xdebug/profiler
  2. 2. 定期清理:find /var/log/xdebug/profiler -mtime +7 -delete 3. 分析工具:KCacheGrind (Linux)、QCacheGrind (跨平台)、PhpStorm 內置、VS Code PHP Debug 擴展 4. 生產環境慎用 profiler_enable=1,必須用觸發器 + IP 白名單

科學的文件命名策略,讓性能分析從「大海撈針」變為「精準定位」。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved