Try using the two-point measurement function of Firebase Performance Monitoring. [Android]

One of the functions of FireBasePerformanceMonitoring is the Trace function that measures between two points from one point to another. Try using Firebase Performance Monitoring for Android

If it is in the same class, you can create an object with newTrace ("") and do .startTrace () → .stopTrace ().

Considering the case of measuring, it is more maintainable and expandable if it can be used like a utility. Another reason is that I don't want to give Activity a Trace object and stop () as soon as it receives a callback.

I made it with Singleton and Enum, so I will share the output.

PerformanceMontoringSample.java



public class PerformanceMonitoringSample {
    private static PerformanceMonitorSample performanceMonitorSample;
    private final Map<TraceName, Trace> traceMap = new HashMap<>();

    public enum TraceName {
        SAMPLE_ONE("sample_one"),
        SAMPLE_TWO("sample_two");

        private final String traceInstanceName;

        TraceName(String traceInstanceName) {
            this.traceInstanceName = traceInstancName;
        }
    }

    private PerformanceMonitoringSample() {
    }

/**
 *Get an instance
 */
    public static PerformanceMonitoringSample getInstance() {
        if (performanceMonitorSample == null) {
            performanceMonitoringSample = new PerformanceMonitoringSample();
            return performanceMonitorSample;
        } else {
            return performanceMonitorSample;
        }
    }

Up to here

is not it.

Below, I will write the processing of measurement start and end.

PerformanceMontoringSample.java


    /**
     *Start measurement
     */
    public void startTrace(TraceName traceName) {
        if (traceMap.containsKey(traceName)) {
            return;
        }
        Trace myTrace = = FirebasePerformance.getInstance().newTrace(traceName.traceInstanceName);
        myTrace.start();
        traceMap.put(traceName.traceInstanceName, myTrace);

        }
    }

    /**
     *Finish measurement
     */
    public void stopTrace(TraceName traceName) {
         //It may be necessary to check the map itself for null ...
         if (traceMap.containsKey(traceName)) {
             traceMap.remove(traceName.traceInstanceName).stop;
            }
        }
    }
}

How to use

To use it, first create a PerformanceMonitoring instance with getInstance (), and then start () and stop () between the two points you want to measure. Performance Monitoring does a good job behind the scenes, so if you cover only the branches, it will measure nicely & it should not fall so much ...

Recommended Posts

Try using the two-point measurement function of Firebase Performance Monitoring. [Android]
Try using || instead of the ternary operator
Try using the service on Android Oreo
Try using the Emotion API from Android
Try using Firebase Cloud Functions on Android (Java)
Try using the query attribute of Ruby on Rails
Check the status of Java application without using monitoring tool
[Java] Try editing the elements of the Json string using the library
[Swift] How to implement the Twitter login function using Firebase UI ①
I tried using the cache function of Application Container Cloud Service
[Swift] How to implement the Twitter login function using Firebase UI ②
About the basics of Android development
User evaluation using the like function
Try using the messaging system Pulsar
[Rails] I will explain the implementation procedure of the follow function using form_with.
I measured the performance of the in-memory data grid Apache Ignite using Yardstick.