Xdebug 內建 Profiler 能協助找出 PHP 腳本效能瓶頸,輸出相容 CacheGrind 格式,搭配 KCacheGrind(Linux/KDE)、QCacheGrind(Windows/Mac)或 WinCacheGrind 進行視覺化分析。
在 php.ini 設定:
xdebug.profiler_enable = 1 xdebug.profiler_output_dir = "/tmp/xdebug_profiler" xdebug.profiler_output_name = "cachegrind.out.%p"
或使用觸發模式(不影響所有請求):
xdebug.profiler_enable = 0 xdebug.profiler_enable_trigger = 1 # 瀏覽器加上 ?XDEBUG_PROFILE=1 即可觸發
Self 欄為該函數本身耗時(不含子函數),Called 為呼叫次數Cost 顯示在該呼叫鏈路中佔比Distance 顯示呼叫層級差異內建函數會加 php:: 前綴(如 php::mysql_query),include 檔案顯示為 include::/path/to/file。
| 設定 | 型別 | 預設 | 說明 |
|---|---|---|---|
| xdebug.profiler_append | integer | 0 | 1 = 同名檔案附加不覆蓋 |
| xdebug.profiler_enable | integer | 0 | 全域啟動,不可用 ini_set() 修改 |
| xdebug.profiler_enable_trigger | integer | 0 | 透過 GET/POST/COOKIE 觸發 |
| xdebug.profiler_enable_trigger_value | string | "" | 觸發密碼,防止濫用 |
| xdebug.profiler_output_dir | string | /tmp | 輸出目錄,需寫入權限 |
| xdebug.profiler_output_name | string | cachegrind.out.%p | 檔名格式,支援 %p(pid)、%r、%u 等 |
xdebug_get_profiler_filename() // 回傳目前分析檔案完整路徑
善用 Xdebug Profiler 能快速定位慢查詢、重複計算、遞歸過深等效能殺手。