From 993428cd0f994ccfa1b6f9fd1c724e6ad0ff45ec Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Thu, 11 Feb 2021 15:19:26 -0800 Subject: [PATCH] For #17969: add duration probes for App.onCreate and HomeActivity.onCreate. --- app/metrics.yaml | 32 +++++++++++++++++ .../org/mozilla/fenix/FenixApplication.kt | 5 +++ .../java/org/mozilla/fenix/HomeActivity.kt | 3 +- .../org/mozilla/fenix/FenixApplicationTest.kt | 35 +++++++++++++++++++ .../org/mozilla/fenix/HomeActivityTest.kt | 23 ++++++++++++ docs/metrics.md | 2 ++ 6 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt diff --git a/app/metrics.yaml b/app/metrics.yaml index 7aed730d5..ee9289f5d 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -3962,6 +3962,38 @@ startup.timeline: - mcomella@mozilla.com expires: "2021-08-01" +perf.startup: + application_on_create: + type: timing_distribution + time_unit: millisecond + description: | + The duration of `FenixApplication.onCreate` in the main process. + bugs: + - https://github.com/mozilla-mobile/fenix/issues/17969 + data_reviews: + - https://github.com/mozilla-mobile/fenix/pull/17973#issue-572183889 + data_sensitivity: + - technical + notification_emails: + - perf-android-fe@mozilla.com + - mcomella@mozilla.com + expires: "2021-08-11" + home_activity_on_create: + type: timing_distribution + time_unit: millisecond + description: | + The duration of `HomeActivity.onCreate`. + bugs: + - https://github.com/mozilla-mobile/fenix/issues/17969 + data_reviews: + - https://github.com/mozilla-mobile/fenix/pull/17973#issue-572183889 + data_sensitivity: + - technical + notification_emails: + - perf-android-fe@mozilla.com + - mcomella@mozilla.com + expires: "2021-08-11" + perf.awesomebar: history_suggestions: send_in_pings: diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 03db2e676..d3ddf601b 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -39,6 +39,7 @@ import mozilla.components.support.rusthttp.RustHttpConfig import mozilla.components.support.rustlog.RustLog import mozilla.components.support.utils.logElapsedTime import mozilla.components.support.webextensions.WebExtensionSupport +import org.mozilla.fenix.GleanMetrics.PerfStartup import org.mozilla.fenix.components.Components import org.mozilla.fenix.components.metrics.MetricServiceType import org.mozilla.fenix.ext.settings @@ -71,6 +72,7 @@ open class FenixApplication : LocaleAwareApplication(), Provider { private set override fun onCreate() { + val methodDurationTimerId = PerfStartup.applicationOnCreate.start() // DO NOT MOVE ANYTHING ABOVE HERE. super.onCreate() setupInAllProcesses() @@ -92,6 +94,9 @@ open class FenixApplication : LocaleAwareApplication(), Provider { } setupInMainProcessOnly() + + // We use start/stop instead of measure so we don't measure outside the main process. + PerfStartup.applicationOnCreate.stopAndAccumulate(methodDurationTimerId) // DO NOT MOVE ANYTHING BELOW HERE. } protected open fun initializeGlean() { diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index e07844bd9..a648c64a7 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -65,6 +65,7 @@ import mozilla.components.support.utils.SafeIntent import mozilla.components.support.utils.toSafeIntent import mozilla.components.support.webextensions.WebExtensionPopupFeature import org.mozilla.fenix.GleanMetrics.Metrics +import org.mozilla.fenix.GleanMetrics.PerfStartup import org.mozilla.fenix.addons.AddonDetailsFragmentDirections import org.mozilla.fenix.addons.AddonPermissionsDetailsFragmentDirections import org.mozilla.fenix.browser.browsingmode.BrowsingMode @@ -163,7 +164,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { private lateinit var navigationToolbar: Toolbar - final override fun onCreate(savedInstanceState: Bundle?) { + final override fun onCreate(savedInstanceState: Bundle?): Unit = PerfStartup.homeActivityOnCreate.measure { // DO NOT MOVE ANYTHING ABOVE THIS addMarker CALL. components.core.engine.profiler?.addMarker("Activity.onCreate", "HomeActivity") diff --git a/app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt b/app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt new file mode 100644 index 000000000..c3f21d59a --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt @@ -0,0 +1,35 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix + +import androidx.test.core.app.ApplicationProvider +import mozilla.components.service.glean.testing.GleanTestRule +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.GleanMetrics.PerfStartup +import org.mozilla.fenix.helpers.FenixRobolectricTestRunner + +@RunWith(FenixRobolectricTestRunner::class) +class FenixApplicationTest { + + @get:Rule val gleanTestRule = GleanTestRule(ApplicationProvider.getApplicationContext()) + + private lateinit var application: FenixApplication + + @Before + fun setUp() { + application = ApplicationProvider.getApplicationContext() + } + + @Test + fun `GIVEN onCreate is called THEN the duration is measured`() { + // application.onCreate is called before the test as part of test set up: + // https://robolectric.blogspot.com/2013/04/the-test-lifecycle-in-20.html + assertTrue(PerfStartup.applicationOnCreate.testHasValue()) + } +} diff --git a/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt b/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt index 9d83d5360..9c086a9f5 100644 --- a/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt @@ -6,10 +6,12 @@ package org.mozilla.fenix import android.content.Intent import android.os.Bundle +import androidx.test.core.app.ApplicationProvider import io.mockk.every import io.mockk.mockk import io.mockk.spyk import io.mockk.verify +import mozilla.components.service.glean.testing.GleanTestRule import mozilla.components.support.test.robolectric.testContext import mozilla.components.support.utils.toSafeIntent import org.junit.Assert.assertEquals @@ -18,8 +20,10 @@ import org.junit.Assert.assertNotEquals import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Before +import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import org.mozilla.fenix.GleanMetrics.PerfStartup import org.mozilla.fenix.HomeActivity.Companion.PRIVATE_BROWSING_MODE import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager @@ -28,10 +32,13 @@ import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.settings import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.utils.Settings +import org.robolectric.Robolectric @RunWith(FenixRobolectricTestRunner::class) class HomeActivityTest { + @get:Rule val gleanTestRule = GleanTestRule(ApplicationProvider.getApplicationContext()) + private lateinit var activity: HomeActivity @Before @@ -131,4 +138,20 @@ class HomeActivityTest { assertFalse(activity.isActivityColdStarted(startingIntent, Bundle())) } + + @Test + fun `WHEN onCreate is called THEN the duration is measured`() { + assertFalse(PerfStartup.homeActivityOnCreate.testHasValue()) // sanity check. + + // For some reason, the androidx replacement for this method, ActivityScenario, fails so we + // use the old Robolectric version. Perhaps it's because it forces the Activity to the + // RESUMED state (unlike Robolectric where we can get to CREATED) so not enough code is + // mocked for that to work. + // + // There are various exceptions thrown on background threads when this test runs but it + // doesn't seem to impact correctness so we ignore them. + Robolectric.buildActivity(HomeActivity::class.java) + .create() + assertTrue(PerfStartup.homeActivityOnCreate.testHasValue()) + } } diff --git a/docs/metrics.md b/docs/metrics.md index 413204af1..a3026ee72 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -315,6 +315,8 @@ The following metrics are added to the ping: | perf.awesomebar.session_suggestions |[timing_distribution](https://mozilla.github.io/glean/book/user/metrics/timing_distribution.html) |Duration of a session awesomebar suggestion query. |[1](https://github.com/mozilla-mobile/fenix/pull/10276#pullrequestreview-411101979)||2020-11-15 |1, 2 | | perf.awesomebar.shortcuts_suggestions |[timing_distribution](https://mozilla.github.io/glean/book/user/metrics/timing_distribution.html) |Duration of a shortcuts awesomebar suggestion query. |[1](https://github.com/mozilla-mobile/fenix/pull/10276#pullrequestreview-411101979)||2020-11-15 |1, 2 | | perf.awesomebar.synced_tabs_suggestions |[timing_distribution](https://mozilla.github.io/glean/book/user/metrics/timing_distribution.html) |Duration of a synced tabs awesomebar suggestion query. |[1](https://github.com/mozilla-mobile/fenix/pull/10276#pullrequestreview-411101979)||2020-11-15 |1, 2 | +| perf.startup.application_on_create |[timing_distribution](https://mozilla.github.io/glean/book/user/metrics/timing_distribution.html) |The duration of `FenixApplication.onCreate` in the main process. |[1](todo)||2021-08-11 |1 | +| perf.startup.home_activity_on_create |[timing_distribution](https://mozilla.github.io/glean/book/user/metrics/timing_distribution.html) |The duration of `HomeActivity.onCreate`. |[1](todo)||2021-08-11 |1 | | preferences.accessibility_services |[string_list](https://mozilla.github.io/glean/book/user/metrics/string_list.html) |Whether or not the user has touch exploration or switch services enabled. These are built into the Android OS, not Fenix prefs. default: "" |[1](https://github.com/mozilla-mobile/fenix/pull/11211), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 | | preferences.open_links_in_a_private_tab |[string_list](https://mozilla.github.io/glean/book/user/metrics/string_list.html) |Whether or not the user has enabled open links in a private tab. default: false |[1](https://github.com/mozilla-mobile/fenix/pull/11211), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 | | preferences.open_links_in_app |[string_list](https://mozilla.github.io/glean/book/user/metrics/string_list.html) |Whether or not the user has the open links in apps feature enabled. default: false |[1](https://github.com/mozilla-mobile/fenix/pull/11446), [2](https://github.com/mozilla-mobile/fenix/pull/15713#issuecomment-703972068)||2021-08-01 |2 |