use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl in project firebase-android-sdk by firebase.
the class RateLimiterTest method getRateFromNetworkParams_defaultValues_worksCorrectly.
@Test
public void getRateFromNetworkParams_defaultValues_worksCorrectly() {
makeConfigResolverReturnDefaultValues();
RateLimiterImpl limiterImpl = new RateLimiterImpl(/* rate= */
TWO_TOKENS_PER_SECOND, /* capacity= */
2, mClock, mockConfigResolver, NETWORK, /* isLogcatEnabled= */
false);
assertThat(limiterImpl.getRate().getTokensPerSeconds()).isEqualTo(2.0);
assertThat(limiterImpl.getForegroundRate().getTokensPerSeconds()).isEqualTo(700.0d / 600);
assertThat(limiterImpl.getForegroundCapacity()).isEqualTo(700L);
assertThat(limiterImpl.getBackgroundRate().getTokensPerSeconds()).isEqualTo(70.0d / 600);
assertThat(limiterImpl.getBackgroundCapacity()).isEqualTo(70L);
}
use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl in project firebase-android-sdk by firebase.
the class RateLimiterTest method testRateLimiterImplWithLongTimeGapBetweenEvents_doesNotAccumulateTokensOrCauseBurst.
@Test
public void testRateLimiterImplWithLongTimeGapBetweenEvents_doesNotAccumulateTokensOrCauseBurst() {
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 660 seconds, count before check is 0, 2 new tokens added, count after check is 1
currentTime = currentTime.plusSeconds(600);
assertThat(limiterImpl.check(metric)).isTrue();
// clock is 661 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 662 seconds, count before check is 0, 0 new tokens added, count after check is 0
currentTime = currentTime.plusSeconds(1);
assertThat(limiterImpl.check(metric)).isFalse();
// clock is 663 seconds, count before check is 0, 0 new tokens added, count after check is 0
currentTime = currentTime.plusSeconds(1);
assertThat(limiterImpl.check(metric)).isFalse();
}
use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl in project firebase-android-sdk by firebase.
the class RateLimiterTest method getRateFromNetworkParams_nonDefaultValues_worksCorrectly.
@Test
public void getRateFromNetworkParams_nonDefaultValues_worksCorrectly() {
makeConfigResolverReturnDefaultValues();
when(mockConfigResolver.getNetworkEventCountBackground()).thenReturn(60L);
when(mockConfigResolver.getNetworkEventCountForeground()).thenReturn(700L);
when(mockConfigResolver.getRateLimitSec()).thenReturn(60L);
when(mockConfigResolver.getRateLimitSec()).thenReturn(60L);
RateLimiterImpl limiterImpl = new RateLimiterImpl(/* rate= */
TWO_TOKENS_PER_SECOND, /* capacity= */
2, mClock, mockConfigResolver, NETWORK, /* isLogcatEnabled= */
false);
assertThat(limiterImpl.getRate().getTokensPerSeconds()).isEqualTo(2.0);
assertThat(limiterImpl.getForegroundRate().getTokensPerSeconds()).isEqualTo(700.0d / 60L);
assertThat(limiterImpl.getForegroundCapacity()).isEqualTo(700L);
assertThat(limiterImpl.getBackgroundRate().getTokensPerSeconds()).isEqualTo(60.0d / 60L);
assertThat(limiterImpl.getBackgroundCapacity()).isEqualTo(60L);
}
use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl in project firebase-android-sdk by firebase.
the class RateLimiterTest method getRateFromTraceParams_defaultValues_worksCorrectly.
@Test
public void getRateFromTraceParams_defaultValues_worksCorrectly() {
makeConfigResolverReturnDefaultValues();
RateLimiterImpl limiterImpl = new RateLimiterImpl(/* rate= */
TWO_TOKENS_PER_SECOND, /* capacity= */
2, mClock, mockConfigResolver, TRACE, /* isLogcatEnabled= */
false);
assertThat(limiterImpl.getRate().getTokensPerSeconds()).isEqualTo(2.0);
assertThat(limiterImpl.getForegroundRate().getTokensPerSeconds()).isEqualTo(300.0d / 600);
assertThat(limiterImpl.getForegroundCapacity()).isEqualTo(300L);
assertThat(limiterImpl.getBackgroundRate().getTokensPerSeconds()).isEqualTo(30.0d / 600);
assertThat(limiterImpl.getBackgroundCapacity()).isEqualTo(30L);
}
use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl 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();
}
Aggregations