aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/src/jmh/kotlin/benchmarks/flow/SafeFlowBenchmark.kt
blob: d957bdbe0ef2b5dd83e4834ea258504138a05b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package benchmarks.flow

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import org.openjdk.jmh.annotations.*
import java.util.concurrent.*
import benchmarks.flow.scrabble.flow as unsafeFlow
import kotlinx.coroutines.flow.flow as safeFlow

@Warmup(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
open class SafeFlowBenchmark {

    private fun numbersSafe() = safeFlow {
        for (i in 2L..1000L) emit(i)
    }

    private fun numbersUnsafe() = unsafeFlow {
        for (i in 2L..1000L) emit(i)
    }

    @Benchmark
    fun safeNumbers(): Int = runBlocking {
        numbersSafe().count()
    }

    @Benchmark
    fun unsafeNumbers(): Int = runBlocking {
        numbersUnsafe().count()
    }
}