Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #17969: add duration probes for App.onCreate and HomeActivity.onC…
Browse files Browse the repository at this point in the history
…reate.
  • Loading branch information
mcomella committed Feb 19, 2021
1 parent 6e0a648 commit 993428c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
32 changes: 32 additions & 0 deletions app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/mozilla/fenix/FenixApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
35 changes: 35 additions & 0 deletions app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt
Original file line number Diff line number Diff line change
@@ -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())
}
}
23 changes: 23 additions & 0 deletions app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())
}
}
2 changes: 2 additions & 0 deletions docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down

0 comments on commit 993428c

Please sign in to comment.