ปิดใช้การตรวจสอบประสิทธิภาพของ Firebase


ในระหว่างการพัฒนาและการทดสอบแอป คุณอาจพบว่าการปิดใช้ Performance Monitoringมีประโยชน์

ตัวอย่างเช่น การ ปิดใช้ Performance Monitoringในระหว่างกระบวนการบิลด์แอปจะช่วยให้คุณทำสิ่งต่อไปนี้ได้

  • ปิดใช้ฟังก์ชันการทำงานบางอย่างของ Performance Monitoring (เช่น ฟังก์ชันการทำงานที่ปลั๊กอิน Performance Monitoring Gradle มีให้) ในบิลด์ดีบัก แต่เปิดใช้ฟังก์ชันการทำงานอีกครั้งสำหรับ บิลด์ที่เผยแพร่

  • ปิดใช้ Performance Monitoring เมื่อบิลด์แอป แต่ให้แอปเปิดใช้การตรวจสอบประสิทธิภาพอีกครั้ง ในระหว่างรันไทม์

  • ปิดใช้ Performance Monitoring เมื่อบิลด์แอป และไม่อนุญาตให้แอป เปิดใช้การตรวจสอบประสิทธิภาพอีกครั้งในระหว่างรันไทม์

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

ปิดใช้ Performance Monitoring ในระหว่างกระบวนการบิลด์แอป

คุณสามารถปิดใช้ Performance Monitoring ในระหว่างกระบวนการบิลด์ได้โดย ปิดใช้ปลั๊กอิน Performance Monitoring Gradle และ/หรือโดย ปิดใช้คลัง Performance Monitoring สำหรับ Android

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

ปิดใช้ปลั๊กอิน GradlePerformance Monitoring

คุณสามารถปิดใช้ปลั๊กอิน Performance Monitoring ได้โดยเพิ่มแฟล็ก instrumentationEnabled โดยใช้ตัวเลือกต่อไปนี้

ปิดใช้ปลั๊กอินผ่านแฟล็กพร็อพเพอร์ตี้ส่วนขยาย

การใช้แฟล็ก พร็อพเพอร์ตี้ส่วนขยาย จะช่วยให้คุณปิดใช้ปลั๊กอินPerformance Monitoring สำหรับตัวแปรบิลด์ เฉพาะ ในระหว่างคอมไพล์ได้

  1. ในไฟล์ Gradle ระดับราก (ระดับโปรเจ็กต์) (<project>/build.gradle.kts หรือ <project>/build.gradle) ให้ตรวจสอบว่าได้ระบุทรัพยากร Dependency ของ ปลั๊กอิน Android Gradle เป็น v3.4.0 ขึ้นไป

    สำหรับปลั๊กอิน Android Gradle เวอร์ชันก่อนหน้า คุณยังคงปิดใช้ ปลั๊กอิน Performance Monitoringสำหรับตัวแปรบิลด์เฉพาะได้ แต่ระบบจะไม่ตัดเวลาบิลด์ สำหรับตัวแปรนั้นออกทั้งหมด

  2. เพิ่มแฟล็กต่อไปนี้ลงในไฟล์ Gradle ระดับโมดูล (ระดับแอป) (โดยปกติจะเป็น <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle) จากนั้นตั้งค่าเป็น false เพื่อปิดใช้ ปลั๊กอินPerformance Monitoring

    Kotlin

    import com.google.firebase.perf.plugin.FirebasePerfExtension
    
    // ...
    
    android {
      // ...
      buildTypes {
        getByName("debug") {
          configure<FirebasePerfExtension> {
            // Set this flag to 'false' to disable @AddTrace annotation processing and
            // automatic monitoring of HTTP/S network requests
            // for a specific build variant at compile time.
            setInstrumentationEnabled(false)
          }
        }
      }
    }

    Groovy

    android {
      // ...
      buildTypes {
        debug {
          FirebasePerformance {
            // Set this flag to 'false' to disable @AddTrace annotation processing and
            // automatic monitoring of HTTP/S network requests
            // for a specific build variant at compile time.
            instrumentationEnabled false
          }
        }
      }
    }

ปิดใช้ปลั๊กอินผ่านแฟล็กพร็อพเพอร์ตี้โปรเจ็กต์

การใช้แฟล็ก พร็อพเพอร์ตี้โปรเจ็กต์ จะช่วยให้คุณปิดใช้ปลั๊กอิน Performance Monitoring สำหรับ ทั้งหมด ในระหว่างคอมไพล์ได้

เพิ่มแฟล็กต่อไปนี้ลงในไฟล์ gradle.properties จากนั้นตั้งค่าเป็น false เพื่อปิดใช้ปลั๊กอิน Performance Monitoring

// ...

// Set this flag to 'false' to disable @AddTrace annotation processing and
// automatic monitoring of HTTP/S network requests
// for all build variants at compile time.
firebasePerformanceInstrumentationEnabled=false

ปิดใช้คลังสำหรับ AndroidPerformance Monitoring

หากคุณปิดใช้ไลบรารี Performance Monitoring ในระหว่างคอมไพล์ คุณสามารถเลือกว่า จะอนุญาตให้แอปเปิดใช้ไลบรารีในระหว่างรันไทม์หรือไม่

ปิดใช้คลังในระหว่างคอมไพล์ แต่ให้แอปเปิดใช้คลังในระหว่างรันไทม์

เพิ่มองค์ประกอบ <meta-data> ต่อไปนี้ลงในไฟล์ AndroidManifest.xml ของแอป

  <application>
    <meta-data
      android:name="firebase_performance_collection_enabled"
      android:value="false" />
  </application>

ปิดใช้คลังในระหว่างคอมไพล์ แต่ไม่อนุญาตให้แอปเปิดใช้คลังในระหว่างรันไทม์

เพิ่มองค์ประกอบ <meta-data> ต่อไปนี้ลงในไฟล์ AndroidManifest.xml ของแอป

  <application>
    <meta-data
      android:name="firebase_performance_collection_deactivated"
      android:value="true" />
  </application>

ปิดใช้แอปในระหว่างรันไทม์โดยใช้ Remote Config

Firebase Remote Config ช่วยให้คุณทำการเปลี่ยนแปลงลักษณะการทำงานและรูปลักษณ์ ของแอปได้ จึงเป็นวิธีที่เหมาะอย่างยิ่งในการปิดใช้ Performance Monitoring ใน อินสแตนซ์ที่ใช้งานจริงของแอป

หากต้องการปิดใช้การเก็บรวบรวมข้อมูลPerformance Monitoringในครั้งถัดไปที่แอป Android เริ่มทำงาน ให้ใช้โค้ดตัวอย่างที่แสดงด้านล่าง ดูข้อมูลเพิ่มเติมเกี่ยวกับการใช้ Remote Configในแอป Android ได้ที่หัวข้อ ใช้ Firebase Remote Config ใน Android

  1. ตรวจสอบว่า Remote Config อยู่ในส่วน dependencies ของไฟล์ Gradle ระดับโมดูล (ระดับแอป) (โดยปกติจะเป็น <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle)

    Kotlin

      implementation("com.google.firebase:firebase-config-ktx:23.1.0")
    

    Java

      implementation("com.google.firebase:firebase-config:23.1.0")
    
  2. ตั้งค่า Remote Config และปิดใช้ Performance Monitoring หาก perf_disable ถูก ตั้งค่าเป็น true:

    Kotlin

    // Setup remote config
    val config = Firebase.remoteConfig
    
    // You can uncomment the following two statements to permit more fetches when
    // validating your app, but you should comment out or delete these lines before
    // distributing your app in production.
    // val configSettings = remoteConfigSettings {
    //     minimumFetchIntervalInSeconds = 3600
    // }
    // config.setConfigSettingsAsync(configSettings)
    // Load in-app defaults from an XML file that sets perf_disable to false until you update
    // values in the Firebase Console
    
    // Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true
    config.setDefaultsAsync(R.xml.remote_config_defaults)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Firebase.performance.isPerformanceCollectionEnabled = !config.getBoolean("perf_disable")
            } else {
                // An error occurred while setting default parameters
            }
        }

    Java

    // Setup remote config
    final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
    
    // You can uncomment the following two statements to permit more fetches when
    // validating your app, but you should comment out or delete these lines before
    // distributing your app in production.
    // FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
    //       .setMinimumFetchIntervalInSeconds(3600)
    //       .build();
    // config.setConfigSettingsAsync(configSettings);
    // Load in-app defaults from an XML file that sets perf_disable to false until you update
    // values in the Firebase Console
    
    //Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true
    config.setDefaultsAsync(R.xml.remote_config_defaults)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        if (config.getBoolean("perf_disable")) {
                            FirebasePerformance.getInstance().setPerformanceCollectionEnabled(false);
                        } else {
                            FirebasePerformance.getInstance().setPerformanceCollectionEnabled(true);
                        }
                    } else {
                        // An error occurred while setting default parameters
                    }
                }
            });
  3. เพิ่มโค้ดต่อไปนี้ลงใน MainActivity.java เพื่อดึงและเปิดใช้งาน Remote Config ค่า:

    Kotlin

    // Remote Config fetches and activates parameter values from the service
    val config = Firebase.remoteConfig
    config.fetch(3600)
        .continueWithTask { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    throw it
                }
            }
            config.activate()
        }
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Parameter values successfully activated
                // ...
            } else {
                // Handle errors
            }
        }

    Java

    //Remote Config fetches and activates parameter values from the service
    final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
    config.fetch(3600)
            .continueWithTask(new Continuation<Void, Task<Boolean>>() {
                @Override
                public Task<Boolean> then(@NonNull Task<Void> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }
                    return config.activate();
                }
            })
            .addOnCompleteListener(new OnCompleteListener<Boolean>() {
                @Override
                public void onComplete(@NonNull Task<Boolean> task) {
                    if (task.isSuccessful()) {
                        // Parameter values successfully activated
                        // ...
                    } else {
                        // Handle errors
                    }
                }
            });
  4. หากต้องการปิดใช้ Performance Monitoring ในคอนโซล Firebase ให้สร้างพารามิเตอร์ perf_disable ในโปรเจ็กต์ของแอป แล้วตั้งค่าเป็น true

    การเปลี่ยนแปลงนี้จะทำให้การเรียกใช้ Performance Monitoring SDK เป็นการเรียกใช้ "ไม่มีการดำเนินการ" (NOOP) ซึ่งจะช่วยลดผลกระทบอย่างมีนัยสำคัญต่อประสิทธิภาพของแอปจากการใช้ Performance Monitoring SDK ในแอป

    หากคุณตั้งค่า perf_disable เป็น false Performance Monitoring จะยังคง เปิดใช้