Skip to content

Commit b296e9a

Browse files
committed
BAEL-1423 Java Concurrency Utility with JCTools Library
Refactored JMH benchmark after digging deeper into its features
1 parent f4feab9 commit b296e9a

1 file changed

Lines changed: 32 additions & 57 deletions

File tree

‎jctools/src/main/java/com/baeldung/jctools/MpmcBenchmark.java‎

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,57 @@
1414
@Fork(1)
1515
@Warmup(iterations = 1)
1616
@Measurement(iterations = 3)
17+
@State(Scope.Group)
1718
public class MpmcBenchmark {
1819

19-
public static final String GROUP_UNSAFE = "MpmcArrayQueue";
20-
public static final String GROUP_AFU = "MpmcAtomicArrayQueue";
21-
public static final String GROUP_JDK = "ArrayBlockingQueue";
20+
public static final String PARAM_UNSAFE = "MpmcArrayQueue";
21+
public static final String PARAM_AFU = "MpmcAtomicArrayQueue";
22+
public static final String PARAM_JDK = "ArrayBlockingQueue";
2223

2324
public static final int PRODUCER_THREADS_NUMBER = 32;
2425
public static final int CONSUMER_THREADS_NUMBER = 32;
2526

26-
public static final int CAPACITY = 128;
27-
28-
@State(Scope.Group)
29-
public static class Mpmc {
30-
public final Queue<Long> queue = new MpmcArrayQueue<>(CAPACITY);
31-
}
32-
33-
@State(Scope.Group)
34-
public static class MpmcAtomic {
35-
public final Queue<Long> queue = new MpmcAtomicArrayQueue<>(CAPACITY);
36-
}
37-
38-
@State(Scope.Group)
39-
public static class Jdk {
40-
public final Queue<Long> queue = new ArrayBlockingQueue<>(CAPACITY);
41-
}
27+
public static final String GROUP_NAME = "MyGroup";
4228

43-
@Benchmark
44-
@Group(GROUP_UNSAFE)
45-
@GroupThreads(PRODUCER_THREADS_NUMBER)
46-
public void mpmcWrite(Control control, Mpmc state) {
47-
write(control, state.queue);
48-
}
49-
50-
@Benchmark
51-
@Group(GROUP_UNSAFE)
52-
@GroupThreads(CONSUMER_THREADS_NUMBER)
53-
public void mpmcRead(Control control, Mpmc state) {
54-
read(control, state.queue);
55-
}
29+
public static final int CAPACITY = 128;
5630

57-
@Benchmark
58-
@Group(GROUP_AFU)
59-
@GroupThreads(PRODUCER_THREADS_NUMBER)
60-
public void mpmcAtomicWrite(Control control, MpmcAtomic state) {
61-
write(control, state.queue);
31+
@Param({PARAM_UNSAFE, PARAM_AFU, PARAM_JDK})
32+
public volatile String implementation;
33+
34+
public volatile Queue<Long> queue;
35+
36+
@Setup(Level.Trial)
37+
public void setUp() {
38+
switch (implementation) {
39+
case PARAM_UNSAFE:
40+
queue = new MpmcArrayQueue<>(CAPACITY);
41+
break;
42+
case PARAM_AFU:
43+
queue = new MpmcAtomicArrayQueue<>(CAPACITY);
44+
break;
45+
case PARAM_JDK:
46+
queue = new ArrayBlockingQueue<>(CAPACITY);
47+
break;
48+
default:
49+
throw new UnsupportedOperationException("Unsupported implementation " + implementation);
50+
}
6251
}
6352

64-
@Benchmark
65-
@Group(GROUP_AFU)
66-
@GroupThreads(CONSUMER_THREADS_NUMBER)
67-
public void mpmcAtomicRead(Control control, MpmcAtomic state) {
68-
read(control, state.queue);
69-
}
7053

7154
@Benchmark
72-
@Group(GROUP_JDK)
55+
@Group(GROUP_NAME)
7356
@GroupThreads(PRODUCER_THREADS_NUMBER)
74-
public void jdkWrite(Control control, Jdk state) {
75-
write(control, state.queue);
76-
}
77-
78-
@Benchmark
79-
@Group(GROUP_JDK)
80-
@GroupThreads(CONSUMER_THREADS_NUMBER)
81-
public void jdkRead(Control control, Jdk state) {
82-
read(control, state.queue);
83-
}
84-
85-
private void write(Control control, Queue<Long> queue) {
57+
public void write(Control control) {
8658
//noinspection StatementWithEmptyBody
8759
while (!control.stopMeasurement && !queue.offer(1L)) {
8860
// Is intentionally left blank
8961
}
9062
}
9163

92-
private void read(Control control, Queue<Long> queue) {
64+
@Benchmark
65+
@Group(GROUP_NAME)
66+
@GroupThreads(CONSUMER_THREADS_NUMBER)
67+
public void read(Control control) {
9368
//noinspection StatementWithEmptyBody
9469
while (!control.stopMeasurement && queue.poll() == null) {
9570
// Is intentionally left blank

0 commit comments

Comments
 (0)