https://kotlinlang.org logo
#compose
Title
# compose
s

Simon Stahl

05/12/2022, 8:04 PM
Is there a good way to measure time to UI in compose? Meaning, can I measure how long it takes to fully render a UI?
A naive approach would be to just measure the time it takes to exectue the root composable, but that doesn't really render the UI yet. Also some things like
LazyLists
are not called synchronously as the below exampel shows.
Copy code
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        Log.e("Simon", "MainActivity.onCreate() ... start benchmark")
        ComposeTestTheme {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colors.background
            ) {
                LazyColumn {
                    items(100) {
                        Log.e("Simon", "MainActivity.onCreate() ... compose item $it")
                        Text(
                            modifier = Modifier.padding(10.dp),
                            text = "Item $it"
                        )
                    }
                }
            }
        }
        Log.e("Simon", "MainActivity.onCreate() ... stop benchmark")
    }
}
Log output:
Copy code
MainActivity.onCreate() ... start benchmark
MainActivity.onCreate() ... stop benchmark
MainActivity.onCreate() ... compose item 0
MainActivity.onCreate() ... compose item 1
...
b

Ben Trengrove [G]

05/12/2022, 9:37 PM
Macrobenchmark is what you need
s

Simon Stahl

05/12/2022, 10:50 PM
Hey @Ben Trengrove [G]. Glad to see you here and thanks for the answer. I've looked into macrobenchmark a little while ago, but I wasn't aware that you can run it right out of AS. Although what we are looking for is something that we could permanently leave in the app to get benchmark data from all out users and I don't think macrobenchmark is supposed to stay in the release, or is it? The idea is that for each page, we can get averaged data about how much time is spend for network calls, db queries, ui rendering etc
b

Ben Trengrove [G]

05/12/2022, 11:31 PM
No you are correct, macrobenchmark is for local or CI testing. Not production
I don't think we have a tool for collecting average render times in production. We did just release JankStats though, https://developer.android.com/topic/performance/jankstats
You might be able to take some ideas from it to implement this yourself
s

Simon Stahl

05/12/2022, 11:45 PM
Oh yea, I've looked at that too, but not in too much details. I guess I'll give it another shot and see if I can use it for what we want
Thanks for the response 🙂
16 Views