Search in sources :

Example 1 with PerfMetric

use of com.google.firebase.perf.v1.PerfMetric in project firebase-android-sdk by firebase.

the class TransportManager method syncLog.

// endregion
// region Transport Private APIs
@WorkerThread
private void syncLog(PerfMetric.Builder perfMetricBuilder, ApplicationProcessState appState) {
    if (!isInitialized()) {
        if (isAllowedToCache(perfMetricBuilder)) {
            logger.debug("Transport is not initialized yet, %s will be queued for to be dispatched later", getLogcatMsg(perfMetricBuilder));
            pendingEventsQueue.add(new PendingPerfEvent(perfMetricBuilder, appState));
        }
        return;
    }
    PerfMetric perfMetric = setApplicationInfoAndBuild(perfMetricBuilder, appState);
    if (isAllowedToDispatch(perfMetric)) {
        dispatchLog(perfMetric);
        // TODO(b/172008005): This might not be the best place for this call, consider utilizing a
        // callback in the SessionManager itself.
        SessionManager.getInstance().updatePerfSessionIfExpired();
    }
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) WorkerThread(androidx.annotation.WorkerThread)

Example 2 with PerfMetric

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

Example 3 with PerfMetric

use of com.google.firebase.perf.v1.PerfMetric in project firebase-android-sdk by firebase.

the class RateLimiterTest method testGaugesAreNeverSampled.

@Test
public void testGaugesAreNeverSampled() {
    makeConfigResolverReturnDefaultValues();
    when(mockConfigResolver.getTraceSamplingRate()).thenReturn(0.70f);
    when(mockConfigResolver.getNetworkRequestSamplingRate()).thenReturn(0.70f);
    when(mockConfigResolver.getFragmentSamplingRate()).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.71f, mockConfigResolver);
    assertThat(limiter.getIsDeviceAllowedToSendTraces()).isFalse();
    assertThat(limiter.getIsDeviceAllowedToSendNetworkEvents()).isFalse();
    PerfMetric gauge = PerfMetric.newBuilder().setGaugeMetric(GaugeMetric.getDefaultInstance()).build();
    assertThat(limiter.isEventSampled(gauge)).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 4 with PerfMetric

use of com.google.firebase.perf.v1.PerfMetric in project firebase-android-sdk by firebase.

the class RateLimiterTest method testGaugeMetricIsNotRateLimitApplicable.

@Test
public void testGaugeMetricIsNotRateLimitApplicable() {
    makeConfigResolverReturnDefaultValues();
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.99f, 0.99f, mockConfigResolver);
    PerfMetric metric = PerfMetric.newBuilder().setGaugeMetric(GaugeMetric.getDefaultInstance()).build();
    assertThat(limiter.isRateLimitApplicable(metric)).isFalse();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 5 with PerfMetric

use of com.google.firebase.perf.v1.PerfMetric in project firebase-android-sdk by firebase.

the class RateLimiterTest method testDeviceSampling_tracesDisabledButFragmentEnabled_dropsFragmentTrace.

@Test
public void testDeviceSampling_tracesDisabledButFragmentEnabled_dropsFragmentTrace() {
    makeConfigResolverReturnDefaultValues();
    when(mockConfigResolver.getTraceSamplingRate()).thenReturn(0.02f);
    when(mockConfigResolver.getFragmentSamplingRate()).thenReturn(0.5f);
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.49f, 0.49f, mockConfigResolver);
    assertThat(limiter.getIsDeviceAllowedToSendFragmentScreenTraces()).isTrue();
    assertThat(limiter.getIsDeviceAllowedToSendTraces()).isFalse();
    // All traces including fragment trace should be dropped
    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)).isFalse();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) 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