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