Search in sources :

Example 31 with PerfMetric

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

the class RateLimiterTest method testRateLimitImpl.

/**
 * A simple test case for Token Bucket algorithm.
 */
@Test
public void testRateLimitImpl() {
    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.
    RateLimiterImpl limiterImpl = new RateLimiterImpl(TWO_TOKENS_PER_MINUTE, 2, mClock, mockConfigResolver, NETWORK, false);
    PerfMetric metric = PerfMetric.getDefaultInstance();
    // clock is 15 seconds, token count is 1.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isTrue();
    // clock is 30 seconds, count is 1.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isTrue();
    // clock is 45 seconds, count is 0.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isTrue();
    // clock is 60 seconds, count is 1
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isTrue();
    // clock is 75 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isFalse();
    // clock is 90 seconds, count is 1
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isTrue();
    // clock is 105 seconds, count is 0
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isFalse();
    // clock is 120 seconds, count is 1,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isTrue();
    // clock is 135 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isFalse();
    // clock is 150 seconds, count is 1,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiterImpl.check(metric)).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) RateLimiterImpl(com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl) Test(org.junit.Test)

Example 32 with PerfMetric

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

the class RateLimiterTest method testNetworkRequestMetricIsRateLimitApplicable.

@Test
public void testNetworkRequestMetricIsRateLimitApplicable() {
    makeConfigResolverReturnDefaultValues();
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.99f, 0.99f, mockConfigResolver);
    PerfMetric metric = PerfMetric.newBuilder().setNetworkRequestMetric(NetworkRequestMetric.getDefaultInstance()).build();
    assertThat(limiter.isRateLimitApplicable(metric)).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 33 with PerfMetric

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

the class RateLimiterTest method testTraceMetricNoSpecialNameIsRateLimitApplicable.

@Test
public void testTraceMetricNoSpecialNameIsRateLimitApplicable() {
    makeConfigResolverReturnDefaultValues();
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.99f, 0.99f, mockConfigResolver);
    PerfMetric metric = PerfMetric.newBuilder().setTraceMetric(TraceMetric.newBuilder().setName("devInstrumentedTrace")).build();
    assertThat(limiter.isRateLimitApplicable(metric)).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 34 with PerfMetric

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

the class RateLimiterTest method testRateLimiterImplWithBursts_rateMoreThanCapacity_doesNotAllowMoreThanCapacity.

@Test
public void testRateLimiterImplWithBursts_rateMoreThanCapacity_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 2.
    RateLimiterImpl limiterImpl = new RateLimiterImpl(THREE_TOKENS_PER_SECOND, 2, mClock, mockConfigResolver, NETWORK, false);
    PerfMetric metric = PerfMetric.getDefaultInstance();
    // clock is 0, token count starts at 2, none should be replenished
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isFalse();
    assertThat(limiterImpl.check(metric)).isFalse();
    // clock is 1 second, 2 events should be allowed within the second due to capacity cap
    currentTime = currentTime.plusSeconds(1);
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isFalse();
    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();
    // the second burst is here, but only capacity = 2 events are allowed
    currentTime = currentTime.plusSeconds(10);
    assertThat(limiterImpl.check(metric)).isTrue();
    assertThat(limiterImpl.check(metric)).isTrue();
    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 35 with PerfMetric

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

the class RateLimiterTest method isEventSampled_fragmentWithVerboseSessionDisabled_returnsFalse.

@Test
public void isEventSampled_fragmentWithVerboseSessionDisabled_returnsFalse() {
    makeConfigResolverReturnDefaultValues();
    when(mockConfigResolver.getFragmentSamplingRate()).thenReturn(0.70f);
    when(mockConfigResolver.getTraceSamplingRate()).thenReturn(1.0f);
    // Passing a value for samplingBucketId which is greater than the sampling rate means that
    // the sampling dice roll failed causing all the metrics to be dropped
    RateLimiter limiter = new RateLimiter(/* rate= */
    TWO_TOKENS_PER_SECOND, /* capacity= */
    2, mClock, /* samplingBucketId= */
    0, /* fragmentBucketId= */
    0.71f, mockConfigResolver);
    assertThat(limiter.getIsDeviceAllowedToSendTraces()).isTrue();
    assertThat(limiter.getIsDeviceAllowedToSendFragmentScreenTraces()).isFalse();
    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)).isFalse();
}
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