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

theapache64

05/09/2023, 9:49 AM
is it okay to measure composition time like this?
a

Albert Chang

05/09/2023, 10:00 AM
Never use
System.currentTimeMillis()
to measure elapsed time. Use
System.nanoTime()
or
SystemClock.elapsedRealtime()
on Android.
c

CLOVIS

05/09/2023, 10:01 AM
Even then, there's a lot of stuff going on during recomposition that I don't think is executed then 🤔 I doubt this measures rendering time
a

Albert Chang

05/09/2023, 10:02 AM
Yeah this only measures the time of composition phase in the three phases.
t

theapache64

05/09/2023, 10:03 AM
Sure. am not trying to measure
doFrame
call here anyway. but what’s the issue with
currentTimeMillis
?
t

theapache64

05/09/2023, 10:08 AM
Thanks 🙇 updated
Copy code
@Composable
inline fun MeasureComposition(
    tag: String,
    content: @Composable () -> Unit
) {
    val timestamp = System.nanoTime()
    content()
    val diff = System.nanoTime() - timestamp
    println("$tag took for recomposition: ${diff / 1_000_000}ms")
}
m

marstran

05/09/2023, 10:13 AM
You could use the
measureTime
functions in Kotlin: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/measure-time.html
y

yschimke

05/09/2023, 10:16 AM
What about hooking in with the tracing APIs the compose runtime tracing uses?
t

theapache64

05/09/2023, 10:17 AM
aah.. good idea.. but is it possible? 🤔
y

yschimke

05/09/2023, 10:18 AM
Maybe library restricted...
But must be public in the bytecode.
t

theapache64

05/09/2023, 10:20 AM
@Ben Trengrove [G] can you please confirm the feasibility ?
s

shikasd

05/09/2023, 12:43 PM
This method of tracking recomposition is fine, tracing does almost the same
You can also use
Composer.setTracer
to hook into tracing APIs, but that's going to override default tracing tracker
1
t

theapache64

05/09/2023, 12:59 PM
Cool. So
traceEventStart
will be called for each recomposition and if its shows my composable name like this, it means it got recomposed? What else we can do with this? how can i get more data? Can i get entire
doFrame
call time at the Runtime? ( I’d like to get the measure time as well).. (sorry for asking too many questions at once 😄 this API looks exciting 😬 )
s

shikasd

05/09/2023, 1:01 PM
It is doing exactly the same as the sample above, but wrapped with an interface 🙂
So you can trace when composable is run and duration of that call 🙂
m

mlykotom

05/09/2023, 4:08 PM
I’m not sure if it fits your need, but you could also add a trace section to your composable and use
TraceSectionMetric
from Macrobenchmark.
t

theapache64

05/09/2023, 4:09 PM
@mlykotom but that’ll only appear in the
.trace
file right? I am trying to get those info at runtime
m

mlykotom

05/09/2023, 4:43 PM
Yes, that's only in a trace :/
y

yschimke

05/09/2023, 6:28 PM
I haven't tried them, but remember seeing these APIs, for tracing particular blocks. Mainly because macrobenchmark added them. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:benchmar[…]/PerfettoTrace.kt?q=androidx.benchmark.perfetto.PerfettoTrace Added experimental new APIs
PerfettoTrace.record {}
and
PerfettoTraceRule
to capture Perfetto traces (also known as System Traces) as part of a test, to inspect test behavior and performance.
i

Ian G. Clifton

05/11/2023, 8:20 PM
I’d just like to call out the giant caveat that you will have substantial variability measuring like this even on the same device. For example, the core can be underclocked to preserve battery or due to thermals. If you’re just trying to get some really rough data or look for outliers in a release sample, it’s probably okay. If you want to measure performance so that you can make improvements and verify them, you definitely want to use Macrobenchmark.
k

Kshitij Patil

11/11/2023, 8:32 PM
I have used perfetto traces and macrobenchmark for wider picture of screen. Not able to understand how can we use these to measure performance of just single Composable. Say I’m building some variations of button, how can I get to know which one is performant? Place single button on blank surface and capture perfetto trace for that activity?
t

theapache64

11/12/2023, 6:38 AM
@Kshitij Patil You can use intent extra to have a different behaviours in an activity. Here’s an example, where am checking if Coil is better than native Image component. Hope this helps
👍 1
k

Kshitij Patil

11/12/2023, 7:04 AM
Thanks @theapache64, this is really helpful
👍 1
119 Views