diff --git a/jctools/src/main/java/com/baeldung/jctools/MpmcBenchmark.java b/jctools/src/main/java/com/baeldung/jctools/MpmcBenchmark.java index 0e543fa609..7b754bf709 100644 --- a/jctools/src/main/java/com/baeldung/jctools/MpmcBenchmark.java +++ b/jctools/src/main/java/com/baeldung/jctools/MpmcBenchmark.java @@ -14,82 +14,57 @@ import java.util.concurrent.TimeUnit; @Fork(1) @Warmup(iterations = 1) @Measurement(iterations = 3) +@State(Scope.Group) public class MpmcBenchmark { - public static final String GROUP_UNSAFE = "MpmcArrayQueue"; - public static final String GROUP_AFU = "MpmcAtomicArrayQueue"; - public static final String GROUP_JDK = "ArrayBlockingQueue"; + public static final String PARAM_UNSAFE = "MpmcArrayQueue"; + public static final String PARAM_AFU = "MpmcAtomicArrayQueue"; + public static final String PARAM_JDK = "ArrayBlockingQueue"; public static final int PRODUCER_THREADS_NUMBER = 32; public static final int CONSUMER_THREADS_NUMBER = 32; + public static final String GROUP_NAME = "MyGroup"; + public static final int CAPACITY = 128; - @State(Scope.Group) - public static class Mpmc { - public final Queue queue = new MpmcArrayQueue<>(CAPACITY); + @Param({PARAM_UNSAFE, PARAM_AFU, PARAM_JDK}) + public volatile String implementation; + + public volatile Queue queue; + + @Setup(Level.Trial) + public void setUp() { + switch (implementation) { + case PARAM_UNSAFE: + queue = new MpmcArrayQueue<>(CAPACITY); + break; + case PARAM_AFU: + queue = new MpmcAtomicArrayQueue<>(CAPACITY); + break; + case PARAM_JDK: + queue = new ArrayBlockingQueue<>(CAPACITY); + break; + default: + throw new UnsupportedOperationException("Unsupported implementation " + implementation); + } } - @State(Scope.Group) - public static class MpmcAtomic { - public final Queue queue = new MpmcAtomicArrayQueue<>(CAPACITY); - } - - @State(Scope.Group) - public static class Jdk { - public final Queue queue = new ArrayBlockingQueue<>(CAPACITY); - } @Benchmark - @Group(GROUP_UNSAFE) + @Group(GROUP_NAME) @GroupThreads(PRODUCER_THREADS_NUMBER) - public void mpmcWrite(Control control, Mpmc state) { - write(control, state.queue); - } - - @Benchmark - @Group(GROUP_UNSAFE) - @GroupThreads(CONSUMER_THREADS_NUMBER) - public void mpmcRead(Control control, Mpmc state) { - read(control, state.queue); - } - - @Benchmark - @Group(GROUP_AFU) - @GroupThreads(PRODUCER_THREADS_NUMBER) - public void mpmcAtomicWrite(Control control, MpmcAtomic state) { - write(control, state.queue); - } - - @Benchmark - @Group(GROUP_AFU) - @GroupThreads(CONSUMER_THREADS_NUMBER) - public void mpmcAtomicRead(Control control, MpmcAtomic state) { - read(control, state.queue); - } - - @Benchmark - @Group(GROUP_JDK) - @GroupThreads(PRODUCER_THREADS_NUMBER) - public void jdkWrite(Control control, Jdk state) { - write(control, state.queue); - } - - @Benchmark - @Group(GROUP_JDK) - @GroupThreads(CONSUMER_THREADS_NUMBER) - public void jdkRead(Control control, Jdk state) { - read(control, state.queue); - } - - private void write(Control control, Queue queue) { + public void write(Control control) { //noinspection StatementWithEmptyBody while (!control.stopMeasurement && !queue.offer(1L)) { // Is intentionally left blank } } - private void read(Control control, Queue queue) { + @Benchmark + @Group(GROUP_NAME) + @GroupThreads(CONSUMER_THREADS_NUMBER) + public void read(Control control) { //noinspection StatementWithEmptyBody while (!control.stopMeasurement && queue.poll() == null) { // Is intentionally left blank