|
14 | 14 | @Fork(1) |
15 | 15 | @Warmup(iterations = 1) |
16 | 16 | @Measurement(iterations = 3) |
| 17 | +@State(Scope.Group) |
17 | 18 | public class MpmcBenchmark { |
18 | 19 |
|
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"; |
22 | 23 |
|
23 | 24 | public static final int PRODUCER_THREADS_NUMBER = 32; |
24 | 25 | public static final int CONSUMER_THREADS_NUMBER = 32; |
25 | 26 |
|
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"; |
42 | 28 |
|
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; |
56 | 30 |
|
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 | + } |
62 | 51 | } |
63 | 52 |
|
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 | | - } |
70 | 53 |
|
71 | 54 | @Benchmark |
72 | | - @Group(GROUP_JDK) |
| 55 | + @Group(GROUP_NAME) |
73 | 56 | @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) { |
86 | 58 | //noinspection StatementWithEmptyBody |
87 | 59 | while (!control.stopMeasurement && !queue.offer(1L)) { |
88 | 60 | // Is intentionally left blank |
89 | 61 | } |
90 | 62 | } |
91 | 63 |
|
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) { |
93 | 68 | //noinspection StatementWithEmptyBody |
94 | 69 | while (!control.stopMeasurement && queue.poll() == null) { |
95 | 70 | // Is intentionally left blank |
|
0 commit comments