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 |
|
static final int LAG = (1 << 12); |
47 |
|
static final int LAG_MASK = LAG - 1; |
49 |
|
public static void main(String[] args) throws Exception { |
50 |
|
int maxn = 12; // NCPUS * 3 / 2; |
51 |
|
|
52 |
< |
if (args.length > 0) |
52 |
> |
if (args.length > 0) |
53 |
|
maxn = Integer.parseInt(args[0]); |
54 |
|
|
55 |
|
warmup(); |
61 |
|
if (i == k) { |
62 |
|
k = i << 1; |
63 |
|
i = i + (i >>> 1); |
64 |
< |
} |
65 |
< |
else |
64 |
> |
} |
65 |
> |
else |
66 |
|
i = k; |
67 |
|
} |
68 |
|
pool.shutdown(); |
73 |
|
System.out.print("Warmup "); |
74 |
|
int it = 2000; |
75 |
|
for (int j = 5; j > 0; --j) { |
76 |
< |
oneTest(j, it); |
76 |
> |
oneTest(j, it); |
77 |
|
System.out.print("."); |
78 |
|
it += 1000; |
79 |
|
} |
80 |
|
System.gc(); |
81 |
|
it = 20000; |
82 |
|
for (int j = 5; j > 0; --j) { |
83 |
< |
oneTest(j, it); |
83 |
> |
oneTest(j, it); |
84 |
|
System.out.print("."); |
85 |
|
it += 10000; |
86 |
|
} |
110 |
|
if (print) |
111 |
|
System.out.print("LinkedBlockingDeque "); |
112 |
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
113 |
< |
|
113 |
> |
|
114 |
|
Thread.sleep(100); // System.gc(); |
115 |
|
if (print) |
116 |
|
System.out.print("ArrayBlockingQueue "); |
121 |
|
System.out.print("SynchronousQueue "); |
122 |
|
oneRun(new SynchronousQueue<Integer>(), n, iters); |
123 |
|
|
124 |
< |
|
124 |
> |
|
125 |
|
Thread.sleep(100); // System.gc(); |
126 |
|
if (print) |
127 |
|
System.out.print("SynchronousQueue(fair) "); |
136 |
|
if (print) |
137 |
|
System.out.print("LinkedTransferQueue(half)"); |
138 |
|
oneRun(new HalfSyncLTQ<Integer>(), n, iters); |
139 |
< |
|
139 |
> |
|
140 |
|
Thread.sleep(100); // System.gc(); |
141 |
|
if (print) |
142 |
|
System.out.print("PriorityBlockingQueue "); |
149 |
|
|
150 |
|
|
151 |
|
} |
152 |
< |
|
152 |
> |
|
153 |
|
static abstract class Stage implements Runnable { |
154 |
|
final int iters; |
155 |
|
final BlockingQueue<Integer> queue; |
158 |
|
final int lag; |
159 |
|
Stage (BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
160 |
|
int iters, int lag) { |
161 |
< |
queue = q; |
161 |
> |
queue = q; |
162 |
|
barrier = b; |
163 |
|
lagPhaser = s; |
164 |
|
this.iters = iters; |
192 |
|
addProducerSum(ps); |
193 |
|
barrier.await(); |
194 |
|
} |
195 |
< |
catch (Exception ie) { |
196 |
< |
ie.printStackTrace(); |
197 |
< |
return; |
195 |
> |
catch (Exception ie) { |
196 |
> |
ie.printStackTrace(); |
197 |
> |
return; |
198 |
|
} |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
static class Consumer extends Stage { |
203 |
|
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
204 |
< |
int iters, int lag) { |
204 |
> |
int iters, int lag) { |
205 |
|
super(q, b, s, iters, lag); |
206 |
|
} |
207 |
|
|
222 |
|
addConsumerSum(cs); |
223 |
|
barrier.await(); |
224 |
|
} |
225 |
< |
catch (Exception ie) { |
226 |
< |
ie.printStackTrace(); |
227 |
< |
return; |
225 |
> |
catch (Exception ie) { |
226 |
> |
ie.printStackTrace(); |
227 |
> |
return; |
228 |
|
} |
229 |
|
} |
230 |
|
|
247 |
|
if (print) |
248 |
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * (n + 1))) + " ns per transfer"); |
249 |
|
} |
250 |
< |
|
250 |
> |
|
251 |
|
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
252 |
|
LTQasSQ() { super(); } |
253 |
|
public void put(T x) { |
254 |
< |
try { super.transfer(x); |
254 |
> |
try { super.transfer(x); |
255 |
|
} catch (InterruptedException ex) { throw new Error(); } |
256 |
|
} |
257 |
|
} |
263 |
|
if ((++calls & 1) == 0) |
264 |
|
super.put(x); |
265 |
|
else { |
266 |
< |
try { super.transfer(x); |
267 |
< |
} catch (InterruptedException ex) { |
268 |
< |
throw new Error(); |
266 |
> |
try { super.transfer(x); |
267 |
> |
} catch (InterruptedException ex) { |
268 |
> |
throw new Error(); |
269 |
|
} |
270 |
|
} |
271 |
|
} |
272 |
|
} |
273 |
|
} |
274 |
– |
|