Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package benchmarks

import kotlinx.coroutines.channels.*
import org.openjdk.jmh.annotations.*
import java.util.concurrent.*

@Warmup(iterations = 30, time = 1)
@Measurement(iterations = 30, time = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Fork(1)
open class ChannelNanoBenchmarkConflated {
var channel: Channel<Int> = Channel(Channel.CONFLATED)

@Benchmark
fun trySend() {
channel.trySend(42)
}

@Benchmark
fun trySendTryReceive(): Int {
channel.trySend(42)
return channel.tryReceive().getOrThrow()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package benchmarks

import kotlinx.coroutines.channels.*
import org.openjdk.jmh.annotations.*
import java.util.concurrent.*

@Warmup(iterations = 30, time = 1)
@Measurement(iterations = 30, time = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
open class ChannelNanoBenchmarkUnlimited {
@State(Scope.Benchmark)
open class PrefilledChannelState {
private val list = List(1_000_000) { it }

@Param(value = ["0", "10000", "100000", "1000000"]) // 0, 40, 400 KB, 4 MB
private var prefill = 0

lateinit var channel: Channel<Int>

@Setup(Level.Trial)
fun createPrefilledChannel() {
channel = Channel(Channel.UNLIMITED)
repeat(prefill) {
channel.trySend(list[it])
}
}
}

@Benchmark
fun trySendTryReceive(s: PrefilledChannelState): Int {
s.channel.trySend(42)
return s.channel.tryReceive().getOrThrow()
}
}