Search in sources :

Example 6 with RateLimiterImpl

use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl 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 7 with RateLimiterImpl

use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl 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 8 with RateLimiterImpl

use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl 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();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) RateLimiterImpl(com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl) Test(org.junit.Test)

Example 9 with RateLimiterImpl

use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl in project firebase-android-sdk by firebase.

the class RateLimiterTest method getRateFromTraceParams_nonDefaultValues_worksCorrectly.

@Test
public void getRateFromTraceParams_nonDefaultValues_worksCorrectly() {
    makeConfigResolverReturnDefaultValues();
    when(mockConfigResolver.getTraceEventCountBackground()).thenReturn(60L);
    when(mockConfigResolver.getTraceEventCountForeground()).thenReturn(600L);
    when(mockConfigResolver.getRateLimitSec()).thenReturn(60L);
    when(mockConfigResolver.getRateLimitSec()).thenReturn(60L);
    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(600.0d / 60L);
    assertThat(limiterImpl.getForegroundCapacity()).isEqualTo(600L);
    assertThat(limiterImpl.getBackgroundRate().getTokensPerSeconds()).isEqualTo(60.0d / 60L);
    assertThat(limiterImpl.getBackgroundCapacity()).isEqualTo(60L);
}
Also used : RateLimiterImpl(com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl) Test(org.junit.Test)

Example 10 with RateLimiterImpl

use of com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl 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();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) RateLimiterImpl(com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl) Test(org.junit.Test)

Aggregations

RateLimiterImpl (com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl)10 Test (org.junit.Test)10 PerfMetric (com.google.firebase.perf.v1.PerfMetric)6