Search in sources :

Example 36 with PerfMetric

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

Example 37 with PerfMetric

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

Example 38 with PerfMetric

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

Example 39 with PerfMetric

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

Example 40 with PerfMetric

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

Aggregations

PerfMetric (com.google.firebase.perf.v1.PerfMetric)51 Test (org.junit.Test)50 GaugeMetric (com.google.firebase.perf.v1.GaugeMetric)10 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)9 TraceMetric (com.google.firebase.perf.v1.TraceMetric)9 RateLimiterImpl (com.google.firebase.perf.transport.RateLimiter.RateLimiterImpl)6 ArrayList (java.util.ArrayList)4 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)3 Clock (com.google.firebase.perf.util.Clock)2 PerfSession (com.google.firebase.perf.v1.PerfSession)2 WorkerThread (androidx.annotation.WorkerThread)1 AndroidMemoryReading (com.google.firebase.perf.v1.AndroidMemoryReading)1 CpuMetricReading (com.google.firebase.perf.v1.CpuMetricReading)1 GaugeMetadata (com.google.firebase.perf.v1.GaugeMetadata)1