|
| 1 | +package com.caplin.integration.datasourcex.util.flow |
| 2 | + |
| 3 | +import com.caplin.integration.datasourcex.util.flow.MapEvent.EntryEvent.Upsert |
| 4 | +import com.caplin.integration.datasourcex.util.flow.MapEvent.Populated |
| 5 | +import java.util.concurrent.TimeUnit |
| 6 | +import kotlinx.coroutines.CoroutineScope |
| 7 | +import kotlinx.coroutines.Dispatchers |
| 8 | +import kotlinx.coroutines.cancel |
| 9 | +import kotlinx.coroutines.flow.flow |
| 10 | +import kotlinx.coroutines.flow.launchIn |
| 11 | +import kotlinx.coroutines.flow.take |
| 12 | +import kotlinx.coroutines.flow.takeWhile |
| 13 | +import kotlinx.coroutines.runBlocking |
| 14 | +import org.openjdk.jmh.annotations.* |
| 15 | + |
| 16 | +/** |
| 17 | + * Benchmarks for [FlowMap] implementation, focusing on mutation performance, lookup efficiency, and |
| 18 | + * Flow-based state reconstruction. |
| 19 | + */ |
| 20 | +@State(Scope.Benchmark) |
| 21 | +@BenchmarkMode(Mode.Throughput) |
| 22 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 23 | +@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) |
| 24 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 25 | +@Fork(1) |
| 26 | +open class FlowMapBenchmark { |
| 27 | + |
| 28 | + private lateinit var flowMap: MutableFlowMap<String, String> |
| 29 | + |
| 30 | + @Setup |
| 31 | + fun setup() { |
| 32 | + flowMap = mutableFlowMapOf() |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Measures the throughput of single [MutableFlowMap.put] operations on an initially empty map. |
| 37 | + */ |
| 38 | + @Benchmark |
| 39 | + fun putSingle() { |
| 40 | + flowMap.put("key", "value") |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Measures the throughput of a [MutableFlowMap.put] followed by a [MutableFlowMap.remove] on the |
| 45 | + * same key, exercising the event emission and state update logic. |
| 46 | + */ |
| 47 | + @Benchmark |
| 48 | + fun putAndRemove() { |
| 49 | + flowMap.put("key", "value") |
| 50 | + flowMap.remove("key") |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Measures the throughput of [MutableFlowMap.putAll] with a small map, which triggers multiple |
| 55 | + * events in a single state update. |
| 56 | + */ |
| 57 | + @Benchmark |
| 58 | + fun putAllSmall() { |
| 59 | + flowMap.putAll(mapOf("1" to "A", "2" to "B", "3" to "C")) |
| 60 | + } |
| 61 | + |
| 62 | + /** State holder for a [FlowMap] pre-populated with 1,000 entries. */ |
| 63 | + @State(Scope.Benchmark) |
| 64 | + open class PopulatedFlowMapState { |
| 65 | + lateinit var flowMap: MutableFlowMap<String, Int> |
| 66 | + |
| 67 | + @Setup |
| 68 | + fun setup() { |
| 69 | + flowMap = mutableFlowMapOf() |
| 70 | + repeat(1000) { flowMap.put("key$it", it) } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** State holder for measuring mutation throughput with multiple active subscribers. */ |
| 75 | + @State(Scope.Benchmark) |
| 76 | + open class ActiveSubscriberState { |
| 77 | + @Param("1", "10", "100") var subscriberCount: Int = 0 |
| 78 | + |
| 79 | + lateinit var flowMap: MutableFlowMap<String, String> |
| 80 | + lateinit var scope: CoroutineScope |
| 81 | + |
| 82 | + @Setup |
| 83 | + fun setup() { |
| 84 | + flowMap = mutableFlowMapOf() |
| 85 | + // Using Dispatchers.Default for subscribers to simulate real-world processing |
| 86 | + scope = CoroutineScope(Dispatchers.Default) |
| 87 | + repeat(subscriberCount) { flowMap.asFlow().launchIn(scope) } |
| 88 | + } |
| 89 | + |
| 90 | + @TearDown |
| 91 | + fun tearDown() { |
| 92 | + scope.cancel() |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Measures the throughput of [MutableFlowMap.put] when there are multiple active subscribers |
| 98 | + * collecting from the map. This identifies contention or overhead in the event dispatching logic. |
| 99 | + */ |
| 100 | + @Benchmark |
| 101 | + fun putWithSubscribers(state: ActiveSubscriberState) { |
| 102 | + state.flowMap.put("key", "value") |
| 103 | + } |
| 104 | + |
| 105 | + /** Measures the throughput of retrieving a value from a large, pre-populated [FlowMap]. */ |
| 106 | + @Benchmark |
| 107 | + fun getFromLargeMap(state: PopulatedFlowMapState): Int? { |
| 108 | + return state.flowMap["key500"] |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Measures the time taken to collect the initial state of a large [FlowMap] via [FlowMap.asFlow]. |
| 113 | + */ |
| 114 | + @Benchmark |
| 115 | + fun asFlowCollection(state: PopulatedFlowMapState) = runBlocking { |
| 116 | + state.flowMap |
| 117 | + .asFlow() |
| 118 | + .takeWhile { it != Populated } |
| 119 | + .collect { |
| 120 | + // just collect |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Measures the time taken to collect the initial state of a large [FlowMap] via |
| 126 | + * [FlowMap.asFlowWithState]. This avoids emitting individual Upsert events, making it much |
| 127 | + * faster. |
| 128 | + */ |
| 129 | + @Benchmark |
| 130 | + fun asFlowWithStateCollection(state: PopulatedFlowMapState) = runBlocking { |
| 131 | + state.flowMap.asFlowWithState().take(1).collect { |
| 132 | + // just collect |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Measures the time taken to collect the initial state of a large [FlowMap] via [FlowMap.asFlow] |
| 138 | + * when a predicate is applied, exercising the filtering logic within the flow. |
| 139 | + */ |
| 140 | + @Benchmark |
| 141 | + fun asFlowWithPredicateCollection(state: PopulatedFlowMapState) = runBlocking { |
| 142 | + state.flowMap |
| 143 | + .asFlow { _, value -> value % 2 == 0 } |
| 144 | + .takeWhile { it != Populated } |
| 145 | + .collect { |
| 146 | + // just collect |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Measures the overhead of reconstructing a [FlowMap] from a stream of events using |
| 152 | + * [toFlowMapIn]. |
| 153 | + */ |
| 154 | + @Benchmark |
| 155 | + fun toFlowMapInBenchmark() = runBlocking { |
| 156 | + val events = flow { |
| 157 | + repeat(100) { emit(Upsert("key$it", null, it)) } |
| 158 | + emit(Populated) |
| 159 | + } |
| 160 | + val scope = CoroutineScope(Dispatchers.Default) |
| 161 | + events.toFlowMapIn(scope) |
| 162 | + scope.cancel() |
| 163 | + } |
| 164 | +} |
0 commit comments