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());
}
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;
}
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");
}
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;
}
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;
}
}
Aggregations