|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.common.queue; |
| 10 | + |
| 11 | +import org.openjdk.jmh.annotations.Benchmark; |
| 12 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 13 | +import org.openjdk.jmh.annotations.Fork; |
| 14 | +import org.openjdk.jmh.annotations.Level; |
| 15 | +import org.openjdk.jmh.annotations.Measurement; |
| 16 | +import org.openjdk.jmh.annotations.Mode; |
| 17 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 18 | +import org.openjdk.jmh.annotations.Param; |
| 19 | +import org.openjdk.jmh.annotations.Scope; |
| 20 | +import org.openjdk.jmh.annotations.Setup; |
| 21 | +import org.openjdk.jmh.annotations.State; |
| 22 | +import org.openjdk.jmh.annotations.Threads; |
| 23 | +import org.openjdk.jmh.annotations.Warmup; |
| 24 | +import org.openjdk.jmh.infra.Blackhole; |
| 25 | + |
| 26 | +import java.util.LinkedList; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.concurrent.locks.ReentrantLock; |
| 29 | + |
| 30 | +/** |
| 31 | + * JMH benchmark for {@link LockableConcurrentQueue} measuring throughput of |
| 32 | + * lock-and-poll / add-and-unlock cycles under varying concurrency levels. |
| 33 | + */ |
| 34 | +@Fork(3) |
| 35 | +@Warmup(iterations = 5, time = 1) |
| 36 | +@Measurement(iterations = 10, time = 1) |
| 37 | +@BenchmarkMode(Mode.Throughput) |
| 38 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 39 | +@State(Scope.Benchmark) |
| 40 | +@SuppressWarnings("unused") |
| 41 | +public class LockableConcurrentQueueBenchmark { |
| 42 | + |
| 43 | + @Param({ "1", "4", "8" }) |
| 44 | + int concurrency; |
| 45 | + |
| 46 | + @Param({ "16", "64" }) |
| 47 | + int poolSize; |
| 48 | + |
| 49 | + private LockableConcurrentQueue<LockableEntry> queue; |
| 50 | + |
| 51 | + @Setup(Level.Iteration) |
| 52 | + public void setup() { |
| 53 | + queue = new LockableConcurrentQueue<>(LinkedList::new, concurrency); |
| 54 | + for (int i = 0; i < poolSize; i++) { |
| 55 | + LockableEntry entry = new LockableEntry(); |
| 56 | + entry.lock(); |
| 57 | + queue.addAndUnlock(entry); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Benchmark |
| 62 | + @Threads(1) |
| 63 | + public LockableEntry pollAndReturn_1thread() { |
| 64 | + return pollAndReturn(); |
| 65 | + } |
| 66 | + |
| 67 | + @Benchmark |
| 68 | + @Threads(4) |
| 69 | + public LockableEntry pollAndReturn_4threads() { |
| 70 | + return pollAndReturn(); |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + @Threads(8) |
| 75 | + public LockableEntry pollAndReturn_8threads() { |
| 76 | + return pollAndReturn(); |
| 77 | + } |
| 78 | + |
| 79 | + private LockableEntry pollAndReturn() { |
| 80 | + LockableEntry entry = queue.lockAndPoll(); |
| 81 | + if (entry != null) { |
| 82 | + queue.addAndUnlock(entry); |
| 83 | + } |
| 84 | + return entry; |
| 85 | + } |
| 86 | + |
| 87 | + @Benchmark |
| 88 | + @Threads(4) |
| 89 | + public void writerWorkload_4threads(Blackhole bh) { |
| 90 | + writerWorkload(bh); |
| 91 | + } |
| 92 | + |
| 93 | + @Benchmark |
| 94 | + @Threads(8) |
| 95 | + public void writerWorkload_8threads(Blackhole bh) { |
| 96 | + writerWorkload(bh); |
| 97 | + } |
| 98 | + |
| 99 | + @Benchmark |
| 100 | + @Threads(16) |
| 101 | + public void writerWorkload_16threads(Blackhole bh) { |
| 102 | + writerWorkload(bh); |
| 103 | + } |
| 104 | + |
| 105 | + private void writerWorkload(Blackhole bh) { |
| 106 | + LockableEntry entry = queue.lockAndPoll(); |
| 107 | + if (entry != null) { |
| 108 | + bh.consume(simulateWork(entry)); |
| 109 | + queue.addAndUnlock(entry); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static long simulateWork(LockableEntry entry) { |
| 114 | + long result = entry.hashCode(); |
| 115 | + for (int i = 0; i < 20; i++) { |
| 116 | + result ^= (result << 13); |
| 117 | + result ^= (result >> 7); |
| 118 | + result ^= (result << 17); |
| 119 | + } |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + static final class LockableEntry implements Lockable { |
| 124 | + private final ReentrantLock lock = new ReentrantLock(); |
| 125 | + |
| 126 | + @Override |
| 127 | + public void lock() { |
| 128 | + lock.lock(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean tryLock() { |
| 133 | + return lock.tryLock(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void unlock() { |
| 138 | + lock.unlock(); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments