Search in sources :

Example 21 with TraceMetric

use of com.google.firebase.perf.v1.TraceMetric in project firebase-android-sdk by firebase.

the class TraceMetricBuilderTest method testAddingCustomAttributes.

@Test
public void testAddingCustomAttributes() {
    Trace trace = new Trace(TRACE_1, transportManager, clock, appStateMonitor);
    currentTime = 1;
    trace.start();
    currentTime = 2;
    trace.startStage(TRACE_2);
    trace.putAttribute(TRACE_ATTRIBUTE_KEY, TRACE_ATTRIBUTE_VALUE);
    currentTime = 3;
    trace.stop();
    TraceMetric traceMetric = new TraceMetricBuilder(trace).build();
    Assert.assertEquals(TRACE_1, traceMetric.getName());
    Assert.assertEquals(1, traceMetric.getCustomAttributesCount());
    Assert.assertEquals(TRACE_ATTRIBUTE_VALUE, trace.getAttribute(TRACE_ATTRIBUTE_KEY));
}
Also used : TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Example 22 with TraceMetric

use of com.google.firebase.perf.v1.TraceMetric in project firebase-android-sdk by firebase.

the class TraceMetricBuilderTest method testRemovingCustomAttributes.

@Test
public void testRemovingCustomAttributes() {
    Trace trace = new Trace(TRACE_1, transportManager, clock, appStateMonitor);
    currentTime = 1;
    trace.start();
    currentTime = 2;
    trace.startStage(TRACE_2);
    trace.putAttribute(TRACE_ATTRIBUTE_KEY, TRACE_ATTRIBUTE_VALUE);
    currentTime = 3;
    trace.removeAttribute(TRACE_ATTRIBUTE_KEY);
    trace.stop();
    TraceMetric traceMetric = new TraceMetricBuilder(trace).build();
    Assert.assertEquals(TRACE_1, traceMetric.getName());
    Assert.assertEquals(0, traceMetric.getCustomAttributesCount());
}
Also used : TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Example 23 with TraceMetric

use of com.google.firebase.perf.v1.TraceMetric in project firebase-android-sdk by firebase.

the class TraceMetricBuilderTest method testAddingCustomAttributesBeforeStartAfterStop.

@Test
public void testAddingCustomAttributesBeforeStartAfterStop() {
    String beforeStart = "beforeStart";
    String afterStart = "afterStart";
    String afterStop = "afterStop";
    Trace trace = new Trace(TRACE_1, transportManager, clock, appStateMonitor);
    currentTime = 1;
    trace.start();
    currentTime = 2;
    trace.putAttribute(TRACE_ATTRIBUTE_KEY + beforeStart, TRACE_ATTRIBUTE_VALUE + beforeStart);
    trace.startStage(TRACE_2);
    trace.putAttribute(TRACE_ATTRIBUTE_KEY + afterStart, TRACE_ATTRIBUTE_VALUE + afterStart);
    currentTime = 3;
    trace.stop();
    trace.putAttribute(TRACE_ATTRIBUTE_KEY + afterStop, TRACE_ATTRIBUTE_VALUE + afterStop);
    TraceMetric traceMetric = new TraceMetricBuilder(trace).build();
    Assert.assertEquals(TRACE_1, traceMetric.getName());
    Assert.assertEquals(2, traceMetric.getCustomAttributesCount());
    Assert.assertEquals(TRACE_ATTRIBUTE_VALUE + beforeStart, trace.getAttribute(TRACE_ATTRIBUTE_KEY + beforeStart));
    Assert.assertEquals(TRACE_ATTRIBUTE_VALUE + afterStart, trace.getAttribute(TRACE_ATTRIBUTE_KEY + afterStart));
    Assert.assertNull(trace.getAttribute(TRACE_ATTRIBUTE_KEY + afterStop));
}
Also used : TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Example 24 with TraceMetric

use of com.google.firebase.perf.v1.TraceMetric in project firebase-android-sdk by firebase.

the class AppStartTraceTest method verifyFinalState.

private void verifyFinalState(Activity activity, AppStartTrace trace, long createTime, long startTime, long resumeTime) {
    Assert.assertEquals(activity, trace.getAppStartActivity());
    Assert.assertEquals(createTime, trace.getOnCreateTime().getMicros());
    Assert.assertEquals(startTime, trace.getOnStartTime().getMicros());
    Assert.assertEquals(resumeTime, trace.getOnResumeTime().getMicros());
    verify(transportManager, times(1)).log(traceArgumentCaptor.capture(), ArgumentMatchers.nullable(ApplicationProcessState.class));
    TraceMetric metric = traceArgumentCaptor.getValue();
    Assert.assertEquals(Constants.TraceNames.APP_START_TRACE_NAME.toString(), metric.getName());
    Assert.assertEquals(appStartTime, metric.getClientStartTimeUs());
    Assert.assertEquals(resumeTime - appStartHRT, metric.getDurationUs());
    Assert.assertEquals(3, metric.getSubtracesCount());
    Assert.assertEquals(Constants.TraceNames.ON_CREATE_TRACE_NAME.toString(), metric.getSubtraces(0).getName());
    Assert.assertEquals(appStartTime, metric.getSubtraces(0).getClientStartTimeUs());
    Assert.assertEquals(createTime - appStartHRT, metric.getSubtraces(0).getDurationUs());
    Assert.assertEquals(Constants.TraceNames.ON_START_TRACE_NAME.toString(), metric.getSubtraces(1).getName());
    Assert.assertEquals(createTime, metric.getSubtraces(1).getClientStartTimeUs());
    Assert.assertEquals(startTime - createTime, metric.getSubtraces(1).getDurationUs());
    Assert.assertEquals(Constants.TraceNames.ON_RESUME_TRACE_NAME.toString(), metric.getSubtraces(2).getName());
    Assert.assertEquals(startTime, metric.getSubtraces(2).getClientStartTimeUs());
    Assert.assertEquals(resumeTime - startTime, metric.getSubtraces(2).getDurationUs());
    Assert.assertEquals(1, metric.getPerfSessionsCount());
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) TraceMetric(com.google.firebase.perf.v1.TraceMetric)

Example 25 with TraceMetric

use of com.google.firebase.perf.v1.TraceMetric in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method testTwoActivities.

@Test
public void testTwoActivities() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    // activity1 comes to foreground.
    currentTime = 1;
    monitor.onActivityResumed(activity1);
    Assert.assertEquals(currentTime, monitor.getResumeTime().getMicros());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), nullable(ApplicationProcessState.class));
    currentTime = 2;
    monitor.onActivityResumed(activity2);
    // second activity becomes visible does not change resumeTime.
    Assert.assertEquals(1, monitor.getResumeTime().getMicros());
    // two activities visible.
    Assert.assertEquals(2, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // activity1 goes to background.
    currentTime = 3;
    monitor.onActivityStopped(activity1);
    Assert.assertNull(monitor.getPauseTime());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // activity2 goes to background.
    currentTime = 4;
    monitor.onActivityStopped(activity2);
    // pauseTime updated.
    Assert.assertEquals(4, monitor.getPauseTime().getMicros());
    // no activity visible.
    Assert.assertEquals(0, monitor.getResumed().size());
    Assert.assertFalse(monitor.isForeground());
    // send foreground trace log.
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    TraceMetric metric = argTraceMetric.getValue();
    Assert.assertEquals(Constants.TraceNames.FOREGROUND_TRACE_NAME.toString(), metric.getName());
    Assert.assertEquals(monitor.getResumeTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getResumeTime().getDurationMicros(monitor.getPauseTime()), metric.getDurationUs());
    // activity1 goes to foreground.
    currentTime = 5;
    monitor.onActivityResumed(activity1);
    // resumeTime updated.
    Assert.assertEquals(currentTime, monitor.getResumeTime().getMicros());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    // send background trace.
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    metric = argTraceMetric.getValue();
    Assert.assertEquals(Constants.TraceNames.BACKGROUND_TRACE_NAME.toString(), metric.getName());
    Assert.assertEquals(monitor.getPauseTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getPauseTime().getDurationMicros(monitor.getResumeTime()), metric.getDurationUs());
    // activity2 goes to foreground.
    currentTime = 6;
    monitor.onActivityResumed(activity2);
    // resumeTime does not change because this is second activity becomes visible.
    Assert.assertEquals(5, monitor.getResumeTime().getMicros());
    // two activities are visible.
    Assert.assertEquals(2, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    // no new event log.
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)44 TraceMetric (com.google.firebase.perf.v1.TraceMetric)43 PerfMetric (com.google.firebase.perf.v1.PerfMetric)11 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)7 Trace (com.google.firebase.perf.metrics.Trace)2 GaugeMetric (com.google.firebase.perf.v1.GaugeMetric)2 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)2 PerfSession (com.google.firebase.perf.v1.PerfSession)2 Fragment (androidx.fragment.app.Fragment)1 AppStateMonitor (com.google.firebase.perf.application.AppStateMonitor)1 TransportManager (com.google.firebase.perf.transport.TransportManager)1