Search in sources :

Example 1 with PerfSession

use of com.google.firebase.perf.session.PerfSession 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 PerfSession

use of com.google.firebase.perf.session.PerfSession in project firebase-android-sdk by firebase.

the class NetworkRequestMetricBuilder method setRequestStartTimeMicros.

/**
 * Sets the clientStartTimeUs for the current {@link NetworkRequestMetric}.
 *
 * <p>Note: The start of the request also trigger collection of a single {@link GaugeMetric} data
 * point depending upon the current {@link PerfSession} verbosity.
 *
 * @see GaugeManager#collectGaugeMetricOnce(Timer)
 * @see PerfSession#isGaugeAndEventCollectionEnabled()
 */
public NetworkRequestMetricBuilder setRequestStartTimeMicros(long time) {
    SessionManager sessionManager = SessionManager.getInstance();
    PerfSession perfSession = sessionManager.perfSession();
    SessionManager.getInstance().registerForSessionUpdates(weakReference);
    builder.setClientStartTimeUs(time);
    updateSession(perfSession);
    if (perfSession.isGaugeAndEventCollectionEnabled()) {
        gaugeManager.collectGaugeMetricOnce(perfSession.getTimer());
    }
    return this;
}
Also used : SessionManager(com.google.firebase.perf.session.SessionManager) PerfSession(com.google.firebase.perf.session.PerfSession)

Example 3 with PerfSession

use of com.google.firebase.perf.session.PerfSession in project firebase-android-sdk by firebase.

the class GaugeManagerTest method testStartCollectingGaugesStartsCollectingMetricsInForegroundState.

@Test
public void testStartCollectingGaugesStartsCollectingMetricsInForegroundState() {
    PerfSession fakeSession = new PerfSession("sessionId", new Clock());
    testGaugeManager.startCollectingGauges(fakeSession, ApplicationProcessState.FOREGROUND);
    verify(fakeCpuGaugeCollector).startCollecting(eq(DEFAULT_CPU_GAUGE_COLLECTION_FREQUENCY_FG_MS), ArgumentMatchers.nullable(Timer.class));
    verify(fakeMemoryGaugeCollector).startCollecting(eq(DEFAULT_MEMORY_GAUGE_COLLECTION_FREQUENCY_FG_MS), ArgumentMatchers.nullable(Timer.class));
}
Also used : Timer(com.google.firebase.perf.util.Timer) PerfSession(com.google.firebase.perf.session.PerfSession) Clock(com.google.firebase.perf.util.Clock) Test(org.junit.Test)

Example 4 with PerfSession

use of com.google.firebase.perf.session.PerfSession in project firebase-android-sdk by firebase.

the class GaugeManagerTest method testStartGaugeManagerWithSameSessionIdButDifferentAppState.

@Test
public void testStartGaugeManagerWithSameSessionIdButDifferentAppState() {
    PerfSession fakeSession = new PerfSession("sessionId", new Clock());
    // Start collecting Gauges.
    testGaugeManager.startCollectingGauges(fakeSession, ApplicationProcessState.BACKGROUND);
    CpuMetricReading fakeCpuMetricReading1 = createFakeCpuMetricReading(200, 100);
    fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading1);
    AndroidMemoryReading fakeMemoryMetricReading1 = createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */
    1234);
    fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading1);
    fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
    GaugeMetric recordedGaugeMetric1 = getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
    assertThatCpuGaugeMetricWasSentToTransport("sessionId", recordedGaugeMetric1, fakeCpuMetricReading1);
    assertThatMemoryGaugeMetricWasSentToTransport("sessionId", recordedGaugeMetric1, fakeMemoryMetricReading1);
    // One Cpu and Memory metric was added when the gauge was collecting for the previous sessionId.
    CpuMetricReading fakeCpuMetricReading2 = createFakeCpuMetricReading(400, 500);
    fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading2);
    AndroidMemoryReading fakeMemoryMetricReading2 = createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */
    2345);
    fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading2);
    // Start collecting gauges for same session, but new app state
    testGaugeManager.startCollectingGauges(fakeSession, ApplicationProcessState.FOREGROUND);
    // The next sweep conducted by GaugeManager still associates metrics to old sessionId and state.
    fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
    GaugeMetric recordedGaugeMetric2 = getLastRecordedGaugeMetric(ApplicationProcessState.BACKGROUND, 1);
    assertThatCpuGaugeMetricWasSentToTransport("sessionId", recordedGaugeMetric2, fakeCpuMetricReading2);
    assertThatMemoryGaugeMetricWasSentToTransport("sessionId", recordedGaugeMetric2, fakeMemoryMetricReading2);
    // Collect some more Cpu and Memory metrics and verify that they're associated with new
    // sessionId and state.
    CpuMetricReading fakeCpuMetricReading3 = createFakeCpuMetricReading(500, 600);
    fakeCpuGaugeCollector.cpuMetricReadings.add(fakeCpuMetricReading3);
    AndroidMemoryReading fakeMemoryMetricReading3 = createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */
    3456);
    fakeMemoryGaugeCollector.memoryMetricReadings.add(fakeMemoryMetricReading3);
    fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
    GaugeMetric recordedGaugeMetric3 = getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND, 1);
    assertThatCpuGaugeMetricWasSentToTransport("sessionId", recordedGaugeMetric3, fakeCpuMetricReading3);
    assertThatMemoryGaugeMetricWasSentToTransport("sessionId", recordedGaugeMetric3, fakeMemoryMetricReading3);
}
Also used : AndroidMemoryReading(com.google.firebase.perf.v1.AndroidMemoryReading) CpuMetricReading(com.google.firebase.perf.v1.CpuMetricReading) PerfSession(com.google.firebase.perf.session.PerfSession) GaugeMetric(com.google.firebase.perf.v1.GaugeMetric) Clock(com.google.firebase.perf.util.Clock) Test(org.junit.Test)

Example 5 with PerfSession

use of com.google.firebase.perf.session.PerfSession in project firebase-android-sdk by firebase.

the class GaugeManagerTest method testGaugeManagerClearsTheQueueEachRun.

@Test
public void testGaugeManagerClearsTheQueueEachRun() {
    PerfSession fakeSession = new PerfSession("sessionId", new Clock());
    testGaugeManager.startCollectingGauges(fakeSession, ApplicationProcessState.BACKGROUND);
    fakeCpuGaugeCollector.cpuMetricReadings.add(createFakeCpuMetricReading(200, 100));
    fakeCpuGaugeCollector.cpuMetricReadings.add(createFakeCpuMetricReading(300, 400));
    fakeMemoryGaugeCollector.memoryMetricReadings.add(createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */
    1234));
    assertThat(fakeCpuGaugeCollector.cpuMetricReadings).isNotEmpty();
    assertThat(fakeMemoryGaugeCollector.memoryMetricReadings).isNotEmpty();
    fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
    assertThat(fakeCpuGaugeCollector.cpuMetricReadings).isEmpty();
    assertThat(fakeMemoryGaugeCollector.memoryMetricReadings).isEmpty();
    fakeCpuGaugeCollector.cpuMetricReadings.add(createFakeCpuMetricReading(200, 100));
    fakeMemoryGaugeCollector.memoryMetricReadings.add(createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */
    1234));
    fakeMemoryGaugeCollector.memoryMetricReadings.add(createFakeAndroidMetricReading(/* currentUsedAppJavaHeapMemoryKb= */
    2345));
    assertThat(fakeCpuGaugeCollector.cpuMetricReadings).isNotEmpty();
    assertThat(fakeMemoryGaugeCollector.memoryMetricReadings).isNotEmpty();
    fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
    assertThat(fakeCpuGaugeCollector.cpuMetricReadings).isEmpty();
    assertThat(fakeMemoryGaugeCollector.memoryMetricReadings).isEmpty();
}
Also used : PerfSession(com.google.firebase.perf.session.PerfSession) Clock(com.google.firebase.perf.util.Clock) Test(org.junit.Test)

Aggregations

PerfSession (com.google.firebase.perf.session.PerfSession)22 Clock (com.google.firebase.perf.util.Clock)19 Test (org.junit.Test)19 Timer (com.google.firebase.perf.util.Timer)7 AndroidMemoryReading (com.google.firebase.perf.v1.AndroidMemoryReading)5 CpuMetricReading (com.google.firebase.perf.v1.CpuMetricReading)5 GaugeMetric (com.google.firebase.perf.v1.GaugeMetric)5 SessionManager (com.google.firebase.perf.session.SessionManager)2 Keep (androidx.annotation.Keep)1 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)1