use of com.google.firebase.perf.v1.GaugeMetric in project firebase-android-sdk by firebase.
the class GaugeManagerTest method testStartingGaugeManagerWithNewSessionIdButSameAppState.
@Test
public void testStartingGaugeManagerWithNewSessionIdButSameAppState() {
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, but same app state.
testGaugeManager.startCollectingGauges(fakeSession2, ApplicationProcessState.BACKGROUND);
// 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.BACKGROUND, 1);
assertThatCpuGaugeMetricWasSentToTransport("sessionId2", recordedGaugeMetric3, fakeCpuMetricReading3);
assertThatMemoryGaugeMetricWasSentToTransport("sessionId2", recordedGaugeMetric3, fakeMemoryMetricReading3);
}
use of com.google.firebase.perf.v1.GaugeMetric 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);
}
use of com.google.firebase.perf.v1.GaugeMetric in project firebase-android-sdk by firebase.
the class TransportManagerTest method validGaugeMetric_withCpuReadings_isLogged.
// endregion
// region Gauge Specific
@Test
public void validGaugeMetric_withCpuReadings_isLogged() {
ApplicationProcessState expectedAppState = ApplicationProcessState.FOREGROUND;
// Construct a list of Cpu metric readings
List<CpuMetricReading> expectedCpuMetricReadings = new ArrayList<>();
expectedCpuMetricReadings.add(createValidCpuMetricReading(/* userTimeUs= */
10, /* systemTimeUs= */
20));
expectedCpuMetricReadings.add(createValidCpuMetricReading(/* userTimeUs= */
20, /* systemTimeUs= */
30));
GaugeMetric validGauge = GaugeMetric.newBuilder().setSessionId("sessionId").addAllCpuMetricReadings(expectedCpuMetricReadings).build();
testTransportManager.log(validGauge, expectedAppState);
fakeExecutorService.runAll();
PerfMetric loggedPerfMetric = getLastLoggedEvent(times(1));
assertThat(loggedPerfMetric.getGaugeMetric().getCpuMetricReadingsList()).containsExactlyElementsIn(expectedCpuMetricReadings);
assertThat(loggedPerfMetric.getGaugeMetric().getSessionId()).isEqualTo("sessionId");
}
use of com.google.firebase.perf.v1.GaugeMetric in project firebase-android-sdk by firebase.
the class TransportManagerTest method logMultipleGauges_transportNotInitialized_gaugesAfterMaxCapAreNotQueued.
@Test
public void logMultipleGauges_transportNotInitialized_gaugesAfterMaxCapAreNotQueued() {
// 1. Transport is not initialized in the beginning
initializeTransport(false);
// 2. Log multiple Gauges such that they are capped
// only 50 GaugeMetric events are allowed to cache
int maxGaugesCacheSize = 50;
int totalGaugeEvents = maxGaugesCacheSize + 10;
GaugeMetric[] validGauges = new GaugeMetric[totalGaugeEvents];
for (int i = 0; i < totalGaugeEvents; i++) {
validGauges[i] = createValidGaugeMetric().toBuilder().setSessionId("Session - " + (i + 1)).build();
testTransportManager.log(validGauges[i], ApplicationProcessState.FOREGROUND);
fakeExecutorService.runAll();
assertThat(getLastLoggedEvent(never())).isNull();
}
// 3. Even though we recorded "totalGaugeEvents", events up-to "maxGaugesCacheSize" are only
// queued
assertThat(testTransportManager.getPendingEventsQueue().size()).isEqualTo(maxGaugesCacheSize);
// 4. Initialize Transport
initializeTransport(true);
// 5. Consume all queued Gauges and validate them
for (int i = 0; i < maxGaugesCacheSize; i++) {
clearLastLoggedEvents();
fakeExecutorService.runNext();
PerfMetric loggedValidGauge = getLastLoggedEvent(times(1));
assertThat(loggedValidGauge.getGaugeMetric()).isEqualTo(validGauges[i]);
validateApplicationInfo(loggedValidGauge, ApplicationProcessState.FOREGROUND);
}
// 6. Queue is all consumed after iterating "maxGaugesCacheSize" events
assertThat(testTransportManager.getPendingEventsQueue().isEmpty()).isTrue();
// 7. No pending events
clearLastLoggedEvents();
fakeExecutorService.runAll();
assertThat(getLastLoggedEvent(never())).isNull();
}
use of com.google.firebase.perf.v1.GaugeMetric in project firebase-android-sdk by firebase.
the class TransportManagerTest method validGaugeMetric_withMetadata_isLogged.
@Test
public void validGaugeMetric_withMetadata_isLogged() {
ApplicationProcessState expectedAppState = ApplicationProcessState.FOREGROUND;
GaugeMetadata gaugeMetadata = GaugeMetadata.newBuilder().setDeviceRamSizeKb(2000).setMaxAppJavaHeapMemoryKb(1000).setMaxEncouragedAppJavaHeapMemoryKb(800).build();
GaugeMetric validGauge = GaugeMetric.newBuilder().setSessionId("sessionId").setGaugeMetadata(gaugeMetadata).build();
testTransportManager.log(validGauge, expectedAppState);
fakeExecutorService.runAll();
PerfMetric loggedPerfMetric = getLastLoggedEvent(times(1));
assertThat(loggedPerfMetric.getGaugeMetric().getSessionId()).isEqualTo("sessionId");
assertThat(loggedPerfMetric.getGaugeMetric().getGaugeMetadata().getDeviceRamSizeKb()).isEqualTo(2000);
assertThat(loggedPerfMetric.getGaugeMetric().getGaugeMetadata().getMaxAppJavaHeapMemoryKb()).isEqualTo(1000);
assertThat(loggedPerfMetric.getGaugeMetric().getGaugeMetadata().getMaxEncouragedAppJavaHeapMemoryKb()).isEqualTo(800);
}
Aggregations