Skip to content

Commit 78f3e23

Browse files
committed
Safe flow benchmark
1 parent c438a0e commit 78f3e23

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package benchmarks.flow.misc
6+
7+
import kotlinx.coroutines.*
8+
import kotlinx.coroutines.flow.*
9+
import org.openjdk.jmh.annotations.*
10+
import java.util.concurrent.*
11+
import benchmarks.flow.scrabble.flow as unsafeFlow
12+
import kotlinx.coroutines.flow.flow as safeFlow
13+
14+
@Warmup(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS)
15+
@Measurement(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS)
16+
@Fork(value = 1)
17+
@BenchmarkMode(Mode.AverageTime)
18+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
19+
@State(Scope.Benchmark)
20+
open class SafeFlowBenchmark {
21+
22+
private fun numbersSafe() = safeFlow {
23+
for (i in 2L..1000L) emit(i)
24+
}
25+
26+
private fun numbersUnsafe() = unsafeFlow {
27+
for (i in 2L..1000L) emit(i)
28+
}
29+
30+
@Benchmark
31+
fun safeNumbers(): Int = runBlocking {
32+
numbersSafe().count()
33+
}
34+
35+
@Benchmark
36+
fun unsafeNumbers(): Int = runBlocking {
37+
numbersUnsafe().count()
38+
}
39+
}

0 commit comments

Comments
 (0)