Search in sources :

Example 31 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfNetworkValidatorTest method testNullTimeToResponseInitiated.

@Test
public void testNullTimeToResponseInitiated() {
    NetworkRequestMetricBuilder metricBuilder = createNetworkRequestMetricBuilderWithRequiredValuesPresent();
    NetworkRequestMetric metric = metricBuilder.build();
    assertFalse(metric.hasTimeToResponseInitiatedUs());
    FirebasePerfNetworkValidator validator = new FirebasePerfNetworkValidator(metric, ApplicationProvider.getApplicationContext());
    assertTrue(validator.isValidPerfMetric());
}
Also used : NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) Test(org.junit.Test)

Example 32 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfNetworkValidatorTest method createNetworkRequestMetricBuilderWithRequiredValuesPresent.

private NetworkRequestMetricBuilder createNetworkRequestMetricBuilderWithRequiredValuesPresent() {
    NetworkRequestMetricBuilder networkRequestMetricBuilder = NetworkRequestMetricBuilder.builder(TransportManager.getInstance());
    networkRequestMetricBuilder.setUrl("https://www.google.com");
    networkRequestMetricBuilder.setHttpMethod("GET");
    networkRequestMetricBuilder.setHttpResponseCode(200);
    networkRequestMetricBuilder.setRequestStartTimeMicros(System.currentTimeMillis() * 1000L);
    networkRequestMetricBuilder.setTimeToResponseCompletedMicros(400L);
    return networkRequestMetricBuilder;
}
Also used : NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)

Example 33 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class InstrumentApacheHttpResponseHandlerTest method testHandleResponse.

@Test
public void testHandleResponse() throws IOException {
    // mocks what an app developer would use.  Need to verify that the app's response handler
    // is also called
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

        @Override
        public String handleResponse(HttpResponse httpResponse) throws IOException {
            return "testString";
        }
    };
    TransportManager transportManager = mock(TransportManager.class);
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    InstrumentApacheHttpResponseHandler<String> instrumentResponseHandler = new InstrumentApacheHttpResponseHandler<>(responseHandler, mockTimer(), builder);
    String response = instrumentResponseHandler.handleResponse(mockHttpResponse());
    // Verify that TransportManager is called with correct argument
    ArgumentCaptor<NetworkRequestMetric> argument = ArgumentCaptor.forClass(NetworkRequestMetric.class);
    verify(transportManager, times(1)).log(argument.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = argument.getValue();
    assertThat(metric.getHttpResponseCode()).isEqualTo(200);
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
    assertThat(metric.getResponseContentType()).isEqualTo("text/html");
    assertThat(metric.getResponsePayloadBytes()).isEqualTo(256);
    // Verify that the app developer's response handler is also called
    assertThat(response).isEqualTo("testString");
}
Also used : ResponseHandler(org.apache.http.client.ResponseHandler) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) HttpResponse(org.apache.http.HttpResponse) TransportManager(com.google.firebase.perf.transport.TransportManager) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) Test(org.junit.Test)

Example 34 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfHttpClient method execute.

/**
 * Send network metric for Apache HttpClient.execute(HttpUriRequest request)
 *
 * @return HttpResponse from executing request
 * @throws IOException if unable to execute request or receive response
 */
static HttpResponse execute(final HttpClient client, final HttpUriRequest request, final Timer timer, final TransportManager transportManager) throws IOException {
    HttpResponse response = null;
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    try {
        builder.setUrl(request.getURI().toString()).setHttpMethod(request.getMethod());
        Long requestContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(request);
        if (requestContentLength != null) {
            builder.setRequestPayloadBytes(requestContentLength);
        }
        // Execute network Request
        timer.reset();
        builder.setRequestStartTimeMicros(timer.getMicros());
        response = client.execute(request);
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        builder.setHttpResponseCode(response.getStatusLine().getStatusCode());
        Long responseContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(response);
        if (responseContentLength != null) {
            builder.setResponsePayloadBytes(responseContentLength);
        }
        String contentType = NetworkRequestMetricBuilderUtil.getApacheHttpResponseContentType(response);
        if (contentType != null) {
            builder.setResponseContentType(contentType);
        }
        builder.build();
    } catch (IOException e) {
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
    return response;
}
Also used : HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)

Example 35 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfHttpClient method execute.

/**
 * Send network metric for Apache HttpClient.execute(HttpUriRequest request, ResponseHandler<?
 * extends T> responseHandler)
 *
 * @return <T> T
 * @throws IOException if unable to execute request or receive response
 */
static <T> T execute(final HttpClient client, final HttpUriRequest request, final ResponseHandler<T> responseHandler, final Timer timer, final TransportManager transportManager) throws IOException {
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    try {
        builder.setUrl(request.getURI().toString()).setHttpMethod(request.getMethod());
        Long requestContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(request);
        if (requestContentLength != null) {
            builder.setRequestPayloadBytes(requestContentLength);
        }
        // Execute network Request
        timer.reset();
        builder.setRequestStartTimeMicros(timer.getMicros());
        return client.execute(request, new InstrumentApacheHttpResponseHandler<T>(responseHandler, timer, builder));
    } catch (IOException e) {
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
}
Also used : IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)

Aggregations

NetworkRequestMetricBuilder (com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)40 Test (org.junit.Test)27 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)13 IOException (java.io.IOException)12 HttpResponse (org.apache.http.HttpResponse)5 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)4 HttpURLConnection (java.net.HttpURLConnection)3 URLConnection (java.net.URLConnection)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 HttpUrl (okhttp3.HttpUrl)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 TransportManager (com.google.firebase.perf.transport.TransportManager)2 RequestBody (okhttp3.RequestBody)2 ResponseBody (okhttp3.ResponseBody)2 Keep (androidx.annotation.Keep)1 GaugeManager (com.google.firebase.perf.session.gauges.GaugeManager)1 Timer (com.google.firebase.perf.util.Timer)1 ResponseHandler (org.apache.http.client.ResponseHandler)1