前言
Eclipse 內建 Profiling 模式需搭配 TPTP (Test and Performance Tools Platform) 使用。本文記錄在 Eclipse 中安裝 TPTP Runtime、Agent Controller 並對 Tomcat 進行效能分析的完整步驟。
環境需求
- Eclipse 3.x / 4.x (含現代版本)
- Tomcat 6.0+
- JDK 1.5+
安裝步驟
1. 下載 TPTP Runtime (必要)
- 官網:
https://www.eclipse.org/tptp/home/downloads/?ver=4.6.0#tptp-plugins
- 下載 Runtime** 版本 (非 SDK),選擇對應平台或 All-in-one ZIP
- 解壓至 Eclipse 安裝目錄 (覆蓋 plugins/features 目錄)
2. 下載 Agent Controller (必要)
- 官網:
https://www.eclipse.org/tptp/home/downloads/?ver=4.6.0#rac
- 下載 Runtime 版本,解壓至 Eclipse 安裝目錄
- Agent Controller 負責在目標 JVM 收集資料並傳回 Eclipse
3. 可選元件 (視需求)
- Native Logging / Generic Log Adapter:日誌整合
- SDK 版本:需自行擴展 TPTP 時使用
4. 重啟 Eclipse 驗證
- 啟動 Eclipse
- 檢查
Window → Preferences → Test and Performance → Profiling 是否出現設定選項
- 伺服器視圖中 Tomcat 啟動圖示旁出現「Start the server in profiling mode」圖示 (綠色三角形帶圖表圖示)
啟動 Profiling 模式
- 在 Servers 視圖右鍵 Tomcat → Profile (或點擊 Profiling 模式圖示)
- 彈出 Profiling Configuration 對話框
- 選擇監控類型:
- Execution Flow Analysis:方法呼叫流程、執行時間、呼叫次數
- Memory Profiling:物件分配、記憶體洩漏、GC 分析
- Thread Profiling:執行緒狀態、死鎖、同步爭用
- Coverage:代碼覆蓋率
- 點擊 OK 啟動 Tomcat
常見問題排查
錯誤:Could not launch in profiling mode because no profilers are configured
原因:TPTP Runtime 未正確安裝或版本不匹配。
解決:
- 確認 Eclipse 版本與 TPTP 版本對應 (如 Eclipse 3.4 需 TPTP 4.5.x)
- 檢查
$ECLIPSE/plugins 是否有 org.eclipse.tptp.* 套件
- 嘗試
eclipse -clean 重新載入外掛
- 查看
.metadata/.log 尋找 TPTP 相關錯誤
Agent Controller 連線失敗
- 確認
$ECLIPSE/configuration/org.eclipse.tptp.platform.ac.core/agentctrl.properties 設定正確
- 防火牆允許 Agent Controller 通訊埠 (預設 10002)
- Tomcat 啟動參數加入:
-agentlib:JPIBootLoader=JPIAgent:server=standalone (舊版需手動加入)
現代替代方案 (推薦)
TPTP 專案已於 2011 年停止維護,現代 Java 開發建議使用:
| 工具 | 特點 | 整合方式 |
| VisualVM | JDK 內建、GUI 豐富、支援取樣/檢測模式 | 獨立啟動 jvisualvm 或 Eclipse 外掛 |
| JProfiler | 商業級、功能最強、支援遠端、異常分析 | Eclipse/IDEA 外掛、獨立 UI |
| YourKit | 商業級、低開銷、優異的記憶體/CPU 分析 | Eclipse/IDEA 外掛、Maven/Gradle 整合 |
| Java Mission Control (JMC) | Oracle 官方、JFR 飛行記錄器、生產級低開銷 | JDK 內建 (JDK 7u40+)、獨立啟動 jmc |
| Async Profiler | 開源、極低開銷、火焰圖、支援 Linux/macOS | 命令列、GitHub Actions CI 整合 |
| Eclipse MAT | 記憶體分析專家、支援 HPROF、支配者樹 | 獨立 RCP、Eclipse 外掛 |
VisualVM 快速上手
- JDK bin 目錄執行
jvisualvm
- 左側 Applications 列表雙擊本地 Tomcat 進程
- 分頁:Overview (摘要)、Monitor (即時圖表)、Threads (執行緒)、Sampler (CPU/記憶體取樣)、Profiler (檢測模式)
- Heap Dump 分析:右鍵進程 → Heap Dump → 類別/物件/支配者樹分析
生產環境遠端 Profiling 最佳實踐
- 使用 JFR (Java Flight Recorder):
-XX:StartFlightRecording=duration=60s,filename=recording.jfr 低開銷持續記錄
- 避免檢測模式:生產環境僅用取樣模式,防止效能影響
- 定期 Heap Dump:
jmap -dump:live,format=b,file=heap.hprof <pid>
- 安全考量:JMX/RMI 遠端連線需 TLS、認證、IP 白名單