For perf-frontend-issues#33: Add HotStartPerformanceMonitorTest.
We really don't want our startup tests to subtlely change so I thought it was worth writing some quick tests.master
parent
e60141e98d
commit
eef080a46c
|
@ -22,19 +22,22 @@ import android.util.Log
|
||||||
* To see logs from this class, the user must enable VERBOSE logging for the appropriate tag:
|
* To see logs from this class, the user must enable VERBOSE logging for the appropriate tag:
|
||||||
* adb shell setprop log.tag.FenixPerf VERBOSE
|
* adb shell setprop log.tag.FenixPerf VERBOSE
|
||||||
*/
|
*/
|
||||||
class HotStartPerformanceMonitor {
|
class HotStartPerformanceMonitor(
|
||||||
|
private val log: (String) -> Unit = { Log.v(Performance.TAG, it) }, // android log to minimize overhead.
|
||||||
|
private val getElapsedRealtime: () -> Long = { SystemClock.elapsedRealtime() }
|
||||||
|
) {
|
||||||
|
|
||||||
private var onRestartMillis: Long = -1
|
private var onRestartMillis: Long = -1
|
||||||
|
|
||||||
fun onRestartFirstMethodCall() {
|
fun onRestartFirstMethodCall() {
|
||||||
onRestartMillis = SystemClock.elapsedRealtime()
|
onRestartMillis = getElapsedRealtime()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onPostResumeFinalMethodCall() {
|
fun onPostResumeFinalMethodCall() {
|
||||||
// If onRestart was never called, this is not a hot start: ignore it.
|
// If onRestart was never called, this is not a hot start: ignore it.
|
||||||
if (onRestartMillis >= 0) {
|
if (onRestartMillis >= 0) {
|
||||||
val elapsedMillis = SystemClock.elapsedRealtime() - onRestartMillis
|
val elapsedMillis = getElapsedRealtime() - onRestartMillis
|
||||||
Log.v(Performance.TAG, "hot start: $elapsedMillis") // android log to minimize overhead.
|
log("hot start: $elapsedMillis")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/* 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.perf
|
||||||
|
|
||||||
|
import io.mockk.Called
|
||||||
|
import io.mockk.MockKAnnotations
|
||||||
|
import io.mockk.impl.annotations.MockK
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class HotStartPerformanceMonitorTest {
|
||||||
|
|
||||||
|
private lateinit var monitor: HotStartPerformanceMonitor
|
||||||
|
|
||||||
|
@MockK(relaxed = true) private lateinit var log: (String) -> Unit
|
||||||
|
private var elapsedRealtime = 0L
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
MockKAnnotations.init(this)
|
||||||
|
monitor = HotStartPerformanceMonitor(log, getElapsedRealtime = { elapsedRealtime })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN onRestart is not called but onPostResume is called THEN we do not log`() {
|
||||||
|
monitor.onPostResumeFinalMethodCall()
|
||||||
|
verify { log.invoke(any()) wasNot Called }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN onRestart then onPostResume is called THEN we log the elapsed time`() {
|
||||||
|
elapsedRealtime = 10
|
||||||
|
monitor.onRestartFirstMethodCall()
|
||||||
|
|
||||||
|
elapsedRealtime = 100
|
||||||
|
monitor.onPostResumeFinalMethodCall()
|
||||||
|
|
||||||
|
verify { log.invoke("hot start: 90") } // fragile but it's not worth the time to make robust.
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue