kotlinx-benchmark icon indicating copy to clipboard operation
kotlinx-benchmark copied to clipboard

Support benchmarks having @State annotations in parent classes

Open fzhinkin opened this issue 1 year ago • 2 comments

kotlinx-benchmark requires benchmark classes to be annotated with @State.

It seems reasonable to expect that when there is a base class annotated with @State providing common routines and state for subclasses, and then it should be enough to simply extend that class w/o explicitly annotating leaf class w/ @State.

Here's an example of such code reuse via inheritance from kotlinx-io benchmarks:

@State(Scope.Benchmark)
abstract class BufferRWBenchmarkBase {
    protected val buffer = Buffer()
    @Setup fun setupBuffers() { /* some setup common for all subclasses */ }
}

open class ByteBenchmark : BufferRWBenchmarkBase() {
    @Benchmark fun benchmark(): Byte {
        buffer.writeByte(0x42)
        return buffer.readByte()
    }
}

open class ShortBenchmark : BufferRWBenchmarkBase() {
    @Benchmark fun benchmark(): Short {
        buffer.writeShort(42)
        return buffer.readShort()
    }
}

...

Also, it would be nice to verify that if benchmark-class and its superclass are both annotated w/ @State with the same scope. The condition would be always true now, but may no longer hold in the future.

My expectations regarding State inheritance are based on how it works in JMH, and because that's a different framework it not necessarily should be true for kotlinx-benchmark. However, if the proposal will be rejected, it would be nice to print warning for the classes having @Benchmark-methods, but not annotated w/ the @State.

fzhinkin avatar Jul 19 '23 13:07 fzhinkin

Hi! can I pick this one up?

07jasjeet avatar Mar 13 '24 11:03 07jasjeet

@07jasjeet Yes please

qurbonzoda avatar Mar 13 '24 12:03 qurbonzoda