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