@Around aspect and Kotlin suspended function

Viewed 1657

I try to understand how to create an @Around aspect for a Kotlin’s suspended function (for example, to measure time spent in this function, or for a custom @Transactional aspect):

@Timed("my-timer")
suspend fun test() {
    println("before")
    delay(50) // invokes ProceedingJoinPoint#proceed() before this line
    println("after")
}

Since this function has a suspend function call, the @Around aspect’s proceed function will be invoked right before delay() call. But obviously I’d like to measure full time spent in the function.

What is the right way to solve it? Maybe I can somehow subscribe on the last continuation in the method, or smth like that?

2 Answers

I think you can solve your problem easily, if you want to measure, the execution time of function, you can do it using built-in functionality like this:

val time = measureTimeMillis {
    // yourSuperFunc()
}

Also, you can use measureNanoTime. For full reference look at here.

Related