เริ่มต้นใช้งานการตรวจสอบประสิทธิภาพสำหรับ Android

ก่อนเริ่มต้น

หากยังไม่ได้ดำเนินการ ให้เพิ่ม Firebase ลงในโปรเจ็กต์ Android

ขั้นตอนที่ 1: เพิ่ม Performance Monitoring SDK ลงในแอป

หลังจากเพิ่ม Performance Monitoring SDK แล้ว Firebase จะเริ่มรวบรวมข้อมูลโดยอัตโนมัติ สําหรับการแสดงผลหน้าจอของแอปและข้อมูล ที่เกี่ยวข้องกับวงจรของแอป (เช่น เวลาเริ่มต้นของแอป) หากต้องการ เปิดใช้ Firebase เพื่อตรวจสอบคำขอเครือข่าย คุณต้องเพิ่มPerformance Monitoring ปลั๊กอิน Gradle ด้วย (ขั้นตอนถัดไป)

  1. ในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle) ให้เพิ่มทรัพยากร Dependency สำหรับคลัง Performance Monitoring สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการควบคุมเวอร์ชันของไลบรารี

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:34.0.0"))
    
        // Add 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 เวอร์ชันที่เข้ากันได้อยู่เสมอ

    (ทางเลือก)  เพิ่มการอ้างอิงไลบรารี Firebase โดยไม่ใช้ BoM

    หากเลือกไม่ใช้ Firebase BoM คุณต้องระบุเวอร์ชันของไลบรารี Firebase แต่ละรายการ ในบรรทัดการอ้างอิง

    โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลายรายการในแอป เราขอแนะนำเป็นอย่างยิ่ง ให้ใช้ BoM เพื่อจัดการเวอร์ชันของไลบรารี ซึ่งจะช่วยให้มั่นใจได้ว่าทุกเวอร์ชันจะ เข้ากันได้

    dependencies {
        // Add 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:22.0.0")
    }

  2. คอมไพล์แอปอีกครั้ง

ขั้นตอนที่ 2: เพิ่มปลั๊กอิน Performance Monitoring Gradle ลงในแอป

หลังจากเพิ่มPerformance Monitoringปลั๊กอิน Gradle แล้ว Firebase จะเริ่มรวบรวมข้อมูลสำหรับคำขอเครือข่าย HTTP/S โดยอัตโนมัติ นอกจากนี้ ปลั๊กอินยังช่วยให้คุณวัดร่องรอยโค้ดที่กำหนดเองได้โดยใช้หมายเหตุ@AddTrace

  1. ในไฟล์ Gradle ระดับรูท (ระดับโปรเจ็กต์) (<project>/build.gradle.kts หรือ <project>/build.gradle) ให้เพิ่ม Performance Monitoringปลั๊กอิน Gradle ดังนี้

    Kotlin

    plugins {
        // To benefit from the latest Performance Monitoring plugin features,
        // update your Android Gradle plugin dependency to at least v3.4.0
        id("com.android.application") version "7.3.0" apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id("com.google.gms.google-services") version "4.4.3" apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id("com.google.firebase.firebase-perf") version "2.0.0" apply false
    }

    Groovy

    plugins {
        // To benefit from the latest Performance Monitoring plugin features,
        // update your Android Gradle plugin dependency to at least v3.4.0
        id 'com.android.application' version '7.3.0' apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id 'com.google.gms.google-services' version '4.4.3' apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id 'com.google.firebase.firebase-perf' version '2.0.0' apply false
    }
  2. ในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle) ให้เพิ่มPerformance Monitoring ปลั๊กอิน Gradle ดังนี้

    Kotlin

    plugins {
        id("com.android.application")
    
        // Make sure that you have the Google services Gradle plugin
        id("com.google.gms.google-services")
    
        // Add the Performance Monitoring Gradle plugin
        id("com.google.firebase.firebase-perf")
        ...
    }

    Groovy

    plugins {
        id 'com.android.application'
    
        // Make sure that you have the Google services Gradle plugin
        id 'com.google.gms.google-services'
    
        // Add the Performance Monitoring Gradle plugin
        id 'com.google.firebase.firebase-perf'
        ...
    }
  3. คอมไพล์แอปอีกครั้ง

ขั้นตอนที่ 3: สร้างเหตุการณ์ด้านประสิทธิภาพเพื่อแสดงข้อมูลเริ่มต้น

Firebase จะเริ่มประมวลผลเหตุการณ์เมื่อคุณเพิ่ม SDK ลงในแอปเรียบร้อยแล้ว หากยังคงพัฒนาในเครื่อง ให้โต้ตอบกับแอปเพื่อสร้างเหตุการณ์สำหรับการเก็บรวบรวมและการประมวลผลข้อมูลเริ่มต้น

  1. สร้างเหตุการณ์โดยสลับแอปไปมาระหว่างเบื้องหลังกับเบื้องหน้าหลายครั้ง โต้ตอบกับแอปโดยไปยังส่วนต่างๆ ของหน้าจอ และ/หรือทริกเกอร์คำขอเครือข่าย

  2. ไปที่แดชบอร์ดประสิทธิภาพของFirebaseคอนโซล คุณควรเห็นข้อมูลเริ่มต้นแสดงภายใน ไม่กี่นาที

    หากไม่เห็นข้อมูลเริ่มต้น โปรดอ่านเคล็ดลับในการแก้ปัญหา

ขั้นตอนที่ 4: (ไม่บังคับ) ดูข้อความบันทึกสำหรับเหตุการณ์ด้านประสิทธิภาพ

  1. เปิดใช้การบันทึกการแก้ไขข้อบกพร่องสำหรับ Performance Monitoring ในเวลาที่สร้างโดยการเพิ่มองค์ประกอบ <meta-data> ลงในไฟล์ AndroidManifest.xml ของแอป ดังนี้

    <application>
        <meta-data
          android:name="firebase_performance_logcat_enabled"
          android:value="true" />
    </application>
  2. ตรวจสอบข้อความบันทึกเพื่อหาข้อความแสดงข้อผิดพลาด

  3. Performance Monitoring ติดแท็กข้อความบันทึกด้วย FirebasePerformance การใช้การกรอง logcat จะช่วยให้คุณดูการติดตามระยะเวลาและการบันทึกคำขอเครือข่าย HTTP/S ได้โดยเฉพาะ โดยการเรียกใช้คำสั่งต่อไปนี้

    adb logcat -s FirebasePerformance
  4. ตรวจสอบบันทึกประเภทต่อไปนี้ซึ่งบ่งชี้ว่า Performance Monitoring กำลังบันทึกเหตุการณ์ด้านประสิทธิภาพ

    • Logging trace metric: TRACE_NAME, FIREBASE_PERFORMANCE_CONSOLE_URL
    • Logging network request trace: URL
  5. คลิก URL เพื่อดูข้อมูลในคอนโซล Firebase ระบบอาจใช้เวลาสักครู่ในการอัปเดตข้อมูลในแดชบอร์ด

หากแอปไม่ได้บันทึกเหตุการณ์ประสิทธิภาพ โปรดดูเคล็ดลับในการแก้ปัญหา

ขั้นตอนที่ 5: (ไม่บังคับ) เพิ่มการตรวจสอบที่กำหนดเองสำหรับโค้ดที่เฉพาะเจาะจง

หากต้องการตรวจสอบข้อมูลประสิทธิภาพที่เชื่อมโยงกับโค้ดที่เฉพาะเจาะจงในแอป คุณสามารถ ใช้การติดตามโค้ดที่กำหนดเอง

การติดตามโค้ดที่กำหนดเองช่วยให้คุณวัดระยะเวลาที่แอปใช้ในการทำงาน ที่เฉพาะเจาะจงหรืองานชุดหนึ่ง เช่น การโหลดชุดรูปภาพหรือการค้นหา ฐานข้อมูล เมตริกเริ่มต้นสำหรับการติดตามรหัสที่กำหนดเองคือระยะเวลา แต่คุณยังเพิ่มเมตริกที่กำหนดเองได้ด้วย เช่น การเข้าชมแคชและคำเตือนเกี่ยวกับหน่วยความจำ

ในโค้ด คุณจะกำหนดจุดเริ่มต้นและจุดสิ้นสุดของการติดตามโค้ดที่กำหนดเอง (และเพิ่มเมตริกที่กำหนดเองที่ต้องการ) โดยใช้ API ที่ Performance Monitoring SDK มีให้ สำหรับแอป Android คุณยังตรวจสอบระยะเวลาของ เมธอดที่เฉพาะเจาะจงได้โดยใช้ @AddTrace annotation

ดูข้อมูลเพิ่มเติมเกี่ยวกับฟีเจอร์เหล่านี้และวิธีเพิ่มลงในแอปได้ที่เพิ่มการตรวจสอบโค้ดที่เฉพาะเจาะจง

ขั้นตอนที่ 6: นำแอปไปใช้งานแล้วตรวจสอบผลลัพธ์

หลังจากตรวจสอบความถูกต้องPerformance Monitoringโดยใช้อุปกรณ์ทดสอบอย่างน้อย 1 เครื่องแล้ว คุณจะ นําแอปเวอร์ชันที่อัปเดตแล้วไปใช้งานกับผู้ใช้ได้

คุณตรวจสอบข้อมูลประสิทธิภาพได้ในแดชบอร์ดประสิทธิภาพ ของคอนโซล Firebase

ปัญหาที่ทราบ

  • Performance Monitoring ปลั๊กอิน Gradle v1.1.0 อาจทำให้เกิดความไม่ตรงกันใน Guava dependencies ซึ่งส่งผลให้เกิดข้อผิดพลาดต่อไปนี้

    Error:Execution failed for task ':app:packageInstantRunResourcesDebug'.
    > com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;

    หากเห็นข้อผิดพลาดนี้ คุณจะดำเนินการต่อไปนี้ได้

    • อัปเกรดปลั๊กอิน Performance Monitoring เป็น v1.1.1 ขึ้นไป (เวอร์ชันล่าสุดคือ v2.0.0)

    • แทนที่บรรทัดทรัพยากร Dependency ของปลั๊กอิน Performance Monitoring ในไฟล์ Gradle ระดับรูท (ระดับโปรเจ็กต์) (<project>/build.gradle.kts หรือ <project>/build.gradle) ดังนี้

      Kotlin

      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")
          }
        }
      }

      Groovy

      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'
          }
        }
      }
  • Performance Monitoring รายงานขนาดเพย์โหลดทั้งหมดสำหรับคำขอเครือข่าย HTTP โดยอิงตามค่าที่ตั้งไว้ในส่วนหัว Content-Length ของ HTTP ค่านี้อาจไม่ถูกต้องเสมอไป

  • Performance Monitoring รองรับเฉพาะกระบวนการหลักในแอป Android แบบหลายกระบวนการ

ขั้นตอนถัดไป