Skip to content

Commit 88a99cc

Browse files
committed
Add logic to flush metrics for a given app state and add unit test
1 parent 89a7580 commit 88a99cc

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/session/gauges/GaugeManager.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,17 @@ public void initializeGaugeMetadataManager(Context appContext) {
100100

101101
@Override
102102
public void onUpdateAppState(ApplicationProcessState applicationProcessState) {
103-
this.applicationProcessState = applicationProcessState;
104-
103+
// Update the app state and return.
105104
if (session == null || !session.isVerbose()) {
105+
this.applicationProcessState = applicationProcessState;
106106
return;
107107
}
108108

109+
// Log existing gauges to the current app state.
110+
logGaugeMetrics();
111+
// Update App State.
112+
this.applicationProcessState = applicationProcessState;
113+
109114
// If it's a verbose session, start collecting gauges for the new app state.
110115
startCollectingGauges(this.applicationProcessState, session.getTimer());
111116
}

firebase-perf/src/test/java/com/google/firebase/perf/session/gauges/GaugeManagerTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,42 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() {
357357
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
358358
}
359359

360+
@Test
361+
public void testUpdateAppStateFlushesMetricsInTheCurrentAppState() {
362+
PerfSession fakeSession = createTestSession(1);
363+
fakeSession.setGaugeAndEventCollectionEnabled(true);
364+
testGaugeManager.setApplicationProcessState(ApplicationProcessState.FOREGROUND);
365+
testGaugeManager.startCollectingGauges(fakeSession);
366+
GaugeCounter.INSTANCE.setGaugeManager(testGaugeManager);
367+
368+
// There's no job to log the gauges.
369+
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
370+
371+
// Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
372+
generateMetricsAndIncrementCounter(10);
373+
374+
// There's still no job to log the gauges.
375+
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
376+
377+
testGaugeManager.onUpdateAppState(ApplicationProcessState.BACKGROUND);
378+
379+
assertThat(fakeScheduledExecutorService.isEmpty()).isFalse();
380+
assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS))
381+
.isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS);
382+
383+
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
384+
GaugeMetric recordedGaugeMetric =
385+
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND, 1);
386+
387+
// It flushes all metrics in the ConcurrentLinkedQueues.
388+
int recordedGaugeMetricsCount =
389+
recordedGaugeMetric.getAndroidMemoryReadingsCount()
390+
+ recordedGaugeMetric.getCpuMetricReadingsCount();
391+
assertThat(recordedGaugeMetricsCount).isEqualTo(10);
392+
393+
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo(testSessionId(1));
394+
}
395+
360396
@Test
361397
public void testStopCollectingGaugesStopsCollectingAllGaugeMetrics() {
362398
PerfSession fakeSession = createTestSession(1);

0 commit comments

Comments
 (0)