1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.*; |
34 |
|
static final int POOL_MASK = POOL_SIZE-1; |
35 |
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
36 |
|
static { |
37 |
< |
for (int i = 0; i < POOL_SIZE; ++i) |
37 |
> |
for (int i = 0; i < POOL_SIZE; ++i) |
38 |
|
intPool[i] = Integer.valueOf(i); |
39 |
|
} |
40 |
|
|
41 |
|
// Number of puts by producers or takes by consumers |
42 |
|
static final int ITERS = 1 << 20; |
43 |
|
|
44 |
< |
// max lag between a producer and consumer to avoid |
44 |
> |
// max lag between a producer and consumer to avoid |
45 |
|
// this becoming a GC test rather than queue test. |
46 |
|
// Used only per-pair to lessen impact on queue sync |
47 |
|
static final int LAG_MASK = (1 << 12) - 1; |
49 |
|
public static void main(String[] args) throws Exception { |
50 |
|
int maxN = NCPUS * 3 / 2; |
51 |
|
|
52 |
< |
if (args.length > 0) |
52 |
> |
if (args.length > 0) |
53 |
|
maxN = Integer.parseInt(args[0]); |
54 |
|
|
55 |
|
warmup(); |
56 |
|
print = true; |
57 |
< |
int k = 1; |
58 |
< |
for (int i = 1; i <= maxN;) { |
57 |
> |
for (int k = 1, i = 1; i <= maxN;) { |
58 |
|
System.out.println("Pairs:" + i); |
59 |
|
oneTest(i, ITERS); |
60 |
|
if (i == k) { |
61 |
|
k = i << 1; |
62 |
|
i = i + (i >>> 1); |
63 |
< |
} |
64 |
< |
else |
63 |
> |
} |
64 |
> |
else |
65 |
|
i = k; |
66 |
|
} |
67 |
|
pool.shutdown(); |
72 |
|
System.out.print("Warmup "); |
73 |
|
int it = 2000; |
74 |
|
for (int j = 5; j > 0; --j) { |
75 |
< |
oneTest(j, it); |
75 |
> |
oneTest(j, it); |
76 |
|
System.out.print("."); |
77 |
|
it += 1000; |
78 |
|
} |
79 |
|
System.gc(); |
80 |
|
it = 20000; |
81 |
|
for (int j = 5; j > 0; --j) { |
82 |
< |
oneTest(j, it); |
82 |
> |
oneTest(j, it); |
83 |
|
System.out.print("."); |
84 |
|
it += 10000; |
85 |
|
} |
102 |
|
|
103 |
|
Thread.sleep(100); // System.gc(); |
104 |
|
if (print) |
105 |
+ |
System.out.print("ConcurrentLinkedDeque "); |
106 |
+ |
oneRun(new ConcurrentLinkedDeque<Integer>(), n, iters); |
107 |
+ |
|
108 |
+ |
Thread.sleep(100); // System.gc(); |
109 |
+ |
if (print) |
110 |
|
System.out.print("LinkedBlockingQueue "); |
111 |
|
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
112 |
|
|
119 |
|
if (print) |
120 |
|
System.out.print("LinkedBlockingDeque "); |
121 |
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
122 |
< |
|
122 |
> |
|
123 |
|
Thread.sleep(100); // System.gc(); |
124 |
|
if (print) |
125 |
|
System.out.print("ArrayBlockingQueue "); |
126 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
127 |
|
|
124 |
– |
|
128 |
|
Thread.sleep(100); // System.gc(); |
129 |
|
if (print) |
130 |
|
System.out.print("PriorityBlockingQueue "); |
134 |
|
if (print) |
135 |
|
System.out.print("ArrayBlockingQueue(fair)"); |
136 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
134 |
– |
|
137 |
|
} |
138 |
< |
|
139 |
< |
static abstract class Stage implements Runnable { |
138 |
> |
|
139 |
> |
abstract static class Stage implements Runnable { |
140 |
|
final int iters; |
141 |
|
final Queue<Integer> queue; |
142 |
|
final CyclicBarrier barrier; |
143 |
|
final Phaser lagPhaser; |
144 |
< |
Stage (Queue<Integer> q, CyclicBarrier b, Phaser s, |
145 |
< |
int iters) { |
144 |
< |
queue = q; |
144 |
> |
Stage(Queue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
145 |
> |
queue = q; |
146 |
|
barrier = b; |
147 |
|
lagPhaser = s; |
148 |
|
this.iters = iters; |
172 |
|
break; |
173 |
|
if ((i & LAG_MASK) == LAG_MASK) |
174 |
|
lagPhaser.arriveAndAwaitAdvance(); |
175 |
< |
} |
175 |
> |
} |
176 |
|
} |
177 |
|
addProducerSum(ps); |
178 |
|
barrier.await(); |
179 |
|
} |
180 |
< |
catch (Exception ie) { |
181 |
< |
ie.printStackTrace(); |
182 |
< |
return; |
180 |
> |
catch (Exception ie) { |
181 |
> |
ie.printStackTrace(); |
182 |
> |
return; |
183 |
|
} |
184 |
|
} |
185 |
|
} |
186 |
|
|
187 |
|
static class Consumer extends Stage { |
188 |
|
Consumer(Queue<Integer> q, CyclicBarrier b, Phaser s, |
189 |
< |
int iters) { |
189 |
> |
int iters) { |
190 |
|
super(q, b, s, iters); |
191 |
|
} |
192 |
|
|
210 |
|
addConsumerSum(cs); |
211 |
|
barrier.await(); |
212 |
|
} |
213 |
< |
catch (Exception ie) { |
214 |
< |
ie.printStackTrace(); |
215 |
< |
return; |
213 |
> |
catch (Exception ie) { |
214 |
> |
ie.printStackTrace(); |
215 |
> |
return; |
216 |
|
} |
217 |
|
} |
218 |
|
|
234 |
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer"); |
235 |
|
} |
236 |
|
|
236 |
– |
|
237 |
|
} |
238 |
– |
|