Skip to content

Commit 73d1e46

Browse files
committed
Introduce ChannelSinkBenchmark
1 parent 990feca commit 73d1e46

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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
6+
7+
import kotlinx.coroutines.experimental.*
8+
import kotlinx.coroutines.experimental.channels.*
9+
import org.openjdk.jmh.annotations.*
10+
import java.util.concurrent.*
11+
import kotlin.coroutines.experimental.*
12+
13+
@Warmup(iterations = 10, time = 1)
14+
@Measurement(iterations = 10, time = 1)
15+
@BenchmarkMode(Mode.AverageTime)
16+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
17+
@State(Scope.Benchmark)
18+
@Fork(2)
19+
open class ChannelSinkBenchmark {
20+
21+
@Benchmark
22+
fun channelPipeline(): Int = runBlocking {
23+
Channel
24+
.range(1, 1_000_000, Unconfined)
25+
.filter(Unconfined) { it % 4 == 0 }
26+
.fold(0) { a, b -> a + b }
27+
}
28+
29+
private fun Channel.Factory.range(start: Int, count: Int, context: CoroutineContext) = produce(context) {
30+
for (i in start until (start + count))
31+
send(i)
32+
}
33+
}

0 commit comments

Comments
 (0)