Search in sources :

Example 1 with Timer

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

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

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

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

the class FirebasePerfOkHttpClient method execute.

@Keep
public static Response execute(final Call call) throws IOException {
    final Response response;
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(TransportManager.getInstance());
    Timer timer = new Timer();
    long startTimeMicros = timer.getMicros();
    try {
        response = call.execute();
        long responseCompletedTimeMicros = timer.getDurationMicros();
        sendNetworkMetric(response, builder, startTimeMicros, responseCompletedTimeMicros);
    } catch (IOException e) {
        Request request = call.request();
        if (request != null) {
            HttpUrl url = request.url();
            if (url != null) {
                builder.setUrl(url.url().toString());
            }
            String method = request.method();
            if (method != null) {
                builder.setHttpMethod(request.method());
            }
        }
        builder.setRequestStartTimeMicros(startTimeMicros);
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
    return response;
}
Also used : Response(okhttp3.Response) Timer(com.google.firebase.perf.util.Timer) Request(okhttp3.Request) IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) HttpUrl(okhttp3.HttpUrl) Keep(androidx.annotation.Keep)

Example 5 with Timer

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

the class Trace method startStage.

/**
 * Start a stage. If a stage is already running, it is stopped.
 *
 * @param name Name to be given to the stage.
 * @hide
 */
void startStage(@NonNull String name) {
    Timer currentTime = clock.getTime();
    setEndTimeOfLastStage(currentTime);
    subtraces.add(new Trace(this, name, currentTime, null, null, null, null));
}
Also used : Timer(com.google.firebase.perf.util.Timer)

Aggregations

Timer (com.google.firebase.perf.util.Timer)38 Test (org.junit.Test)29 Before (org.junit.Before)7 TransportManager (com.google.firebase.perf.transport.TransportManager)4 TraceMetric (com.google.firebase.perf.v1.TraceMetric)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)3 FirebasePerformanceTestBase (com.google.firebase.perf.FirebasePerformanceTestBase)3 ConfigResolver (com.google.firebase.perf.config.ConfigResolver)3 DeviceCacheManager (com.google.firebase.perf.config.DeviceCacheManager)3 Clock (com.google.firebase.perf.util.Clock)3 Constants (com.google.firebase.perf.util.Constants)3 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)3 CpuMetricReading (com.google.firebase.perf.v1.CpuMetricReading)3 RunWith (org.junit.runner.RunWith)3 ArgumentCaptor (org.mockito.ArgumentCaptor)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 ArgumentMatchers.nullable (org.mockito.ArgumentMatchers.nullable)3 Mock (org.mockito.Mock)3 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 Mockito.mock (org.mockito.Mockito.mock)3