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


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

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

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

  • ปิดใช้ Performance Monitoring เมื่อสร้างแอป แต่อนุญาตให้แอปเปิดใช้อีกครั้งที่รันไทม์

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

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

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

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

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

ปิดใช้ปลั๊กอิน Gradle ของ Performance Monitoring

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

  • Extension Property — ปิดใช้ปลั๊กอินสำหรับตัวแปรบิลด์ที่เฉพาะเจาะจงในเวลาคอมไพล์

  • พร็อพเพอร์ตี้โปรเจ็กต์ — ปิดใช้ปลั๊กอินสำหรับตัวแปรบิลด์ทั้งหมดในเวลาคอมไพล์

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

การใช้แฟล็ก Extensions Property จะช่วยให้คุณปิดใช้ปลั๊กอิน 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

ปิดใช้ไลบรารี Android Performance 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:24.0.0")
    

    Java

      implementation("com.google.firebase:firebase-config:24.0.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

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

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