Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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()
}
}
19 changes: 19 additions & 0 deletions benchmarks/src/jmh/kotlin/benchmarks/examples/CounterBenchmark.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package benchmarks.examples

import org.openjdk.jmh.annotations.*
import java.util.concurrent.TimeUnit

@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Fork(1)
open class CounterBenchmark {
var counter: ULong = 0u

@Benchmark
fun inc() {
counter++
}
}
48 changes: 48 additions & 0 deletions benchmarks/src/jmh/kotlin/benchmarks/examples/ListBenchmark.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package benchmarks.examples

import org.openjdk.jmh.annotations.*
import java.util.concurrent.TimeUnit

@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
open class ListBenchmark {
@State(Scope.Benchmark)
open class PrefilledList {
@Param("0", "100000", "1000000", "10000000")
var prefill = 0

val lst: MutableList<Int> = mutableListOf(42)

@Setup(Level.Iteration)
fun setup() {
repeat(prefill) { lst.add(it) }
}
}

@Benchmark
fun add(s: PrefilledList) {
s.lst.add(42)
}

@State(Scope.Benchmark)
open class PrefilledReusableList {
@Param("0", "100000", "1000000", "10000000")
var prefill = 0

val lst: MutableList<Int> = mutableListOf(42)

@Setup(Level.Trial)
fun setup() {
repeat(prefill) { lst.add(it) }
}
}

@Benchmark
fun addRemoveLast(s: PrefilledReusableList) {
s.lst.add(42)
s.lst.removeLast()
}
}