Search in sources :

Example 1 with Clock

use of com.google.firebase.perf.util.Clock in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method setUp.

@Before
public void setUp() {
    currentTime = 0;
    initMocks(this);
    doAnswer((Answer<Timer>) invocationOnMock -> new Timer(currentTime)).when(clock).getTime();
    activity1 = createFakeActivity(/* isHardwareAccelerated= */
    true);
    activity2 = createFakeActivity(/* isHardwareAccelerated= */
    true);
    DeviceCacheManager.clearInstance();
    ConfigResolver.clearInstance();
    ConfigResolver configResolver = ConfigResolver.getInstance();
    configResolver.setDeviceCacheManager(new DeviceCacheManager(new FakeDirectExecutorService()));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) Context(android.content.Context) TraceMetric(com.google.firebase.perf.v1.TraceMetric) ActivityController(org.robolectric.android.controller.ActivityController) Bundle(android.os.Bundle) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Captor(org.mockito.Captor) Answer(org.mockito.stubbing.Answer) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) Constants(com.google.firebase.perf.util.Constants) PerfSession(com.google.firebase.perf.v1.PerfSession) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) WeakReference(java.lang.ref.WeakReference) FirebasePerformanceTestBase(com.google.firebase.perf.FirebasePerformanceTestBase) GaugeManager(com.google.firebase.perf.session.gauges.GaugeManager) ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Before(org.junit.Before) FirebasePerformanceInitializer(com.google.firebase.perf.FirebasePerformanceInitializer) Timer(com.google.firebase.perf.util.Timer) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) FOREGROUND_BACKGROUND(com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND) Clock(com.google.firebase.perf.util.Clock) Robolectric(org.robolectric.Robolectric) Trace(com.google.firebase.perf.metrics.Trace) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) TransportManager(com.google.firebase.perf.transport.TransportManager) Mockito.verify(org.mockito.Mockito.verify) SessionManager(com.google.firebase.perf.session.SessionManager) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) Mockito.never(org.mockito.Mockito.never) Application(android.app.Application) LayoutParams(android.view.WindowManager.LayoutParams) Assert(org.junit.Assert) Activity(android.app.Activity) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Timer(com.google.firebase.perf.util.Timer) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) Before(org.junit.Before)

Example 2 with Clock

use of com.google.firebase.perf.util.Clock in project firebase-android-sdk by firebase.

the class FragmentStateMonitorTest method setUp.

@Before
public void setUp() {
    currentTime = 0;
    initMocks(this);
    doAnswer((Answer<Timer>) invocationOnMock -> new Timer(currentTime)).when(clock).getTime();
    DeviceCacheManager.clearInstance();
    when(configResolver.isPerformanceMonitoringEnabled()).thenReturn(true);
    doNothing().when(recorder).start();
    doNothing().when(recorder).startFragment(any());
    activity = createFakeActivity(true);
    // Sample frame counts.
    frameCounts1 = new PerfFrameMetrics(9, 5, 3);
    frameCounts2 = new PerfFrameMetrics(14, 9, 4);
    savedInstanceState = mock(Bundle.class);
    appStateMonitor = mock(AppStateMonitor.class);
    mockFragment1 = mock(Fragment.class);
    mockFragment2 = mock(Fragment.class);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) TraceMetric(com.google.firebase.perf.v1.TraceMetric) ActivityController(org.robolectric.android.controller.ActivityController) PerfFrameMetrics(com.google.firebase.perf.metrics.FrameMetricsCalculator.PerfFrameMetrics) Bundle(android.os.Bundle) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Mock(org.mockito.Mock) WindowManager(android.view.WindowManager) RunWith(org.junit.runner.RunWith) Mockito.spy(org.mockito.Mockito.spy) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) Captor(org.mockito.Captor) Answer(org.mockito.stubbing.Answer) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) Constants(com.google.firebase.perf.util.Constants) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Fragment(androidx.fragment.app.Fragment) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) Mockito.doReturn(org.mockito.Mockito.doReturn) FirebasePerformanceTestBase(com.google.firebase.perf.FirebasePerformanceTestBase) WeakHashMap(java.util.WeakHashMap) ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Before(org.junit.Before) FragmentManager(androidx.fragment.app.FragmentManager) Timer(com.google.firebase.perf.util.Timer) Clock(com.google.firebase.perf.util.Clock) Robolectric(org.robolectric.Robolectric) Trace(com.google.firebase.perf.metrics.Trace) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) TransportManager(com.google.firebase.perf.transport.TransportManager) Mockito.verify(org.mockito.Mockito.verify) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) CounterNames(com.google.firebase.perf.util.Constants.CounterNames) Optional(com.google.firebase.perf.util.Optional) Activity(android.app.Activity) Mockito.mock(org.mockito.Mockito.mock) Timer(com.google.firebase.perf.util.Timer) Bundle(android.os.Bundle) PerfFrameMetrics(com.google.firebase.perf.metrics.FrameMetricsCalculator.PerfFrameMetrics) Fragment(androidx.fragment.app.Fragment) Before(org.junit.Before)

Example 3 with Clock

use of com.google.firebase.perf.util.Clock in project firebase-android-sdk by firebase.

the class PerfSession method create.

/*
   * Creates a PerfSession object and decides what metrics to collect.
   */
public static PerfSession create() {
    String sessionId = UUID.randomUUID().toString().replace("-", "");
    PerfSession session = new PerfSession(sessionId, new Clock());
    session.setGaugeAndEventCollectionEnabled(shouldCollectGaugesAndEvents());
    return session;
}
Also used : Clock(com.google.firebase.perf.util.Clock)

Example 4 with Clock

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

use of com.google.firebase.perf.util.Clock 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

Clock (com.google.firebase.perf.util.Clock)24 Test (org.junit.Test)23 PerfSession (com.google.firebase.perf.session.PerfSession)19 Timer (com.google.firebase.perf.util.Timer)9 AndroidMemoryReading (com.google.firebase.perf.v1.AndroidMemoryReading)5 CpuMetricReading (com.google.firebase.perf.v1.CpuMetricReading)5 GaugeMetric (com.google.firebase.perf.v1.GaugeMetric)5 TraceMetric (com.google.firebase.perf.v1.TraceMetric)3 Activity (android.app.Activity)2 Bundle (android.os.Bundle)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 FirebasePerformanceTestBase (com.google.firebase.perf.FirebasePerformanceTestBase)2 ConfigResolver (com.google.firebase.perf.config.ConfigResolver)2 DeviceCacheManager (com.google.firebase.perf.config.DeviceCacheManager)2 Trace (com.google.firebase.perf.metrics.Trace)2 TransportManager (com.google.firebase.perf.transport.TransportManager)2 Constants (com.google.firebase.perf.util.Constants)2 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)2 PerfMetric (com.google.firebase.perf.v1.PerfMetric)2 PerfSession (com.google.firebase.perf.v1.PerfSession)2