Hello, How are you currently measuring the time s...
# coroutines
h
Hello, How are you currently measuring the time spent running a certain operation? Do you just do a simple block like
Copy code
val startTime = System.currentTimeMillis()
doStuff()
val endTime = System.currentTimeMillis() - startTime
What if we're executing this in a coroutine? What if we're executing coroutines in more than one thread? Should we count suspended time? I've received a request to track how much time we spend running each process, and my initial implementation is similar to what's above, but now I'm second guessing myself, especially because I am just launching one flow per operation, so maybe I should be timing the flows? Or make it simpler and just time the full concurrent block and divide that by the number of runs?
s
For simple cases, Kotlin has
measureTime { … }
functions. But it sounds like maybe you're looking for a tracing solution, like OpenTelemetry. It will take a bit of setup, but that would let you record detailed timings at each stage of an operation, allowing you to distinguish between e.g. time spent waiting for network requests and time spent running code. Lots of popular libraries and frameworks have OpenTelemetry support.
👍 4
h
Sounds good, was hoping that there was some fancy
measureTime
for coroutines, but OpenTelemetry seems like the way. Thank you!