在你開始之前
步驟1 :將Performance Monitoring SDK添加到您的應用中
添加Performance Monitoring SDK後,Firebase會自動開始收集應用程序的屏幕渲染數據以及與應用程序生命週期相關的數據(例如應用程序啟動時間)。為了使火力地堡來監視網絡請求,你還必須添加性能監視搖籃插件(下一步)。
使用Firebase Android BoM ,在模塊(應用程序級)Gradle文件(通常為
app/build.gradle
)中聲明Performance Monitoring Android庫的依賴app/build.gradle
。爪哇
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:27.1.0') // Declare the dependency for the Performance Monitoring library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-perf' }
通過使用Firebase Android BoM ,您的應用將始終使用Firebase Android庫的兼容版本。
(可選)不使用BoM聲明Firebase庫依賴關係
如果選擇不使用Firebase BoM,則必須在其依賴關係行中指定每個Firebase庫版本。
請注意,如果您在應用中使用多個Firebase庫,我們強烈建議您使用BoM來管理庫版本,以確保所有版本都兼容。
dependencies { // Declare the dependency for the Performance Monitoring library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-perf:19.1.1' }
Kotlin + KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:27.1.0') // Declare the dependency for the Performance Monitoring library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-perf-ktx' }
通過使用Firebase Android BoM ,您的應用將始終使用Firebase Android庫的兼容版本。
(可選)不使用BoM聲明Firebase庫依賴關係
如果選擇不使用Firebase BoM,則必須在其依賴關係行中指定每個Firebase庫版本。
請注意,如果您在應用中使用多個Firebase庫,我們強烈建議您使用BoM來管理庫版本,以確保所有版本都兼容。
dependencies { // Declare the dependency for the Performance Monitoring library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-perf-ktx:19.1.1' }
重新編譯您的應用程序。
步驟2 :將Performance Monitoring插件添加到您的應用中
添加了Performance Monitoring Gradle插件後,Firebase會自動開始為HTTP / S網絡請求收集數據。該插件還使您能夠使用@AddTrace註釋來檢測自定義代碼跟踪。
在您的模塊(應用程序級) Gradle文件(通常為
app/build.gradle
)中,應用Performance Monitoring插件:apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' // Apply the Performance Monitoring plugin apply plugin: 'com.google.firebase.firebase-perf' android { // ... }
在您的根級別(項目級別)的Gradle文件(
build.gradle
)中,添加規則以包括Performance Monitoring插件。buildscript { repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository // Add the Bintray repository jcenter() } dependencies { // ... // To benefit from the latest Performance Monitoring plugin features, // update your Android Gradle Plugin dependency to at least v3.4.0 classpath 'com.android.tools.build:gradle:3.4.0' classpath 'com.google.gms:google-services:4.3.5' // Google Services plugin // Add the dependency for the Performance Monitoring plugin classpath 'com.google.firebase:perf-plugin:1.3.5' // Performance Monitoring plugin } }
重新編譯您的應用程序。
步驟3 :生成性能事件以顯示初始數據
將SDK成功添加到應用程序後,Firebase將開始處理事件。如果您仍在本地進行開發,請與您的應用進行交互以生成事件以進行初始數據收集和處理。
通過多次在後台和前台之間切換應用程序,通過在屏幕之間導航和/或觸發網絡請求與應用程序進行交互來生成事件。
轉到Firebase控制台的“性能”儀表板。您將在幾分鐘後看到初始數據顯示。
如果看不到初始數據的顯示,請查看故障排除提示。
步驟4 :(可選)查看性能事件的日誌消息
通過在應用程序的
AndroidManifest.xml
文件中添加<meta-data>
元素,在構建時為性能監控啟用調試日誌記錄,如下所示:<application> <meta-data android:name="firebase_performance_logcat_enabled" android:value="true" /> </application>
檢查您的日誌消息中是否有任何錯誤消息。
性能監控使用
FirebasePerformance
標記其日誌消息。使用logcat篩選,您可以通過運行以下命令來專門查看持續時間跟踪和HTTP / S網絡請求日誌記錄:adb logcat -s FirebasePerformance
檢查以下類型的日誌,它們指示性能監控正在記錄性能事件:
-
Logging trace metric: TRACE_NAME
-
Logging network request trace: URL
-
如果您的應用程序未記錄性能事件,請查看故障排除提示。
步驟5 :(可選)為特定代碼添加自定義監控
要監視與您的應用程序中的特定代碼關聯的性能數據,您可以檢測自定義代碼跟踪。
使用自定義代碼跟踪,您可以測量應用程序完成特定任務或一組任務所需的時間,例如加載一組圖像或查詢數據庫。自定義代碼跟踪的默認度量標準是其持續時間,但是您也可以添加自定義度量標準,例如緩存命中和內存警告。
在代碼中,您可以使用Performance Monitoring SDK提供的API定義自定義代碼跟踪的開始和結束(並添加任何所需的自定義指標)。對於Android應用程序,您還可以使用@AddTrace註解監視特定方法的持續時間。
訪問“添加監控”以獲取特定代碼,以了解有關這些功能以及如何將其添加到您的應用程序的更多信息。
第6步:部署您的應用程序,然後查看結果
使用一個或多個測試設備驗證性能監控後,可以將應用程序的更新版本部署給用戶。
您可以在Firebase控制台的“性能”儀表板中監視性能數據。
已知的問題
Performance Monitoring Gradle插件v1.1.0可能會導致Guava依賴項不匹配,從而導致以下錯誤:
Error:Execution failed for task ':app:packageInstantRunResourcesDebug'. > com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;
如果看到此錯誤,則可以:
將性能監視插件升級到v1.1.1或更高版本(最新版本是v1.3.5)。
替換根級別(項目級別)Gradle文件(
build.gradle
)中的Performance Monitoring插件依賴項行,如下所示:buildscript { // ... dependencies { // ... // Replace the standard Performance Monitoring plugin dependency line, as follows: classpath ('com.google.firebase:perf-plugin:1.1.0') { exclude group: 'com.google.guava', module: 'guava-jdk5' } } }
性能監視基於HTTP內容長度標頭中設置的值報告HTTP網絡請求的總有效負載大小。此值可能並不總是準確的。
性能監控僅支持多進程Android應用中的主進程。
下一步
了解有關性能監控自動收集的數據的更多信息:
- 與應用程序生命週期相關的數據,例如應用程序啟動時間
- 應用中用於屏幕渲染的數據
- 應用發出的HTTP / S網絡請求數據
在Firebase控制台中查看,跟踪和過濾性能數據。
通過檢測自定義代碼跟踪,添加對應用程序中特定任務或工作流的監視。