Skip to content

Commit

Permalink
For mozilla-mobile#7847: Improve startup performance of the Fenix wra…
Browse files Browse the repository at this point in the history
…pper around Glean
  • Loading branch information
Jim Porter authored and jimporter committed Mar 5, 2020
1 parent d719823 commit ec98fd9
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
package org.mozilla.fenix.components.metrics

import android.content.Context
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import mozilla.components.service.glean.Glean
import mozilla.components.service.glean.private.NoExtraKeys
import mozilla.components.support.base.log.logger.Logger
Expand Down Expand Up @@ -509,12 +506,6 @@ class GleanMetricsService(private val context: Context) : MetricsService {

private val logger = Logger("GleanMetricsService")
private var initialized = false
/*
* We need to keep an eye on when we are done starting so that we don't
* accidentally stop ourselves before we've ever started.
*/
private lateinit var gleanInitializer: Job
private lateinit var gleanSetStartupMetrics: Job

private val activationPing = ActivationPing(context)

Expand All @@ -526,17 +517,22 @@ class GleanMetricsService(private val context: Context) : MetricsService {
if (initialized) return
initialized = true

// The code below doesn't need to execute immediately, so we'll add them to the visual
// completeness task queue to be run later.
val taskManager = context.components.performance.visualCompletenessTaskManager

// We have to initialize Glean *on* the main thread, because it registers lifecycle
// observers. However, the activation ping must be sent *off* of the main thread,
// because it calls Google ad APIs that must be called *off* of the main thread.
// These two things actually happen in parallel, but that should be ok because Glean
// can handle events being recorded before it's initialized.
gleanInitializer = MainScope().launch {
taskManager.add {
Glean.registerPings(Pings)
}

// setStartupMetrics is not a fast function. It does not need to be done before we can consider
// ourselves initialized. So, let's do it, well, later.
gleanSetStartupMetrics = MainScope().launch {
taskManager.add {
setStartupMetrics()
}
}
Expand Down Expand Up @@ -575,8 +571,6 @@ class GleanMetricsService(private val context: Context) : MetricsService {
}

override fun stop() {
gleanInitializer.cancel()
gleanSetStartupMetrics.cancel()
Glean.setUploadEnabled(false)
}

Expand Down

0 comments on commit ec98fd9

Please sign in to comment.