use of com.google.firebase.perf.metrics.Trace in project firebase-android-sdk by firebase.
the class FirebasePerfTraceValidatorTest method createValidTraceMetric.
private TraceMetric.Builder createValidTraceMetric() {
String traceName = "trace_1";
long expectedClientStartTime = 1;
long expectedTraceDuration = 50;
TransportManager transportManager = mock(TransportManager.class);
AppStateMonitor appStateMonitor = mock(AppStateMonitor.class);
ArgumentCaptor<TraceMetric> argMetric = ArgumentCaptor.forClass(TraceMetric.class);
Trace trace = new Trace(traceName, transportManager, clock, appStateMonitor);
currentTime = expectedClientStartTime;
trace.start();
currentTime += expectedTraceDuration;
trace.stop();
verify(transportManager).log(argMetric.capture(), ArgumentMatchers.nullable(ApplicationProcessState.class));
TraceMetric traceMetric = argMetric.getValue();
assertThat(traceMetric.getName()).isEqualTo(traceName);
assertThat(traceMetric.getClientStartTimeUs()).isEqualTo(expectedClientStartTime);
assertThat(traceMetric.getDurationUs()).isEqualTo(expectedTraceDuration);
return traceMetric.toBuilder();
}
use of com.google.firebase.perf.metrics.Trace in project firebase-android-sdk by firebase.
the class PerformanceMonitoringTest method trace.
@Test
public void trace() {
Trace trace = FirebasePerformance.getInstance().newTrace("test_trace");
trace.start();
trace.putMetric("counter", 1);
trace.incrementMetric("counter", 2);
trace.putAttribute("is_test", "true");
trace.stop();
assertThat(trace.getLongMetric("counter")).isEqualTo(3);
assertThat(trace.getAttribute("is_test")).isEqualTo("true");
}
use of com.google.firebase.perf.metrics.Trace in project firebase-android-sdk by firebase.
the class AppStateMonitor method sendScreenTrace.
/**
* Sends the screen trace for the provided activity.
*
* @param activity activity object.
*/
private void sendScreenTrace(Activity activity) {
Trace screenTrace = activityToScreenTraceMap.get(activity);
if (screenTrace == null) {
return;
}
activityToScreenTraceMap.remove(activity);
Optional<PerfFrameMetrics> perfFrameMetrics = activityToRecorderMap.get(activity).stop();
if (!perfFrameMetrics.isAvailable()) {
logger.warn("Failed to record frame data for %s.", activity.getClass().getSimpleName());
return;
}
ScreenTraceUtil.addFrameCounters(screenTrace, perfFrameMetrics.get());
// Stop and record trace
screenTrace.stop();
}
use of com.google.firebase.perf.metrics.Trace in project firebase-android-sdk by firebase.
the class FragmentStateMonitor method onFragmentResumed.
@Override
public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
super.onFragmentResumed(fm, f);
// Start Fragment screen trace
logger.debug("FragmentMonitor %s.onFragmentResumed", f.getClass().getSimpleName());
Trace fragmentTrace = new Trace(getFragmentScreenTraceName(f), transportManager, clock, appStateMonitor);
fragmentTrace.start();
fragmentTrace.putAttribute(Constants.PARENT_FRAGMENT_ATTRIBUTE_KEY, f.getParentFragment() == null ? Constants.PARENT_FRAGMENT_ATTRIBUTE_VALUE_NONE : f.getParentFragment().getClass().getSimpleName());
if (f.getActivity() != null) {
fragmentTrace.putAttribute(Constants.ACTIVITY_ATTRIBUTE_KEY, f.getActivity().getClass().getSimpleName());
}
fragmentToTraceMap.put(f, fragmentTrace);
activityFramesRecorder.startFragment(f);
}
use of com.google.firebase.perf.metrics.Trace in project firebase-android-sdk by firebase.
the class FragmentStateMonitor method onFragmentPaused.
@Override
public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) {
super.onFragmentPaused(fm, f);
// Stop Fragment screen trace
logger.debug("FragmentMonitor %s.onFragmentPaused ", f.getClass().getSimpleName());
if (!fragmentToTraceMap.containsKey(f)) {
logger.warn("FragmentMonitor: missed a fragment trace from %s", f.getClass().getSimpleName());
return;
}
Trace fragmentTrace = fragmentToTraceMap.get(f);
fragmentToTraceMap.remove(f);
Optional<PerfFrameMetrics> frameMetricsData = activityFramesRecorder.stopFragment(f);
if (!frameMetricsData.isAvailable()) {
logger.warn("onFragmentPaused: recorder failed to trace %s", f.getClass().getSimpleName());
return;
}
ScreenTraceUtil.addFrameCounters(fragmentTrace, frameMetricsData.get());
fragmentTrace.stop();
}
Aggregations