Kotlin / kotlinx-benchmark

Kotlin multiplatform benchmarking toolkit
Apache License 2.0
513 stars 41 forks source link

Support usages of Blackhole outside of the @Benchmark methods #253

Open qwwdfsad opened 2 months ago

qwwdfsad commented 2 months ago

Real-world use-case: https://github.com/sellmair/evas/blob/sellmair/evas/src/commonBenchmark/kotlin/io/sellmair/evas/benchmark/events/EmitBenchmark.kt

The scenario is the following: @Setup sets up the target entity/system and sets up a data egress that is repeatedly invoked from the target benchmark method. For the benchmark to be representative, egress should properly consume the data.

The boiled down example might be the following:

lateinit var flow: Flow<Int>

@Setup 
fun prepare(bh: Blackhole) {
    flow = createFlow().onEach { bh.consume(it) }
}

@Benchmark
fun collect() {
    // Measure collect only and the cost of the data flow
    flow.collect()
}