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;
}
}
}
}
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