Performance Monitoring 會收集追蹤記錄,協助您監控應用程式效能。A 罩杯 追蹤是系統擷取的
您可以自行建立追蹤記錄,監控與 的特定程式碼透過自訂程式碼追蹤記錄,您可以評估 應用程式完成特定工作或一組工作所需的時間 載入一組圖片或查詢資料庫的範例。
自訂程式碼追蹤記錄的預設指標為「時間長度」(從 追蹤記錄的起點和停止點),但您可以新增 自訂指標。
您可以在程式碼中使用 Performance Monitoring SDK 提供的 API,定義自訂程式碼追蹤的開始和結束時間。
如果是 Android 應用程式,您也可以使用
@AddTrace
註解。
建立自訂程式碼追蹤記錄後,隨時都能啟用,
如何確保執行緒安全
由於這些追蹤記錄的預設指標是「持續時間」,因此不會 有時也稱為「時間長度追蹤記錄」。
您可以在追蹤記錄的「自訂追蹤記錄」子分頁中查看這些追蹤記錄的資料 表格,位於「成效」資訊主頁底部 (進一步瞭解 使用主控台)。
預設屬性、自訂屬性和自訂指標
針對自訂程式碼追蹤記錄,Performance Monitoring 會自動記錄預設屬性 (常見中繼資料,例如應用程式版本、國家/地區、裝置等),方便您在 Firebase 主控台中篩選追蹤記錄資料。個人中心 您也可以新增及監控自訂屬性 例如遊戲關卡或使用者屬性。您可以進一步設定自訂程式碼追蹤,為在追蹤範圍內發生的效能相關事件記錄自訂指標。舉例來說,您可以建立自訂指標,用於計算快取命中和未命中的次數,或是 UI 在一段明顯的時間內未回應的次數。
自訂屬性和自訂指標會顯示在 Firebase 主控台中,與追蹤記錄的預設屬性和預設指標並列。
新增自訂程式碼追蹤記錄
使用「Performance Monitoring」 Trace API 來新增自訂程式碼追蹤記錄,以監控特定的應用程式程式碼。
注意事項:
- 應用程式可以有多個自訂程式碼追蹤記錄。
- 您可以同時執行多個自訂程式碼追蹤記錄。
- 自訂程式碼追蹤的名稱必須符合下列規定:
或結尾空白、沒有前置底線 (
_
) 字元,以及長度上限 最多 100 個字元 - 自訂程式碼追蹤記錄支援新增自訂指標和 自訂屬性。
如要啟動及停止自訂程式碼追蹤,請使用類似下方的程式碼行包裝要追蹤的程式碼 (本範例使用 test_trace
的自訂追蹤名稱):
Kotlin+KTX
// Import these Performance Monitoring classes at the top of your `.kt` file import com.google.firebase.perf.FirebasePerformance; import com.google.firebase.perf.metrics.Trace; val myTrace = Firebase.performance.newTrace("test_trace") myTrace.start() // code that you want to trace myTrace.stop()
Java
// Import these Performance Monitoring classes at the top of your `.java` file import com.google.firebase.perf.FirebasePerformance; import com.google.firebase.perf.metrics.Trace; Trace myTrace = FirebasePerformance.getInstance().newTrace("test_trace"); myTrace.start(); // code that you want to trace myTrace.stop();
(選用) 使用 @AddTrace 監控特定方法
Android 應用程式也支援 @AddTrace
註解,可用於檢測自訂程式碼追蹤記錄。使用這項功能時,追蹤記錄會從
,並在方法完成時停止,包括
這個方法所叫用的任何資訊。
舉例來說,您可以建立名為 onCreateTrace
的自訂程式碼追蹤,在呼叫 onCreate()
方法時執行。
Kotlin+KTX
// Import these Performance Monitoring classes at the top of your `.kt` file import com.google.firebase.perf.FirebasePerformance; import com.google.firebase.perf.metrics.AddTrace; // Add the `@AddTrace` annotation above the method you want to trace // the `enabled` argument is optional and defaults to true @AddTrace(name = "onCreateTrace", enabled = true) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }
Java
// Import these Performance Monitoring classes at the top of your `.java` file import com.google.firebase.perf.FirebasePerformance; import com.google.firebase.perf.metrics.AddTrace; // Add the `@AddTrace` annotation above the method you want to trace @Override @AddTrace(name = "onCreateTrace", enabled = true /* optional */) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
在自訂程式碼追蹤記錄中新增自訂指標
使用「Performance Monitoring」 Trace API 將自訂指標新增至自訂程式碼追蹤記錄。
注意事項:
- 自訂指標的名稱必須符合下列規定:開頭或結尾不得有空白字元、開頭不得有底線 (
_
) 字元,且長度上限為 100 個字元。 - 每個自訂程式碼追蹤記錄最多可記錄 32 個指標 (包括預設指標) 「Duration」指標)。
如要新增自訂指標,每次新增一行類似下方的程式碼
事件發生的時間舉例來說
應用程式內發生的效能相關事件,例如快取命中和失敗事件
(使用「item_cache_hit
」和「item_cache_miss
」的事件名稱範例,以及
遞增 1
)。
Kotlin+KTX
val myTrace = Firebase.performance.newTrace("test_trace") myTrace.start() // code that you want to trace (and log custom metrics) val item = cache.fetch("item") if (item != null) { myTrace.incrementMetric("item_cache_hit", 1) } else { myTrace.incrementMetric("item_cache_miss", 1) } myTrace.stop()
Java
Trace myTrace = FirebasePerformance.getInstance().newTrace("test_trace"); myTrace.start(); // code that you want to trace (and log custom metrics) Item item = cache.fetch("item"); if (item != null) { myTrace.incrementMetric("item_cache_hit", 1); } else { myTrace.incrementMetric("item_cache_miss", 1); } myTrace.stop();
建立自訂程式碼追蹤記錄的自訂屬性
使用 Performance Monitoring Trace API 新增自訂屬性至自訂程式碼追蹤記錄。如要使用自訂屬性,請在應用程式中新增程式碼,定義屬性並將其與特定自訂程式碼追蹤連結。您可以在追蹤開始和停止之間的任何時間設定自訂屬性。
注意事項:
自訂屬性名稱必須符合下列規定:
- 開頭或結尾無空白,無前置底線 (
_
) 字元 - 不能有空格
- 長度上限為 32 個半形字元
- 名稱可使用的字元為
A-Z
、a-z
和_
。
- 開頭或結尾無空白,無前置底線 (
每個自訂程式碼追蹤記錄最多可記錄 5 個自訂屬性。
請確認自訂屬性未包含 則代表 Google 會識別個人身分。
進一步瞭解這項規範
Kotlin+KTX
Firebase.performance.newTrace("test_trace").trace { // Update scenario. putAttribute("experiment", "A") // Reading scenario. val experimentValue = getAttribute("experiment") // Delete scenario. removeAttribute("experiment") // Read attributes. val traceAttributes = this.attributes }
Java
Trace trace = FirebasePerformance.getInstance().newTrace("test_trace"); // Update scenario. trace.putAttribute("experiment", "A"); // Reading scenario. String experimentValue = trace.getAttribute("experiment"); // Delete scenario. trace.removeAttribute("experiment"); // Read attributes. Map<String, String> traceAttributes = trace.getAttributes();
追蹤、查看及篩選成效資料
在資訊主頁中追蹤特定指標
如要瞭解重要指標的趨勢,請將指標新增至 成效資訊主頁頂端的指標看板。您可以查看每週變化,快速找出迴歸問題,或驗證程式碼的近期變更是否有助於改善效能。
Firebase Performance Monitoring 資訊主頁/>如要在指標板中新增指標,請按照下列步驟操作:
- 前往 Firebase 控制台中的「效能」資訊主頁。
- 按一下空白指標資訊卡,然後選取要加入資訊主面板的現有指標。
- 按一下 在已填入的指標資訊卡上, ,可查看更多選項, 例如取代或移除指標
指標板會顯示收集到的指標資料,包括圖形和數字百分比變化。
進一步瞭解如何使用資訊主頁。
查看追蹤記錄和相關資料
如要查看追蹤記錄,請前往 Firebase 控制台中的 「成效」資訊主頁,向下捲動至「追蹤記錄」表格,然後按一下適當的子頁籤。表格會顯示每個追蹤記錄的前幾項指標,您甚至可以依特定指標的百分比變化排序清單。
Performance Monitoring 於 Firebase 控制台提供疑難排解頁面,該頁面會醒目顯示指標 以利快速處理,並盡量減少 應用程式和使用者如要瞭解潛在商機,請利用疑難排解頁面 效能問題,如下列情況:
- 您在資訊主頁上選取相關指標後,發現出現大幅差異。
- 在追蹤記錄表格中,頂端的差異值最大,且 百分比呈現顯著變化
- 您會在出現成效問題時收到電子郵件快訊,
您可以透過下列方式存取疑難排解頁面:
- 在指標資訊主頁中,按一下「查看指標詳細資料」按鈕。
- 在任何指標資訊卡上,依序選取
。「疑難排解」頁面會顯示指標相關資訊 這個虛擬機器 =>「查看詳細資料」 - 在追蹤記錄表格中,按一下追蹤記錄名稱或該資料列中的任何指標值 追蹤記錄。
- 在電子郵件快訊中,按一下「立即調查」。
在追蹤記錄表中按一下追蹤記錄名稱,即可深入瞭解感興趣的指標。按一下
- 依「應用程式版本」篩選,查看過去或最新版本的資料
- 依「裝置」篩選,瞭解舊版裝置如何處理您的應用程式
- 依「國家/地區」篩選,確認資料庫位置不會影響特定區域
進一步瞭解 查看您的資料 追蹤記錄。
後續步驟
進一步瞭解 使用屬性來查看成效資料。
進一步瞭解如何在 Firebase 控制台中追蹤效能問題。
設定快訊,以因應成效下滑的程式碼變更 應用程式的效能舉例來說,您可以設定 修正特定自訂程式碼追蹤記錄的時間長度超過 門檻值
- 查看有關使用者工作階段的詳細報表 您可以查看在時間軸上,查看在相同時間收集到的其他追蹤記錄 會很有幫助