Search in sources :

Example 11 with PerfSession

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

the class GaugeManagerTest method testStartGaugeManagerWithNewSessionIdAndNewAppState.

@Test
public void testStartGaugeManagerWithNewSessionIdAndNewAppState() {
    PerfSession fakeSession1 = new PerfSession("sessionId", new Clock());
    // Start collecting Gauges.
    testGaugeManager.startCollectingGauges(fakeSession1, 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);
    PerfSession fakeSession2 = new PerfSession("sessionId2", new Clock());
    // Start collecting gauges for new session and new app state
    testGaugeManager.startCollectingGauges(fakeSession2, 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("sessionId2", recordedGaugeMetric3, fakeCpuMetricReading3);
    assertThatMemoryGaugeMetricWasSentToTransport("sessionId2", 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 12 with PerfSession

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

the class GaugeManagerTest method testStartCollectingGaugesStartsCollectingMetricsInBackgroundState.

@Test
public void testStartCollectingGaugesStartsCollectingMetricsInBackgroundState() {
    PerfSession fakeSession = new PerfSession("sessionId", new Clock());
    testGaugeManager.startCollectingGauges(fakeSession, ApplicationProcessState.BACKGROUND);
    verify(fakeCpuGaugeCollector).startCollecting(eq(DEFAULT_CPU_GAUGE_COLLECTION_FREQUENCY_BG_MS), ArgumentMatchers.nullable(Timer.class));
    verify(fakeMemoryGaugeCollector).startCollecting(eq(DEFAULT_MEMORY_GAUGE_COLLECTION_FREQUENCY_BG_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 13 with PerfSession

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

the class GaugeManagerTest method testStartCollectingGaugesDoesNotStartAJobToConsumeMetricsWithUnknownAppState.

@Test
public void testStartCollectingGaugesDoesNotStartAJobToConsumeMetricsWithUnknownAppState() {
    PerfSession fakeSession = new PerfSession("sessionId", new Clock());
    testGaugeManager.startCollectingGauges(fakeSession, ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN);
    assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
}
Also used : PerfSession(com.google.firebase.perf.session.PerfSession) Clock(com.google.firebase.perf.util.Clock) Test(org.junit.Test)

Example 14 with PerfSession

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

the class GaugeManagerTest method stopCollectingCPUMetrics_invalidCPUCaptureFrequency_appInForegrounf.

@Test
public void stopCollectingCPUMetrics_invalidCPUCaptureFrequency_appInForegrounf() {
    // PASS 1: Test with 0
    doReturn(0L).when(mockConfigResolver).getSessionsCpuCaptureFrequencyForegroundMs();
    PerfSession fakeSession1 = new PerfSession("sessionId", new Clock());
    testGaugeManager.startCollectingGauges(fakeSession1, ApplicationProcessState.FOREGROUND);
    assertThat(fakeScheduledExecutorService.isEmpty()).isFalse();
    // PASS 2: Test with -ve value
    doReturn(-25L).when(mockConfigResolver).getSessionsCpuCaptureFrequencyForegroundMs();
    PerfSession fakeSession2 = new PerfSession("sessionId", new Clock());
    testGaugeManager.startCollectingGauges(fakeSession2, ApplicationProcessState.FOREGROUND);
    assertThat(fakeScheduledExecutorService.isEmpty()).isFalse();
}
Also used : PerfSession(com.google.firebase.perf.session.PerfSession) Clock(com.google.firebase.perf.util.Clock) Test(org.junit.Test)

Example 15 with PerfSession

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

the class Trace method start.

/**
 * Starts this trace.
 */
@Keep
public void start() {
    if (!ConfigResolver.getInstance().isPerformanceMonitoringEnabled()) {
        logger.debug("Trace feature is disabled.");
        return;
    }
    String err = validateTraceName(name);
    if (err != null) {
        logger.error("Cannot start trace '%s'. Trace name is invalid.(%s)", name, err);
        return;
    }
    if (startTime != null) {
        logger.error("Trace '%s' has already started, should not start again!", name);
        return;
    }
    startTime = clock.getTime();
    registerForAppState();
    SessionManager sessionManager = SessionManager.getInstance();
    PerfSession perfSession = sessionManager.perfSession();
    SessionManager.getInstance().registerForSessionUpdates(sessionAwareObject);
    updateSession(perfSession);
    if (perfSession.isGaugeAndEventCollectionEnabled()) {
        gaugeManager.collectGaugeMetricOnce(perfSession.getTimer());
    }
}
Also used : SessionManager(com.google.firebase.perf.session.SessionManager) PerfSession(com.google.firebase.perf.session.PerfSession) Keep(androidx.annotation.Keep)

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