เริ่มต้นใช้งานการตรวจสอบประสิทธิภาพสำหรับ 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.13.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 เวอร์ชันที่เข้ากันได้อยู่เสมอ

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

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

    โปรดทราบว่าหากคุณใช้ไลบรารี 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.5")
    }

  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.4" apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id("com.google.firebase.firebase-perf") version "2.0.2" 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.4' apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id 'com.google.firebase.firebase-perf' version '2.0.2' apply false
    }
  2. ในไฟล์ Gradle ระดับโมดูล (ระดับแอป) (โดยปกติจะเป็น <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle) ให้เพิ่มปลั๊กอิน Gradle Performance Monitoring ดังนี้

    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. ไปที่แดชบอร์ด Performance ของคอนโซล 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 คุณสามารถดูการบันทึกการติดตามระยะเวลาและคำขอเครือข่าย HTTP/S ได้โดยเฉพาะโดยใช้การกรอง logcat ด้วยการเรียกใช้คำสั่งต่อไปนี้

    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

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

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

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

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

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

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

    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.2)

    • แทนที่บรรทัดทรัพยากร 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 แบบหลายกระบวนการ

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