|
| 1 | +package zio.internal |
| 2 | + |
| 3 | +import com.github.ghik.silencer.silent |
| 4 | +import org.openjdk.jmh.annotations._ |
| 5 | +import org.openjdk.jmh.infra.Blackhole |
| 6 | +import org.springframework.util.{ConcurrentReferenceHashMap => SpringConcurrentReferenceHashMap} |
| 7 | + |
| 8 | +import java.util |
| 9 | +import java.util.Collections |
| 10 | +import java.util.concurrent.{ConcurrentLinkedQueue, TimeUnit} |
| 11 | +import java.util.concurrent.atomic.AtomicInteger |
| 12 | + |
| 13 | +@State(Scope.Benchmark) |
| 14 | +private[this] class AddContext extends BaseContext { |
| 15 | + private var idx: AtomicInteger = _ |
| 16 | + private var refs: ConcurrentLinkedQueue[TestKey] = _ |
| 17 | + |
| 18 | + @Setup(Level.Iteration) |
| 19 | + def setup(): Unit = { |
| 20 | + this.idx = new AtomicInteger(-1) |
| 21 | + this.refs = new ConcurrentLinkedQueue[TestKey]() |
| 22 | + this.javaSetInitializer = { _ => createJavaSet() } |
| 23 | + this.springMapInitializer = { _ => createSpringMap() } |
| 24 | + this.zioSetInitializer = { _ => createZioSet() } |
| 25 | + this.setupBase() |
| 26 | + } |
| 27 | + |
| 28 | + def createCachedKey(): TestKey = { |
| 29 | + val key = TestKey(this.idx.incrementAndGet()) |
| 30 | + this.refs.add(key) |
| 31 | + key |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 36 | +@Warmup(iterations = 2, time = 2) |
| 37 | +@Measurement(iterations = 2, time = 2) |
| 38 | +@Fork(1) |
| 39 | +private[this] class ConcurrentWeakHashSetAddBenchmark { |
| 40 | + |
| 41 | + @Benchmark |
| 42 | + def javaAddSerial(ctx: AddContext, blackhole: Blackhole): Unit = |
| 43 | + blackhole.consume(ctx.javaSet.add(ctx.createCachedKey())) |
| 44 | + |
| 45 | + @Threads(6) |
| 46 | + @Benchmark |
| 47 | + def javaAddConcurrent(ctx: AddContext, blackhole: Blackhole): Unit = |
| 48 | + blackhole.consume(ctx.javaSet.add(ctx.createCachedKey())) |
| 49 | + |
| 50 | + @Benchmark |
| 51 | + def springAddSerial(ctx: AddContext, blackhole: Blackhole): Unit = |
| 52 | + blackhole.consume(ctx.springMap.put(ctx.createCachedKey(), true)) |
| 53 | + |
| 54 | + @Threads(6) |
| 55 | + @Benchmark |
| 56 | + def springAddConcurrent(ctx: AddContext, blackhole: Blackhole): Unit = |
| 57 | + blackhole.consume(ctx.springMap.put(ctx.createCachedKey(), true)) |
| 58 | + |
| 59 | + @Benchmark |
| 60 | + def zioAddSerial(ctx: AddContext, blackhole: Blackhole): Unit = |
| 61 | + blackhole.consume(ctx.zioSet.add(ctx.createCachedKey())) |
| 62 | + |
| 63 | + @Threads(6) |
| 64 | + @Benchmark |
| 65 | + def zioAddConcurrent(ctx: AddContext, blackhole: Blackhole): Unit = |
| 66 | + blackhole.consume(ctx.zioSet.add(ctx.createCachedKey())) |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +@State(Scope.Benchmark) |
| 71 | +private[this] class RemoveContext extends BaseContext { |
| 72 | + private val sampleSize = 100000 |
| 73 | + private val values: Array[TestKey] = (0 to this.sampleSize).map(TestKey).toArray |
| 74 | + private val idx: AtomicInteger = new AtomicInteger(this.sampleSize + 1) |
| 75 | + |
| 76 | + @Setup(Level.Iteration) |
| 77 | + def setup(): Unit = { |
| 78 | + this.javaSetInitializer = { _ => createJavaSet(this.values) } |
| 79 | + this.springMapInitializer = { _ => createSpringMap(this.values) } |
| 80 | + this.zioSetInitializer = { _ => createZioSet(this.values) } |
| 81 | + this.setupBase() |
| 82 | + } |
| 83 | + |
| 84 | + def appendNewKeyToJavaSet(): TestKey = { |
| 85 | + val key = TestKey(this.idx.incrementAndGet()) |
| 86 | + this.javaSet.add(key) |
| 87 | + key |
| 88 | + } |
| 89 | + |
| 90 | + def appendNewKeyToSpringMap(): TestKey = { |
| 91 | + val key = TestKey(this.idx.incrementAndGet()) |
| 92 | + this.springMap.put(key, true) |
| 93 | + key |
| 94 | + } |
| 95 | + |
| 96 | + def appendNewKeyToZioSet(): TestKey = { |
| 97 | + val key = TestKey(this.idx.incrementAndGet()) |
| 98 | + this.zioSet.add(key) |
| 99 | + key |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 104 | +@Warmup(iterations = 2, time = 2) |
| 105 | +@Measurement(iterations = 2, time = 2) |
| 106 | +@Fork(1) |
| 107 | +private[this] class ConcurrentWeakHashSetRemoveBenchmark { |
| 108 | + |
| 109 | + @Benchmark |
| 110 | + def javaRemoveSerial(ctx: RemoveContext, blackhole: Blackhole): Unit = |
| 111 | + blackhole.consume(ctx.javaSet.remove(ctx.appendNewKeyToJavaSet())) |
| 112 | + |
| 113 | + @Threads(6) |
| 114 | + @Benchmark |
| 115 | + def javaRemoveConcurrent(ctx: RemoveContext, blackhole: Blackhole): Unit = |
| 116 | + blackhole.consume(ctx.javaSet.remove(ctx.appendNewKeyToJavaSet())) |
| 117 | + |
| 118 | + @Benchmark |
| 119 | + def springRemoveSerial(ctx: RemoveContext, blackhole: Blackhole): Unit = |
| 120 | + blackhole.consume(ctx.springMap.remove(ctx.appendNewKeyToSpringMap())) |
| 121 | + |
| 122 | + @Threads(6) |
| 123 | + @Benchmark |
| 124 | + def springRemoveConcurrent(ctx: RemoveContext, blackhole: Blackhole): Unit = |
| 125 | + blackhole.consume(ctx.springMap.remove(ctx.appendNewKeyToSpringMap())) |
| 126 | + |
| 127 | + @Benchmark |
| 128 | + def zioRemoveSerial(ctx: RemoveContext, blackhole: Blackhole): Unit = |
| 129 | + blackhole.consume(ctx.zioSet.remove(ctx.appendNewKeyToZioSet())) |
| 130 | + |
| 131 | + @Threads(6) |
| 132 | + @Benchmark |
| 133 | + def zioRemoveConcurrent(ctx: RemoveContext, blackhole: Blackhole): Unit = |
| 134 | + blackhole.consume(ctx.zioSet.remove(ctx.appendNewKeyToZioSet())) |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +@State(Scope.Benchmark) |
| 139 | +private[this] class IterateContext extends BaseContext { |
| 140 | + private val sampleSize = 1000 |
| 141 | + private val values: Array[TestKey] = (0 to this.sampleSize).map(TestKey).toArray |
| 142 | + |
| 143 | + @Setup(Level.Iteration) |
| 144 | + def setup(): Unit = { |
| 145 | + this.javaSetInitializer = { _ => createJavaSet(this.values) } |
| 146 | + this.springMapInitializer = { _ => createSpringMap(this.values) } |
| 147 | + this.zioSetInitializer = { _ => createZioSet(this.values) } |
| 148 | + this.setupBase() |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 153 | +@Warmup(iterations = 2, time = 2) |
| 154 | +@Measurement(iterations = 2, time = 2) |
| 155 | +@Fork(1) |
| 156 | +private[this] class ConcurrentWeakHashSetIterateBenchmark { |
| 157 | + |
| 158 | + @Benchmark |
| 159 | + def javaIterateSerial(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 160 | + ctx.javaSet.forEach(element => blackhole.consume(element)) |
| 161 | + |
| 162 | + @Threads(6) |
| 163 | + @Benchmark |
| 164 | + def javaIterateConcurrent(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 165 | + ctx.javaSet.forEach(element => blackhole.consume(element)) |
| 166 | + |
| 167 | + @Benchmark |
| 168 | + def springIterateSerial(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 169 | + ctx.springMap.forEach((key, _) => blackhole.consume(key)) |
| 170 | + |
| 171 | + @Threads(6) |
| 172 | + @Benchmark |
| 173 | + def springIterateConcurrent(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 174 | + ctx.springMap.forEach((key, _) => blackhole.consume(key)) |
| 175 | + |
| 176 | + @Benchmark |
| 177 | + def zioIterateSerial(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 178 | + ctx.zioSet.foreach(element => blackhole.consume(element)) |
| 179 | + |
| 180 | + @Threads(6) |
| 181 | + @Benchmark |
| 182 | + def zioIterateConcurrent(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 183 | + ctx.zioSet.foreach(element => blackhole.consume(element)) |
| 184 | + |
| 185 | +} |
| 186 | + |
| 187 | +@State(Scope.Benchmark) |
| 188 | +private[this] class ContainsContext extends BaseContext { |
| 189 | + private val sampleSize = 100000 |
| 190 | + private val values: Array[TestKey] = (0 to this.sampleSize).map(TestKey).toArray |
| 191 | + |
| 192 | + @Setup(Level.Iteration) |
| 193 | + def setup(): Unit = { |
| 194 | + this.javaSetInitializer = { _ => createJavaSet(this.values) } |
| 195 | + this.springMapInitializer = { _ => createSpringMap(this.values) } |
| 196 | + this.zioSetInitializer = { _ => createZioSet(this.values) } |
| 197 | + this.setupBase() |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 202 | +@Warmup(iterations = 2, time = 2) |
| 203 | +@Measurement(iterations = 2, time = 2) |
| 204 | +@Fork(1) |
| 205 | +private[this] class ConcurrentWeakHashSetContainsBenchmark { |
| 206 | + |
| 207 | + @Benchmark |
| 208 | + def javaContainsSerial(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 209 | + blackhole.consume(ctx.javaSet.contains(TestKey(50000))) |
| 210 | + |
| 211 | + @Threads(6) |
| 212 | + @Benchmark |
| 213 | + def javaContainsConcurrent(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 214 | + blackhole.consume(ctx.javaSet.contains(TestKey(50000))) |
| 215 | + |
| 216 | + @Benchmark |
| 217 | + def springContainsSerial(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 218 | + blackhole.consume(ctx.springMap.containsKey(TestKey(50000))) |
| 219 | + |
| 220 | + @Threads(6) |
| 221 | + @Benchmark |
| 222 | + def springContainsConcurrent(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 223 | + blackhole.consume(ctx.springMap.containsKey(TestKey(50000))) |
| 224 | + |
| 225 | + @Benchmark |
| 226 | + def zioContainsSerial(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 227 | + blackhole.consume(ctx.zioSet.contains(TestKey(50000))) |
| 228 | + |
| 229 | + @Threads(6) |
| 230 | + @Benchmark |
| 231 | + def zioContainsConcurrent(ctx: IterateContext, blackhole: Blackhole): Unit = |
| 232 | + blackhole.consume(ctx.zioSet.contains(TestKey(50000))) |
| 233 | + |
| 234 | +} |
| 235 | + |
| 236 | +private[this] case class TestKey(name: Int) |
| 237 | + |
| 238 | +private[this] class BaseContext { |
| 239 | + |
| 240 | + protected var javaSetInitializer: Unit => util.Set[TestKey] = _ |
| 241 | + var javaSet: util.Set[TestKey] = _ |
| 242 | + |
| 243 | + protected var springMapInitializer: Unit => SpringConcurrentReferenceHashMap[TestKey, Boolean] = _ |
| 244 | + var springMap: SpringConcurrentReferenceHashMap[TestKey, Boolean] = _ |
| 245 | + |
| 246 | + protected var zioSetInitializer: Unit => ConcurrentWeakHashSet[TestKey] = _ |
| 247 | + var zioSet: ConcurrentWeakHashSet[TestKey] = _ |
| 248 | + |
| 249 | + protected def setupBase(): Unit = { |
| 250 | + this.javaSet = this.javaSetInitializer(()) |
| 251 | + this.springMap = this.springMapInitializer(()) |
| 252 | + this.zioSet = this.zioSetInitializer(()) |
| 253 | + } |
| 254 | + |
| 255 | + protected def createJavaSet(values: Array[TestKey] = new Array[TestKey](0)): util.Set[TestKey] = { |
| 256 | + import scala.jdk.CollectionConverters._ |
| 257 | + val set = Collections.synchronizedSet(Collections.newSetFromMap(new util.WeakHashMap[TestKey, java.lang.Boolean]())) |
| 258 | + set.addAll(values.toSet.asJava): @silent("JavaConverters") |
| 259 | + set |
| 260 | + } |
| 261 | + |
| 262 | + protected def createSpringMap( |
| 263 | + values: Array[TestKey] = new Array(0) |
| 264 | + ): SpringConcurrentReferenceHashMap[TestKey, Boolean] = { |
| 265 | + import scala.jdk.CollectionConverters._ |
| 266 | + val map = |
| 267 | + new SpringConcurrentReferenceHashMap[TestKey, Boolean](16, SpringConcurrentReferenceHashMap.ReferenceType.WEAK) |
| 268 | + map.putAll(values.map((_, true)).toMap.asJava): @silent("JavaConverters") |
| 269 | + map |
| 270 | + } |
| 271 | + |
| 272 | + protected def createZioSet(values: Array[TestKey] = new Array(0)): ConcurrentWeakHashSet[TestKey] = { |
| 273 | + val set = ConcurrentWeakHashSet[TestKey]() |
| 274 | + set.addAll(values) |
| 275 | + set |
| 276 | + } |
| 277 | + |
| 278 | +} |
0 commit comments