Search in sources :

Example 11 with NetworkRequestMetric

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

the class RateLimiterTest method testRateLimit.

/**
 * Inside class RateLimiter, there is one RateLimiterImpl instance for TraceMetric object, and one
 * RateLimiterImpl instance for NetworkRequestMetric object, each RateLimiterImpl instance is a
 * token bucket. Two token buckets works separately
 */
@Test
public void testRateLimit() {
    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.
    RateLimiter limiter = new RateLimiter(TWO_TOKENS_PER_MINUTE, 2, mClock, 0.99f, 0, mockConfigResolver);
    PerfMetric metric = PerfMetric.getDefaultInstance();
    // if PerfMetric object has neither TraceMetric or NetworkRequestMetric field set, always return
    // true.
    assertThat(limiter.isEventRateLimited(metric)).isTrue();
    PerfMetric trace = PerfMetric.newBuilder().setTraceMetric(TraceMetric.getDefaultInstance()).build();
    PerfMetric network = PerfMetric.newBuilder().setNetworkRequestMetric(NetworkRequestMetric.getDefaultInstance()).build();
    // clock is 15 seconds, token count is 1.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 30 seconds, count is 0.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 45 seconds, count is 0.
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 60 seconds, count is 0
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 75 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isTrue();
    assertThat(limiter.isEventRateLimited(network)).isTrue();
    // clock is 90 seconds, count is 0
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 105 seconds, count is 0
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isTrue();
    assertThat(limiter.isEventRateLimited(network)).isTrue();
    // clock is 120 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
    // clock is 135 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isTrue();
    assertThat(limiter.isEventRateLimited(network)).isTrue();
    // clock is 150 seconds, count is 0,
    currentTime = currentTime.plusSeconds(15);
    assertThat(limiter.isEventRateLimited(trace)).isFalse();
    assertThat(limiter.isEventRateLimited(network)).isFalse();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) Test(org.junit.Test)

Example 12 with NetworkRequestMetric

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

the class TransportManagerTest method validNetworkMetric_transportNotInitialized_getLoggedAfterInitialization.

@Test
public void validNetworkMetric_transportNotInitialized_getLoggedAfterInitialization() {
    initializeTransport(false);
    NetworkRequestMetric validNetworkRequest = createValidNetworkRequestMetric();
    testTransportManager.log(validNetworkRequest, ApplicationProcessState.BACKGROUND);
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
    assertThat(testTransportManager.getPendingEventsQueue().size()).isEqualTo(1);
    initializeTransport(true);
    fakeExecutorService.runAll();
    PerfMetric loggedPerfMetric = getLastLoggedEvent(times(1));
    assertThat(loggedPerfMetric.getNetworkRequestMetric()).isEqualTo(validNetworkRequest);
    validateApplicationInfo(loggedPerfMetric, ApplicationProcessState.BACKGROUND);
    assertThat(testTransportManager.getPendingEventsQueue().isEmpty()).isTrue();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Test(org.junit.Test)

Example 13 with NetworkRequestMetric

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

the class TransportManagerTest method logNetworkRequests_fewNetworkRequestsAfterOtherCappedEventsAndTransportNotInitialized_cappedEventsDoesNotCauseOtherEventsToCap.

@Test
public void logNetworkRequests_fewNetworkRequestsAfterOtherCappedEventsAndTransportNotInitialized_cappedEventsDoesNotCauseOtherEventsToCap() {
    // 1. Transport is not initialized in the beginning
    initializeTransport(false);
    // 2. Log multiple Traces (any one of PerfMetric event other than NetworkRequestMetric) such
    // that they are capped
    // only 50 TraceMetric events are allowed to cache
    int maxTracesCacheSize = 50;
    int totalTraceEvents = maxTracesCacheSize + 10;
    for (int i = 0; i < totalTraceEvents; i++) {
        testTransportManager.log(createValidTraceMetric().toBuilder().setName("Trace - " + (i + 1)).build());
        fakeExecutorService.runAll();
        assertThat(getLastLoggedEvent(never())).isNull();
    }
    // 3. Even though we recorded "totalTraceEvents", events up-to "maxTracesCacheSize" are only
    // queued
    assertThat(testTransportManager.getPendingEventsQueue().size()).isEqualTo(maxTracesCacheSize);
    // 4. Log few Network Requests such that they are under the max cap
    // less than max cache for NetworkRequestMetric events
    int totalNetworkRequestEvents = 20;
    NetworkRequestMetric[] validNetworkRequests = new NetworkRequestMetric[totalNetworkRequestEvents];
    for (int i = 0; i < totalNetworkRequestEvents; i++) {
        validNetworkRequests[i] = createValidNetworkRequestMetric().toBuilder().setClientStartTimeUs(i + 1).build();
        testTransportManager.log(validNetworkRequests[i], ApplicationProcessState.FOREGROUND);
        fakeExecutorService.runAll();
        assertThat(getLastLoggedEvent(never())).isNull();
    }
    // 5. All NetworkRequests are queued even after Traces are capped
    assertThat(testTransportManager.getPendingEventsQueue().size()).isEqualTo(maxTracesCacheSize + totalNetworkRequestEvents);
    // 6. Initialize Transport
    initializeTransport(true);
    // 7. Consume all queued Traces
    for (int i = 0; i < maxTracesCacheSize; i++) {
        clearLastLoggedEvents();
        fakeExecutorService.runNext();
    }
    // 8. Consume all queued Network Requests and validate them
    for (int i = 0; i < totalNetworkRequestEvents; i++) {
        clearLastLoggedEvents();
        fakeExecutorService.runNext();
        PerfMetric loggedValidNetworkRequest = getLastLoggedEvent(times(1));
        assertThat(loggedValidNetworkRequest.getNetworkRequestMetric()).isEqualTo(validNetworkRequests[i]);
        validateApplicationInfo(loggedValidNetworkRequest, ApplicationProcessState.FOREGROUND);
    }
    // 9. Queue is all consumed
    assertThat(testTransportManager.getPendingEventsQueue().isEmpty()).isTrue();
    // 10. No pending events
    clearLastLoggedEvents();
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Test(org.junit.Test)

Example 14 with NetworkRequestMetric

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

the class TransportManagerTest method logMultipleNetworkRequests_transportNotInitialized_networkRequestsAfterMaxCapAreNotQueued.

@Test
public void logMultipleNetworkRequests_transportNotInitialized_networkRequestsAfterMaxCapAreNotQueued() {
    // 1. Transport is not initialized in the beginning
    initializeTransport(false);
    // 2. Log multiple Network Requests such that they are capped
    int maxNetworkRequestsCacheSize = // only 50 NetworkRequestMetric events are allowed to cache
    50;
    int totalNetworkRequestEvents = maxNetworkRequestsCacheSize + 10;
    NetworkRequestMetric[] validNetworkRequests = new NetworkRequestMetric[totalNetworkRequestEvents];
    for (int i = 0; i < totalNetworkRequestEvents; i++) {
        validNetworkRequests[i] = createValidNetworkRequestMetric().toBuilder().setClientStartTimeUs(i + 1).build();
        testTransportManager.log(validNetworkRequests[i], ApplicationProcessState.FOREGROUND);
        fakeExecutorService.runAll();
        assertThat(getLastLoggedEvent(never())).isNull();
    }
    // 3. Even though we recorded "totalNetworkRequestEvents", events up-to
    // "maxNetworkRequestsCacheSize" are only queued
    assertThat(testTransportManager.getPendingEventsQueue().size()).isEqualTo(maxNetworkRequestsCacheSize);
    // 4. Initialize Transport
    initializeTransport(true);
    // 5. Consume all queued Network Requests and validate them
    for (int i = 0; i < maxNetworkRequestsCacheSize; i++) {
        clearLastLoggedEvents();
        fakeExecutorService.runNext();
        PerfMetric loggedValidNetworkRequest = getLastLoggedEvent(times(1));
        assertThat(loggedValidNetworkRequest.getNetworkRequestMetric()).isEqualTo(validNetworkRequests[i]);
        validateApplicationInfo(loggedValidNetworkRequest, ApplicationProcessState.FOREGROUND);
    }
    // 6. Queue is all consumed after iterating "maxNetworkRequestsCacheSize" events
    assertThat(testTransportManager.getPendingEventsQueue().isEmpty()).isTrue();
    // 7. No pending events
    clearLastLoggedEvents();
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Test(org.junit.Test)

Example 15 with NetworkRequestMetric

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

the class TransportManagerTest method logMultipleEvents_transportNotInitialized_validEventsGetLoggedInOrderAfterInitialization.

@Test
public void logMultipleEvents_transportNotInitialized_validEventsGetLoggedInOrderAfterInitialization() {
    initializeTransport(false);
    TraceMetric validTrace = createValidTraceMetric();
    testTransportManager.log(validTrace, ApplicationProcessState.BACKGROUND);
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
    TraceMetric invalidTrace = createInvalidTraceMetric();
    testTransportManager.log(invalidTrace, ApplicationProcessState.BACKGROUND);
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
    NetworkRequestMetric validNetworkRequest = createValidNetworkRequestMetric();
    testTransportManager.log(validNetworkRequest, ApplicationProcessState.FOREGROUND);
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
    NetworkRequestMetric invalidNetworkRequest = createInvalidNetworkRequestMetric();
    testTransportManager.log(invalidNetworkRequest, ApplicationProcessState.FOREGROUND);
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
    GaugeMetric validGauge = createValidGaugeMetric();
    testTransportManager.log(validGauge);
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
    GaugeMetric invalidGauge = createInValidGaugeMetric();
    testTransportManager.log(invalidGauge);
    fakeExecutorService.runAll();
    assertThat(getLastLoggedEvent(never())).isNull();
    assertThat(testTransportManager.getPendingEventsQueue().size()).isEqualTo(6);
    initializeTransport(true);
    clearLastLoggedEvents();
    fakeExecutorService.runNext();
    PerfMetric loggedValidTrace = getLastLoggedEvent(times(1));
    assertThat(loggedValidTrace.getTraceMetric()).isEqualTo(validTrace);
    validateApplicationInfo(loggedValidTrace, ApplicationProcessState.BACKGROUND);
    clearLastLoggedEvents();
    fakeExecutorService.runNext();
    PerfMetric loggedInvalidTrace = getLastLoggedEvent(never());
    assertThat(loggedInvalidTrace).isNull();
    clearLastLoggedEvents();
    fakeExecutorService.runNext();
    PerfMetric loggedValidNetworkRequest = getLastLoggedEvent(times(1));
    assertThat(loggedValidNetworkRequest.getNetworkRequestMetric()).isEqualTo(validNetworkRequest);
    validateApplicationInfo(loggedValidNetworkRequest, ApplicationProcessState.FOREGROUND);
    clearLastLoggedEvents();
    fakeExecutorService.runNext();
    PerfMetric loggedInValidNetworkRequest = getLastLoggedEvent(never());
    assertThat(loggedInValidNetworkRequest).isNull();
    clearLastLoggedEvents();
    fakeExecutorService.runNext();
    PerfMetric loggedValidGauge = getLastLoggedEvent(times(1));
    assertThat(loggedValidGauge.getGaugeMetric()).isEqualTo(validGauge);
    validateApplicationInfo(loggedValidGauge, ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN);
    clearLastLoggedEvents();
    fakeExecutorService.runNext();
    PerfMetric loggedInValidGauge = getLastLoggedEvent(never());
    assertThat(loggedInValidGauge).isNull();
    assertThat(testTransportManager.getPendingEventsQueue().isEmpty()).isTrue();
}
Also used : TraceMetric(com.google.firebase.perf.v1.TraceMetric) PerfMetric(com.google.firebase.perf.v1.PerfMetric) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) GaugeMetric(com.google.firebase.perf.v1.GaugeMetric) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)81 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)80 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)45 NetworkRequestMetricBuilder (com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)13 IOException (java.io.IOException)13 HttpResponse (org.apache.http.HttpResponse)13 HttpClient (org.apache.http.client.HttpClient)12 HttpURLConnection (java.net.HttpURLConnection)11 PerfMetric (com.google.firebase.perf.v1.PerfMetric)10 HttpHost (org.apache.http.HttpHost)6 HttpRequest (org.apache.http.HttpRequest)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 HttpContext (org.apache.http.protocol.HttpContext)6 HttpUrl (okhttp3.HttpUrl)4 Request (okhttp3.Request)4 RequestBody (okhttp3.RequestBody)4 TransportManager (com.google.firebase.perf.transport.TransportManager)3 URLWrapper (com.google.firebase.perf.util.URLWrapper)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 Response (okhttp3.Response)3