Search in sources :

Example 1 with PerfSession

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

the class AppStateMonitorTest method updatePerfSession_isAfterSendingForegroundOrBackgroundSession.

@Test
public void updatePerfSession_isAfterSendingForegroundOrBackgroundSession() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    monitor.registerForAppState(SessionManager.getInstance().getAppStateCallback());
    monitor.setStopTime(new Timer(currentTime));
    monitor.setIsColdStart(false);
    // Mandatory due to circular dependencies of singletons AppStateMonitor and SessionManager
    AppStateMonitor.getInstance().setIsColdStart(false);
    // Foreground -> Background, sends _fs
    PerfSession currentSession = SessionManager.getInstance().perfSession().build();
    monitor.onActivityResumed(activity1);
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    PerfSession sentSession = argTraceMetric.getValue().getPerfSessions(0);
    Assert.assertEquals(currentSession, sentSession);
    // Background -> Foreground, sends _bs
    currentSession = SessionManager.getInstance().perfSession().build();
    monitor.onActivityStopped(activity1);
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    sentSession = argTraceMetric.getValue().getPerfSessions(0);
    Assert.assertEquals(currentSession, sentSession);
}
Also used : Timer(com.google.firebase.perf.util.Timer) PerfSession(com.google.firebase.perf.v1.PerfSession) Test(org.junit.Test)

Example 2 with PerfSession

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

the class GaugeManager method startCollectingGauges.

/**
 * Starts the collection of available gauges for the given {@code sessionId} and {@code
 * applicationProcessState}. The collected Gauge Metrics will be flushed at regular intervals.
 *
 * <p>GaugeManager can only collect gauges for one session at a time, and if this method is called
 * again with the same or new sessionId while it's already collecting gauges, all future gauges
 * will then be associated with the same or new sessionId and applicationProcessState.
 *
 * @param session The {@link PerfSession} to which the collected gauges will be associated with.
 * @param applicationProcessState The {@link ApplicationProcessState} the collected GaugeMetrics
 *     will be associated with.
 * @note: This method is NOT thread safe - {@link this.startCollectingGauges()} and {@link
 *     this.stopCollectingGauges()} should always be called from the same thread.
 */
public void startCollectingGauges(PerfSession session, ApplicationProcessState applicationProcessState) {
    if (this.sessionId != null) {
        stopCollectingGauges();
    }
    long collectionFrequency = startCollectingGauges(applicationProcessState, session.getTimer());
    if (collectionFrequency == INVALID_GAUGE_COLLECTION_FREQUENCY) {
        logger.warn("Invalid gauge collection frequency. Unable to start collecting Gauges.");
        return;
    }
    this.sessionId = session.sessionId();
    this.applicationProcessState = applicationProcessState;
    // This is needed, otherwise the Runnable might use a stale value.
    final String sessionIdForScheduledTask = sessionId;
    final ApplicationProcessState applicationProcessStateForScheduledTask = applicationProcessState;
    try {
        gaugeManagerDataCollectionJob = gaugeManagerExecutor.get().scheduleAtFixedRate(() -> {
            syncFlush(sessionIdForScheduledTask, applicationProcessStateForScheduledTask);
        }, /*initialDelay=*/
        collectionFrequency * APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC, /*period=*/
        collectionFrequency * APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC, TimeUnit.MILLISECONDS);
    } catch (RejectedExecutionException e) {
        logger.warn("Unable to start collecting Gauges: " + e.getMessage());
    }
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 3 with PerfSession

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

the class GaugeManager method logGaugeMetadata.

/**
 * Log the Gauge Metadata information to the transport.
 *
 * @param sessionId The {@link PerfSession#sessionId()} to which the collected Gauge Metrics
 *     should be associated with.
 * @param appState The {@link ApplicationProcessState} for which these gauges are collected.
 * @return true if GaugeMetadata was logged, false otherwise.
 */
public boolean logGaugeMetadata(String sessionId, ApplicationProcessState appState) {
    if (gaugeMetadataManager != null) {
        GaugeMetric gaugeMetric = GaugeMetric.newBuilder().setSessionId(sessionId).setGaugeMetadata(getGaugeMetadata()).build();
        transportManager.log(gaugeMetric, appState);
        return true;
    }
    return false;
}
Also used : GaugeMetric(com.google.firebase.perf.v1.GaugeMetric)

Example 4 with PerfSession

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

use of com.google.firebase.perf.v1.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)

Aggregations

Test (org.junit.Test)8 Clock (com.google.firebase.perf.util.Clock)7 PerfSession (com.google.firebase.perf.session.PerfSession)6 GaugeMetric (com.google.firebase.perf.v1.GaugeMetric)6 AndroidMemoryReading (com.google.firebase.perf.v1.AndroidMemoryReading)5 CpuMetricReading (com.google.firebase.perf.v1.CpuMetricReading)5 PerfSession (com.google.firebase.perf.v1.PerfSession)5 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)2 PerfMetric (com.google.firebase.perf.v1.PerfMetric)2 ArrayList (java.util.ArrayList)2 Timer (com.google.firebase.perf.util.Timer)1 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)1 TraceMetric (com.google.firebase.perf.v1.TraceMetric)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1