use of com.google.firebase.perf.v1.PerfMetric in project firebase-android-sdk by firebase.
the class RateLimiterTest method testBackgroundTraceWithCountersIsNotRateLimitApplicable.
@Test
public void testBackgroundTraceWithCountersIsNotRateLimitApplicable() {
makeConfigResolverReturnDefaultValues();
RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.99f, 0.99f, mockConfigResolver);
PerfMetric metric = PerfMetric.newBuilder().setTraceMetric(TraceMetric.newBuilder().putCounters("counter1", 10).setName(Constants.TraceNames.BACKGROUND_TRACE_NAME.toString())).build();
assertThat(limiter.isRateLimitApplicable(metric)).isFalse();
}
use of com.google.firebase.perf.v1.PerfMetric in project firebase-android-sdk by firebase.
the class RateLimiterTest method testChangeRate.
/**
* Initial rate is 2/minute, then increase to 4/minute. Compare to test case testRateLimit(), more
* logs are allowed.
*/
@Test
public void testChangeRate() {
makeConfigResolverReturnDefaultValues();
// 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, TRACE, 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 0.
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 0
currentTime = currentTime.plusSeconds(15);
assertThat(limiterImpl.check(metric)).isTrue();
// change rate to 4/minute
limiterImpl.setRate(FOUR_TOKENS_PER_MINUTE);
// clock is 75 seconds, count is 0,
currentTime = currentTime.plusSeconds(15);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 90 seconds, count is 0
currentTime = currentTime.plusSeconds(15);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 105 seconds, count is 0
currentTime = currentTime.plusSeconds(15);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 120 seconds, count is 0,
currentTime = currentTime.plusSeconds(15);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 135 seconds, count is 0,
currentTime = currentTime.plusSeconds(15);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 150 seconds, count is 0,
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 testBackgroundTraceWithoutCountersIsRateLimitApplicable.
@Test
public void testBackgroundTraceWithoutCountersIsRateLimitApplicable() {
makeConfigResolverReturnDefaultValues();
RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.99f, 0.99f, mockConfigResolver);
PerfMetric metric = PerfMetric.newBuilder().setTraceMetric(TraceMetric.newBuilder().setName(Constants.TraceNames.BACKGROUND_TRACE_NAME.toString())).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 testTracesAreSampledWhenSessionIsNonVerboseAndSamplingEnabled.
@Test
public void testTracesAreSampledWhenSessionIsNonVerboseAndSamplingEnabled() {
makeConfigResolverReturnDefaultValues();
when(mockConfigResolver.getTraceSamplingRate()).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.getIsDeviceAllowedToSendTraces()).isFalse();
PerfMetric trace = PerfMetric.newBuilder().setTraceMetric(TraceMetric.newBuilder().addAllPerfSessions(Arrays.asList(createNonVerbosePerfSessions()))).build();
assertThat(limiter.isEventSampled(trace)).isFalse();
}
use of com.google.firebase.perf.v1.PerfMetric in project firebase-android-sdk by firebase.
the class RateLimiterTest method testRateLimiterImplWithIrregularTimeIntervals.
/**
* An edge test case for Token Bucket algorithm.
*/
@Test
public void testRateLimiterImplWithIrregularTimeIntervals() {
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 20 seconds, count before check is 2, 0 new tokens added, count after check is 1
currentTime = currentTime.plusSeconds(20);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 40 seconds, count before check is 1, 1 new tokens added, count after check is 1
currentTime = currentTime.plusSeconds(20);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 59 seconds, count before check is 1, 0 new tokens added, count after check is 0
currentTime = currentTime.plusSeconds(19);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 60 seconds, count before check is 0, 1 new tokens added, count after check is 0
currentTime = currentTime.plusSeconds(1);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 80 seconds, count before check is 0, 0 new tokens added, count after check is 0
currentTime = currentTime.plusSeconds(20);
assertThat(limiterImpl.check(metric)).isFalse();
// clock is 130 seconds, count before check is 0, 2 new tokens added, count after check is 1
currentTime = currentTime.plusSeconds(50);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 131 seconds, count before check is 1, 0 new tokens added, count after check is 0
currentTime = currentTime.plusSeconds(1);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 132 seconds, count before check is 0, 0 new tokens added, count after check is 0
currentTime = currentTime.plusSeconds(1);
assertThat(limiterImpl.check(metric)).isFalse();
}
Aggregations