Search in sources :

Example 1 with TraceMetric

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

the class AppStateMonitorTest method foregroundBackgroundEvent_activityStateChanges_fgBgEventsCreated.

@Test
public void foregroundBackgroundEvent_activityStateChanges_fgBgEventsCreated() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    // activity1 comes to foreground.
    currentTime = 1;
    monitor.incrementCount("counter1", 10);
    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));
    // activity1 goes to background.
    currentTime = 2;
    monitor.incrementCount("counter2", 20);
    monitor.onActivityStopped(activity1);
    Assert.assertEquals(currentTime, monitor.getPauseTime().getMicros());
    Assert.assertEquals(0, monitor.getResumed().size());
    Assert.assertFalse(monitor.isForeground());
    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(2, metric.getCountersCount());
    Assert.assertEquals(1, metric.getPerfSessionsCount());
    Map<String, Long> counters = metric.getCountersMap();
    Assert.assertEquals(10, (long) counters.get("counter1"));
    Assert.assertEquals(20, (long) counters.get("counter2"));
    Assert.assertEquals(monitor.getResumeTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getResumeTime().getDurationMicros(monitor.getPauseTime()), metric.getDurationUs());
    // Verify bug 36457047 fix, onActivityStopped() is called twice.
    monitor.onActivityStopped(activity1);
    Assert.assertEquals(2, monitor.getPauseTime().getMicros());
    Assert.assertEquals(0, monitor.getResumed().size());
    Assert.assertFalse(monitor.isForeground());
    // log() should NOT be called again on second onActivityStopped() call.
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // activity1 goes to foreground.
    currentTime = 3;
    monitor.incrementCount("counter3", 30);
    monitor.onActivityResumed(activity1);
    Assert.assertEquals(currentTime, monitor.getResumeTime().getMicros());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    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(1, metric.getCountersCount());
    assertThat(metric.getCountersMap()).containsEntry("counter3", 30L);
    Assert.assertEquals(monitor.getPauseTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getPauseTime().getDurationMicros(monitor.getResumeTime()), metric.getDurationUs());
    Assert.assertEquals(1, metric.getPerfSessionsCount());
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Example 2 with TraceMetric

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

the class AppStateMonitorTest method testIncrementCount.

@Test
public void testIncrementCount() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    monitor.incrementCount("counter1", 10);
    monitor.incrementCount("counter2", 20);
    monitor.incrementCount("counter2", 2);
    // Resume and then stop activity to trigger logging of the trace metric
    monitor.onActivityResumed(activity1);
    monitor.onActivityStopped(activity1);
    verify(transportManager).log(argTraceMetric.capture(), nullable(ApplicationProcessState.class));
    TraceMetric metric = argTraceMetric.getValue();
    Map<String, Long> counters = metric.getCountersMap();
    Assert.assertEquals(10, (long) counters.get("counter1"));
    Assert.assertEquals(22, (long) counters.get("counter2"));
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Example 3 with TraceMetric

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

the class RateLimiterTest method createVerbosePerfSessions.

// TODO(b/118844549): Migrate to lists when TraceMetric is migrated.
private PerfSession[] createVerbosePerfSessions() {
    PerfSession[] perfSessions = new PerfSession[1];
    PerfSession perfSessions1 = PerfSession.newBuilder().setSessionId("abcdefg").addSessionVerbosity(SessionVerbosity.GAUGES_AND_SYSTEM_EVENTS).build();
    perfSessions[0] = perfSessions1;
    return perfSessions;
}
Also used : PerfSession(com.google.firebase.perf.v1.PerfSession)

Example 4 with TraceMetric

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

the class RateLimiterTest method testRateLimit.

/**
 * Inside class RateLimiter, there is one RateLimiterImpl instance for TraceMetric object, and one
 * RateLimiterImpl instance for NetworkRequestMetric object, each RateLimiterImpl instance is a
 * token bucket. Two token buckets works separately
 */
@Test
public void testRateLimit() {
    makeConfigResolverReturnDefaultValues();
    // Make Config Resolver returns default value for resource sampling rate.
    when(mockConfigResolver.getTraceSamplingRate()).thenReturn(1.0f);
    when(mockConfigResolver.getNetworkRequestSamplingRate()).thenReturn(1.0f);
    // allow 2 logs every minute. token bucket capacity is 2.
    // clock is 0, token count is 2.
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.99f, 0, mockConfigResolver);
    PerfMetric metric = PerfMetric.getDefaultInstance();
    // if PerfMetric object has neither TraceMetric or NetworkRequestMetric field set, always return
    // true.
    assertThat(limiter.isEventRateLimited(metric)).isTrue();
    PerfMetric trace = PerfMetric.newBuilder().setTraceMetric(TraceMetric.getDefaultInstance()).build();
    PerfMetric network = PerfMetric.newBuilder().setNetworkRequestMetric(NetworkRequestMetric.getDefaultInstance()).build();
    // clock is 15 seconds, token count is 1.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 30 seconds, count is 0.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 45 seconds, count is 0.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 60 seconds, count is 0
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 75 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isTrue();
    assertThat(limiter.isEventRateLimited(network)).isTrue();
    // clock is 90 seconds, count is 0
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 105 seconds, count is 0
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isTrue();
    assertThat(limiter.isEventRateLimited(network)).isTrue();
    // clock is 120 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 135 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isTrue();
    assertThat(limiter.isEventRateLimited(network)).isTrue();
    // clock is 150 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 5 with TraceMetric

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

the class RateLimiterTest method createNonVerbosePerfSessions.

// TODO(b/118844549): Migrate to lists when TraceMetric is migrated.
private PerfSession[] createNonVerbosePerfSessions() {
    PerfSession[] perfSessions = new PerfSession[1];
    PerfSession perfSessions1 = PerfSession.newBuilder().setSessionId("abcdefg").build();
    perfSessions[0] = perfSessions1;
    return perfSessions;
}
Also used : PerfSession(com.google.firebase.perf.v1.PerfSession)

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