Search in sources :

Example 11 with PerfMetric

use of com.google.firebase.perf.v1.PerfMetric 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 12 with PerfMetric

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

the class RateLimiterTest method testDeviceSampling_tracesEnabledButFragmentDisabled_dropsFragmentTrace.

@Test
public void testDeviceSampling_tracesEnabledButFragmentDisabled_dropsFragmentTrace() {
    makeConfigResolverReturnDefaultValues();
    when(mockConfigResolver.getTraceSamplingRate()).thenReturn(0.5f);
    when(mockConfigResolver.getFragmentSamplingRate()).thenReturn(0.02f);
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.49f, 0.49f, mockConfigResolver);
    assertThat(limiter.getIsDeviceAllowedToSendFragmentScreenTraces()).isFalse();
    assertThat(limiter.getIsDeviceAllowedToSendTraces()).isTrue();
    // Fragment trace should be dropped
    PerfMetric fragmentTrace = PerfMetric.newBuilder().setTraceMetric(TraceMetric.newBuilder().setName("_st_TestFragment").putCustomAttributes(Constants.ACTIVITY_ATTRIBUTE_KEY, "TestActivity").addAllPerfSessions(Arrays.asList(createNonVerbosePerfSessions()))).build();
    assertThat(limiter.isFragmentScreenTrace(fragmentTrace)).isTrue();
    assertThat(limiter.isEventSampled(fragmentTrace)).isFalse();
    // Non-fragment trace should be sampled
    PerfMetric activityTrace = PerfMetric.newBuilder().setTraceMetric(TraceMetric.newBuilder().setName("_st_TestActivity").addAllPerfSessions(Arrays.asList(createNonVerbosePerfSessions()))).build();
    assertThat(limiter.isFragmentScreenTrace(activityTrace)).isFalse();
    assertThat(limiter.isEventSampled(activityTrace)).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 13 with PerfMetric

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

the class RateLimiterTest method testDeviceSampling_bothTracesAndFragmentEnabled_acceptsFragmentTrace.

@Test
public void testDeviceSampling_bothTracesAndFragmentEnabled_acceptsFragmentTrace() {
    makeConfigResolverReturnDefaultValues();
    when(mockConfigResolver.getTraceSamplingRate()).thenReturn(0.5f);
    when(mockConfigResolver.getFragmentSamplingRate()).thenReturn(0.5f);
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.49f, 0.49f, mockConfigResolver);
    assertThat(limiter.getIsDeviceAllowedToSendTraces()).isTrue();
    assertThat(limiter.getIsDeviceAllowedToSendFragmentScreenTraces()).isTrue();
    PerfMetric trace = PerfMetric.newBuilder().setTraceMetric(TraceMetric.newBuilder().setName("_st_TestFragment").putCustomAttributes(Constants.ACTIVITY_ATTRIBUTE_KEY, "TestActivity").addAllPerfSessions(Arrays.asList(createNonVerbosePerfSessions()))).build();
    assertThat(limiter.isFragmentScreenTrace(trace)).isTrue();
    assertThat(limiter.isEventSampled(trace)).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 14 with PerfMetric

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

the class RateLimiterTest method testRateLimiterImplWithBursts_rateLessThanCapacity_doesNotAllowMoreThanCapacity.

@Test
public void testRateLimiterImplWithBursts_rateLessThanCapacity_doesNotAllowMoreThanCapacity() {
    makeConfigResolverReturnDefaultValues();
    // Make Config Resolver returns default value for resource sampling rate.
    when(mockConfigResolver.getTraceSamplingRate()).thenReturn(1.0f);
    when(mockConfigResolver.getNetworkRequestSamplingRate()).thenReturn(1.0f);
    // allow 3 logs per second. token bucket capacity is 4.
    RateLimiterImpl limiterImpl = new RateLimiterImpl(THREE_TOKENS_PER_SECOND, 4, mClock, mockConfigResolver, NETWORK, false);
    PerfMetric metric = PerfMetric.getDefaultInstance();
    // clock is 0, token count starts at 4, none should be replenished
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isFalse();
    assertThat(limiterImpl.check(metric)).isFalse();
    // clock is 1 second, 3 events should be allowed within the second
    currentTime = currentTime.plusSeconds(1);
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isFalse();
    // the first burst has finished, and there are 1 event per second for the next 3 seconds
    currentTime = currentTime.plusSeconds(1);
    assertThat(limiterImpl.check(metric)).isTrue();
    currentTime = currentTime.plusSeconds(1);
    assertThat(limiterImpl.check(metric)).isTrue();
    currentTime = currentTime.plusSeconds(1);
    assertThat(limiterImpl.check(metric)).isTrue();
    // after 10 seconds, the second burst is here, but capacity = 4 events are allowed
    currentTime = currentTime.plusSeconds(10);
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isFalse();
    assertThat(limiterImpl.check(metric)).isFalse();
    assertThat(limiterImpl.check(metric)).isFalse();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) RateLimiterImpl(com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl) Test(org.junit.Test)

Example 15 with PerfMetric

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

the class RateLimiterTest method testNetworkRequestsAreNotSampledWhenSessionIsVerboseAndSamplingEnabled.

@Test
public void testNetworkRequestsAreNotSampledWhenSessionIsVerboseAndSamplingEnabled() {
    makeConfigResolverReturnDefaultValues();
    when(mockConfigResolver.getNetworkRequestSamplingRate()).thenReturn(0.70f);
    // Passing a value for samplingBucketId which is greater than the sampling rate ensures that
    // the sampling will be enabled causing all the metrics to be dropped
    RateLimiter limiter = new RateLimiter(/* rate= */
    TWO_TOKENS_PER_SECOND, /* capacity= */
    2, mClock, /* samplingBucketId= */
    0.71f, /* fragmentBucketId= */
    0, mockConfigResolver);
    assertThat(limiter.getIsDeviceAllowedToSendNetworkEvents()).isFalse();
    PerfMetric network = PerfMetric.newBuilder().setNetworkRequestMetric(NetworkRequestMetric.newBuilder().addAllPerfSessions(Arrays.asList(createVerbosePerfSessions()))).build();
    assertThat(limiter.isEventSampled(network)).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Aggregations

PerfMetric (com.google.firebase.perf.v1.PerfMetric)51 Test (org.junit.Test)50 GaugeMetric (com.google.firebase.perf.v1.GaugeMetric)10 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)9 TraceMetric (com.google.firebase.perf.v1.TraceMetric)9 RateLimiterImpl (com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl)6 ArrayList (java.util.ArrayList)4 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)3 Clock (com.google.firebase.perf.util.Clock)2 PerfSession (com.google.firebase.perf.v1.PerfSession)2 WorkerThread (androidx.annotation.WorkerThread)1 AndroidMemoryReading (com.google.firebase.perf.v1.AndroidMemoryReading)1 CpuMetricReading (com.google.firebase.perf.v1.CpuMetricReading)1 GaugeMetadata (com.google.firebase.perf.v1.GaugeMetadata)1