An example of basic performance measurement
Here’s a basic example of an operation to be measured:
fun sampleOperation() { Thread.sleep(1) }
The goal of this example is to find out the following aspects:
- Throughput: How many operations can be performed in a second
- Latency: How long it takes to finish an operation on average
A small function, measureTotalTimeElapsed
, must be defined to measure the total time elapsed for all iterations of the operation:
fun measureTotalTimeElapsed( iterations: Int, operation: (Int) -> Unit, ): Long = measureTimeMillis { repeat(iterations, operation) }
This function uses the measureTimeMillis
Kotlin function from Standard Library to capture the time spent in repeating the operation.
Finally, this is the main
function to launch the test:
fun main...