Search in sources :

Example 1 with NetworkRequestMetric

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

the class NetworkRequestMetricBuilder method build.

/**
 * Builds the current {@link NetworkRequestMetric}.
 */
public NetworkRequestMetric build() {
    SessionManager.getInstance().unregisterForSessionUpdates(weakReference);
    unregisterForAppState();
    com.google.firebase.perf.v1.PerfSession[] perfSessions = PerfSession.buildAndSort(getSessions());
    if (perfSessions != null) {
        builder.addAllPerfSessions(Arrays.asList(perfSessions));
    }
    NetworkRequestMetric metric = builder.build();
    if (!isAllowedUserAgent(userAgent)) {
        logger.debug("Dropping network request from a 'User-Agent' that is not allowed");
        return metric;
    }
    if (!isReportSent) {
        transportManager.log(metric, getAppState());
        isReportSent = true;
        return metric;
    }
    if (isManualNetworkRequestMetric) {
        logger.debug("This metric has already been queued for transmission.  " + "Please create a new HttpMetric for each request/response");
    }
    return metric;
}
Also used : PerfSession(com.google.firebase.perf.session.PerfSession) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric)

Example 2 with NetworkRequestMetric

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

the class InstrHttpURLConnectionTest method testGetContentClasses.

@Test
public void testGetContentClasses() throws IOException {
    HttpURLConnection urlConnection = mockHttpUrlConnection();
    when(urlConnection.getDoOutput()).thenReturn(true);
    @SuppressWarnings("rawtypes") Class[] classes = { TransportManager.class };
    Object fakeObject = new Object();
    when(urlConnection.getContent(classes)).thenReturn(fakeObject);
    Object retObj = new InstrHttpURLConnection(urlConnection, timer, networkMetricBuilder).getContent(classes);
    assertEquals(fakeObject, retObj);
    verify(urlConnection).getContent(classes);
    verify(transportManager).log(networkArgumentCaptor.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = networkArgumentCaptor.getValue();
    assertEquals(1000, metric.getClientStartTimeUs());
    assertEquals(HttpMethod.POST, metric.getHttpMethod());
    assertEquals(urlConnection.getResponseCode(), metric.getHttpResponseCode());
    assertEquals(urlConnection.getContentLength(), metric.getResponsePayloadBytes());
    assertEquals(urlConnection.getContentType(), metric.getResponseContentType());
    assertEquals(2000, metric.getTimeToResponseCompletedUs());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Test(org.junit.Test)

Example 3 with NetworkRequestMetric

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

the class InstrHttpsURLConnectionTest method testGetContentClasses.

@Test
public void testGetContentClasses() throws IOException {
    HttpsURLConnection urlConnection = mockHttpsUrlConnection();
    when(urlConnection.getDoOutput()).thenReturn(true);
    @SuppressWarnings("rawtypes") Class[] classes = { TransportManager.class };
    Object fakeObject = new Object();
    when(urlConnection.getContent(classes)).thenReturn(fakeObject);
    Object retObj = new InstrHttpsURLConnection(urlConnection, timer, networkMetricBuilder).getContent(classes);
    assertThat(retObj).isEqualTo(fakeObject);
    verify(urlConnection).getContent(classes);
    verify(transportManager).log(networkArgumentCaptor.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = networkArgumentCaptor.getValue();
    assertThat(metric.getClientStartTimeUs()).isEqualTo(1000);
    assertThat(metric.getHttpMethod()).isEqualTo(HttpMethod.POST);
    assertThat(metric.getHttpResponseCode()).isEqualTo(urlConnection.getResponseCode());
    assertThat(metric.getResponsePayloadBytes()).isEqualTo(urlConnection.getContentLength());
    assertThat(metric.getResponseContentType()).isEqualTo(urlConnection.getContentType());
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 4 with NetworkRequestMetric

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

the class InstrHttpsURLConnectionTest method testGetContent.

@Test
public void testGetContent() throws IOException {
    HttpsURLConnection urlConnection = mockHttpsUrlConnection();
    when(urlConnection.getDoOutput()).thenReturn(true);
    Object fakeObject = new Object();
    when(urlConnection.getContent()).thenReturn(fakeObject);
    Object retObj = new InstrHttpsURLConnection(urlConnection, timer, networkMetricBuilder).getContent();
    assertThat(retObj).isEqualTo(fakeObject);
    verify(urlConnection).getContent();
    verify(transportManager).log(networkArgumentCaptor.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = networkArgumentCaptor.getValue();
    assertThat(metric.getClientStartTimeUs()).isEqualTo(1000);
    assertThat(metric.getHttpMethod()).isEqualTo(HttpMethod.POST);
    assertThat(metric.getHttpResponseCode()).isEqualTo(urlConnection.getResponseCode());
    assertThat(metric.getResponsePayloadBytes()).isEqualTo(urlConnection.getContentLength());
    assertThat(metric.getResponseContentType()).isEqualTo(urlConnection.getContentType());
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 5 with NetworkRequestMetric

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

the class InstrURLConnectionBaseTest method testConnectThrowsIOException.

@Test
public void testConnectThrowsIOException() throws IOException {
    HttpURLConnection urlConnection = mockHttpUrlConnection();
    doThrow(IOException.class).when(urlConnection).connect();
    assertThrows(IOException.class, () -> new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder).connect());
    verify(transportManager).log(networkArgumentCaptor.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = networkArgumentCaptor.getValue();
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) 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