|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package benchmarks.channels |
| 6 | + |
| 7 | +import kotlinx.coroutines.* |
| 8 | +import kotlinx.coroutines.timeunit.* |
| 9 | +import org.openjdk.jmh.annotations.* |
| 10 | + |
| 11 | +/* |
| 12 | + * Benchmark Mode Cnt Score Error Units |
| 13 | + * ReusableCancellableContinuationBenchmark.runCancellable avgt 5 2464.846 ± 24.565 us/op |
| 14 | + * ReusableCancellableContinuationBenchmark.runNonCancellable avgt 5 1112.962 ± 9.709 us/op |
| 15 | + * ReusableCancellableContinuationBenchmark.runReusable avgt 5 1615.746 ± 30.324 us/op |
| 16 | + */ |
| 17 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 18 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 19 | +@Fork(value = 1) |
| 20 | +@BenchmarkMode(Mode.AverageTime) |
| 21 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 22 | +@State(Scope.Benchmark) |
| 23 | +open class ReusableCancellableContinuationBenchmark { |
| 24 | + |
| 25 | + private val iterations = 10_000 |
| 26 | + |
| 27 | + @Volatile |
| 28 | + private var sink: Int = 0 |
| 29 | + |
| 30 | + @Benchmark |
| 31 | + fun runCancellable() = runBlocking { |
| 32 | + val ch = CancellableChannel() |
| 33 | + async { |
| 34 | + repeat(iterations) { ch.send(it) } |
| 35 | + } |
| 36 | + |
| 37 | + async { |
| 38 | + repeat(iterations) { |
| 39 | + sink = ch.receive() |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Benchmark |
| 45 | + fun runNonCancellable() = runBlocking { |
| 46 | + val ch = NonCancellableChannel() |
| 47 | + async { |
| 48 | + repeat(iterations) { ch.send(it) } |
| 49 | + } |
| 50 | + |
| 51 | + async { |
| 52 | + repeat(iterations) { |
| 53 | + sink = ch.receive() |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + fun runReusable() = runBlocking { |
| 60 | + val ch = ReusableCancellabilityChannel() |
| 61 | + async { |
| 62 | + repeat(iterations) { ch.send(it) } |
| 63 | + } |
| 64 | + |
| 65 | + async { |
| 66 | + repeat(iterations) { |
| 67 | + sink = ch.receive() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | +} |
| 73 | + |
0 commit comments