androidandroid-jetpack-composemicrobenchmark

Generate baseline profile android for authenticated screen


Android support generate baseline profile to improve performance. By using micro benchmark to interact with UI to get something on screen.

Docs here: https://developer.android.com/topic/performance/benchmarking/macrobenchmark-overview

https://developer.android.com/topic/performance/baselineprofiles/create-baselineprofile

But by using function startActivityAndAwait i only can open login screen, so I could not interact on private screen (logged in screen). I am using firebase auth for login function.

@LargeTest
@RunWith(AndroidJUnit4::class)
class SampleStartupBenchmark {
    @get:Rule
    val benchmarkRule = MacrobenchmarkRule()

    @Test
    fun startup() = benchmarkRule.measureRepeated(
        packageName = TARGET_PACKAGE,
        metrics = listOf(StartupTimingMetric()),
        iterations = DEFAULT_ITERATIONS,
        setupBlock = {
            // Press home button before each run to ensure the starting activity isn't visible.
            pressHome()
        }
    ) {
        // starts default launch activity
        startActivityAndWait()
    }
}

The question is how to interact with private screen to generate expecting baseline profile file?


Solution

  • Not sure another ways but I have to have a login step.

    Main function
    fun MacrobenchmarkScope.startBenchmark() {
        pressHome()
        startActivityAndWait()
    
        loginByPhone()   
    
        // After login, do another steps
    }
    
    Login function
    fun MacrobenchmarkScope.loginByPhone() {
        waitAndClickElement("phoneLoginButton")
    
        setTextFieldValue("phoneNumberTextField", "1222223333")
    
        // Send OPT Next button
        waitAndClickElement("sendOTPButton")
    
        setTextFieldValue("smsVerifyTextField", "123456")
    
        // SMS verify Next button
        waitAndClickElement("verifyOTPButton")
    }
    
    Some helpers
    fun MacrobenchmarkScope.waitAndClickElement(res: String) {
        val btnSelector = By.res(res)
        device.wait(Until.hasObject(btnSelector), 5000)
    
        val buttonObject = device.findObject(btnSelector)
        if (buttonObject == null) {
            Log.e("waitAndClickElement", "error at res=$res")
        }
    
        buttonObject.click()
    }
    
    fun MacrobenchmarkScope.setTextFieldValue(res: String, value: String) {
        val textFieldSelector = By.res(res)
        device.wait(Until.hasObject(textFieldSelector), 5_500)
    
        val textFieldObject = device.findObject(textFieldSelector)
    
        if (textFieldObject == null) {
            Log.e("setTextFieldValue", "error at res=$res")
        }
    
        textFieldObject.text = value
    }
    

    Hope this help someone!