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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations