1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Haim Yadid with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package jsr166.bench.concq; |
8 |
|
|
|
9 |
|
|
import org.openjdk.jmh.annotations.Benchmark; |
10 |
|
|
import org.openjdk.jmh.annotations.BenchmarkMode; |
11 |
|
|
import org.openjdk.jmh.annotations.Fork; |
12 |
|
|
import org.openjdk.jmh.annotations.Measurement; |
13 |
|
|
import org.openjdk.jmh.annotations.Mode; |
14 |
|
|
import org.openjdk.jmh.annotations.Scope; |
15 |
|
|
import org.openjdk.jmh.annotations.Setup; |
16 |
|
|
import org.openjdk.jmh.annotations.State; |
17 |
|
|
import org.openjdk.jmh.annotations.Threads; |
18 |
|
|
import org.openjdk.jmh.annotations.Warmup; |
19 |
|
|
|
20 |
|
|
import java.util.Queue; |
21 |
|
|
import java.util.concurrent.ArrayBlockingQueue; |
22 |
|
|
import java.util.concurrent.ConcurrentLinkedDeque; |
23 |
|
|
|
24 |
|
|
import java.util.concurrent.ExecutionException; |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* Demonstrates performance failure fixed by |
28 |
|
|
* https://bugs.openjdk.java.net/browse/JDK-8054446 |
29 |
|
|
*/ |
30 |
|
|
@Fork(1) |
31 |
|
|
@Threads(1) |
32 |
|
|
@BenchmarkMode({Mode.Throughput}) |
33 |
|
|
@Warmup(iterations = 5) |
34 |
|
|
@Measurement(iterations = 5) |
35 |
|
|
public class ConcurrentQueueOfferRemove { |
36 |
|
|
static final String a = "a"; |
37 |
|
|
static final String b = "b"; |
38 |
|
|
public static final int NUM_PRE = 1; |
39 |
|
|
|
40 |
|
|
@State(Scope.Benchmark) |
41 |
|
|
public static class ArrayBlockingQueueState { |
42 |
|
|
Queue<String> queue = new ArrayBlockingQueue<String>(1000); |
43 |
|
|
@Setup |
44 |
|
|
public void setup() throws ExecutionException, InterruptedException { |
45 |
|
|
for (int i = 0; i < NUM_PRE; i++) queue.offer(a); |
46 |
|
|
} |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
@Benchmark |
50 |
|
|
public Object measureABQ(final ArrayBlockingQueueState arrayBlockingQueueState) throws Exception { |
51 |
|
|
Queue queue = arrayBlockingQueueState.queue; |
52 |
|
|
queue.offer(b); |
53 |
|
|
return queue.remove(b); |
54 |
|
|
|
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
@State(Scope.Benchmark) |
58 |
|
|
public static class ConcurrentLinkedDequeState { |
59 |
|
|
Queue<String> queue = new ConcurrentLinkedDeque<>(); |
60 |
|
|
@Setup |
61 |
|
|
public void setup() throws ExecutionException, InterruptedException { |
62 |
|
|
for (int i = 0; i < NUM_PRE; i++) queue.offer(a); |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
@Benchmark |
67 |
|
|
public Object measureCLD(final ConcurrentLinkedDequeState concurrentLinkedDequeState) throws Exception { |
68 |
|
|
Queue queue = concurrentLinkedDequeState.queue; |
69 |
|
|
queue.offer(b); |
70 |
|
|
return queue.remove(b); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
// @State(Scope.Benchmark) |
74 |
|
|
// public static class ConcurrentLinkedQueueState { |
75 |
|
|
// // A concurrentLinked Queue with the remove function replaced |
76 |
|
|
// // with jdk9 version |
77 |
|
|
|
78 |
|
|
// Queue<String> queue = new ConcurrentLinkedQueueFixed<>(); |
79 |
|
|
|
80 |
|
|
// @Setup |
81 |
|
|
// public void setup() throws ExecutionException, InterruptedException { |
82 |
|
|
// for (int i = 0; i < NUM_PRE; i++) queue.offer(a); |
83 |
|
|
// } |
84 |
|
|
// } |
85 |
|
|
|
86 |
|
|
@Benchmark |
87 |
|
|
public Object measureCLQFixed(final ConcurrentLinkedQueueState concurrentLinkedQueueState) throws Exception { |
88 |
|
|
Queue queue = concurrentLinkedQueueState.queue; |
89 |
|
|
queue.offer(b); |
90 |
|
|
return queue.remove(b); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
@State(Scope.Benchmark) |
94 |
|
|
public static class ConcurrentLinkedQueueOrigState { |
95 |
|
|
Queue<String> queue = new java.util.concurrent.ConcurrentLinkedQueue<>(); |
96 |
|
|
@Setup |
97 |
|
|
public void setup() throws ExecutionException, InterruptedException { |
98 |
|
|
for (int i = 0; i < NUM_PRE; i++) queue.offer(a); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
@Benchmark |
103 |
|
|
public Object measureCLQOrig(final ConcurrentLinkedQueueOrigState state) throws Exception { |
104 |
|
|
Queue queue = state.queue; |
105 |
|
|
queue.offer(b); |
106 |
|
|
return queue.remove(b); |
107 |
|
|
} |
108 |
|
|
} |