ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutProducerConsumerLoops.java
Revision: 1.13
Committed: Mon Aug 10 03:13:33 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.12: +0 -1 lines
Log Message:
delete unwanted blank lines

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.2 * Expert Group and released to the public domain, as explained at
4 jsr166 1.9 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.concurrent.*;
8 dl 1.5
9 dl 1.1 public class TimeoutProducerConsumerLoops {
10 dl 1.3 static final int NCPUS = Runtime.getRuntime().availableProcessors();
11 dl 1.1 static final ExecutorService pool = Executors.newCachedThreadPool();
12 dl 1.3
13     // Number of elements passed around -- must be power of two
14     // Elements are reused from pool to minimize alloc impact
15     static final int POOL_SIZE = 1 << 8;
16     static final int POOL_MASK = POOL_SIZE-1;
17     static final Integer[] intPool = new Integer[POOL_SIZE];
18     static {
19 jsr166 1.6 for (int i = 0; i < POOL_SIZE; ++i)
20 dl 1.3 intPool[i] = Integer.valueOf(i);
21     }
22    
23 jsr166 1.6 // max lag between a producer and consumer to avoid
24 dl 1.5 // this becoming a GC test rather than queue test.
25     // Used only per-pair to lessen impact on queue sync
26     static final int LAG_MASK = (1 << 12) - 1;
27 dl 1.3
28 dl 1.1 static boolean print = false;
29     static int producerSum;
30     static int consumerSum;
31     static synchronized void addProducerSum(int x) {
32     producerSum += x;
33     }
34    
35     static synchronized void addConsumerSum(int x) {
36     consumerSum += x;
37     }
38    
39     static synchronized void checkSum() {
40     if (producerSum != consumerSum) {
41     throw new Error("CheckSum mismatch");
42     }
43     }
44    
45     public static void main(String[] args) throws Exception {
46 dl 1.3 int maxPairs = NCPUS * 3 / 2;
47     int iters = 1000000;
48 dl 1.1
49 jsr166 1.6 if (args.length > 0)
50 dl 1.1 maxPairs = Integer.parseInt(args[0]);
51    
52     print = true;
53 jsr166 1.11 for (int k = 1, i = 1; i <= maxPairs;) {
54 dl 1.1 System.out.println("Pairs:" + i);
55     oneTest(i, iters);
56     Thread.sleep(100);
57     if (i == k) {
58     k = i << 1;
59     i = i + (i >>> 1);
60 jsr166 1.6 }
61     else
62 dl 1.1 i = k;
63     }
64     pool.shutdown();
65 jsr166 1.10 }
66 dl 1.1
67 dl 1.3 static void oneTest(int n, int iters) throws Exception {
68     if (print)
69     System.out.print("LinkedTransferQueue ");
70     oneRun(new LinkedTransferQueue<Integer>(), n, iters);
71    
72     if (print)
73     System.out.print("LinkedTransferQueue(xfer)");
74     oneRun(new LTQasSQ<Integer>(), n, iters);
75    
76 dl 1.1 if (print)
77 dl 1.3 System.out.print("LinkedBlockingQueue ");
78     oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
79 dl 1.1
80     if (print)
81 dl 1.3 System.out.print("LinkedBlockingQueue(cap) ");
82     oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
83 dl 1.1
84     if (print)
85 dl 1.3 System.out.print("ArrayBlockingQueue(cap) ");
86     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
87 dl 1.1
88     if (print)
89 dl 1.3 System.out.print("LinkedBlockingDeque ");
90     oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
91 dl 1.1
92     if (print)
93 dl 1.3 System.out.print("SynchronousQueue ");
94     oneRun(new SynchronousQueue<Integer>(), n, iters);
95 dl 1.1
96     if (print)
97 dl 1.3 System.out.print("SynchronousQueue(fair) ");
98     oneRun(new SynchronousQueue<Integer>(true), n, iters);
99 dl 1.1
100     if (print)
101 dl 1.3 System.out.print("PriorityBlockingQueue ");
102     oneRun(new PriorityBlockingQueue<Integer>(), n, iters / 16);
103    
104     if (print)
105     System.out.print("ArrayBlockingQueue(fair) ");
106     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16);
107 dl 1.1 }
108 jsr166 1.6
109 jsr166 1.8 abstract static class Stage implements Runnable {
110 dl 1.1 final int iters;
111     final BlockingQueue<Integer> queue;
112     final CyclicBarrier barrier;
113 dl 1.5 final Phaser lagPhaser;
114 jsr166 1.7 Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
115 jsr166 1.6 queue = q;
116 dl 1.1 barrier = b;
117 dl 1.5 lagPhaser = s;
118 dl 1.1 this.iters = iters;
119     }
120     }
121    
122     static class Producer extends Stage {
123 dl 1.5 Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
124     super(q, b, s, iters);
125 dl 1.1 }
126    
127     public void run() {
128     try {
129     barrier.await();
130     int s = 0;
131     int l = hashCode();
132     int i = 0;
133 dl 1.3 long timeout = 1000;
134 dl 1.1 while (i < iters) {
135     l = LoopHelpers.compute4(l);
136 dl 1.3 Integer v = intPool[l & POOL_MASK];
137     if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) {
138     s += LoopHelpers.compute4(v.intValue());
139 dl 1.1 ++i;
140     if (timeout > 1)
141     timeout--;
142 dl 1.5 if ((i & LAG_MASK) == LAG_MASK)
143     lagPhaser.arriveAndAwaitAdvance();
144 dl 1.1 }
145     else
146     timeout++;
147     }
148     addProducerSum(s);
149     barrier.await();
150     }
151 jsr166 1.6 catch (Exception ie) {
152     ie.printStackTrace();
153     return;
154 dl 1.1 }
155     }
156     }
157    
158     static class Consumer extends Stage {
159 jsr166 1.6 Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
160 dl 1.5 super(q, b, s, iters);
161 dl 1.1 }
162    
163     public void run() {
164     try {
165     barrier.await();
166     int l = 0;
167     int s = 0;
168     int i = 0;
169 dl 1.3 long timeout = 1000;
170 dl 1.1 while (i < iters) {
171 jsr166 1.6 Integer e = queue.poll(timeout,
172 dl 1.1 TimeUnit.NANOSECONDS);
173     if (e != null) {
174     l = LoopHelpers.compute4(e.intValue());
175     s += l;
176     ++i;
177     if (timeout > 1)
178     --timeout;
179 dl 1.5 if ((i & LAG_MASK) == LAG_MASK)
180     lagPhaser.arriveAndAwaitAdvance();
181 dl 1.1 }
182     else
183     ++timeout;
184     }
185     addConsumerSum(s);
186     barrier.await();
187     }
188 jsr166 1.6 catch (Exception ie) {
189     ie.printStackTrace();
190     return;
191 dl 1.1 }
192     }
193    
194     }
195    
196     static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
197     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
198     CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
199     for (int i = 0; i < npairs; ++i) {
200 dl 1.5 Phaser s = new Phaser(2);
201     pool.execute(new Producer(q, barrier, s, iters));
202     pool.execute(new Consumer(q, barrier, s, iters));
203 dl 1.1 }
204     barrier.await();
205     barrier.await();
206     long time = timer.getTime();
207     checkSum();
208 dl 1.3 q.clear();
209 dl 1.1 if (print)
210     System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
211     }
212    
213 dl 1.3 static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
214     LTQasSQ() { super(); }
215     public void put(T x) {
216 jsr166 1.6 try { super.transfer(x);
217 dl 1.3 } catch (InterruptedException ex) { throw new Error(); }
218     }
219    
220     public boolean offer(T x, long timeout, TimeUnit unit) {
221 jsr166 1.6 return super.offer(x, timeout, unit);
222 dl 1.3 }
223    
224     }
225    
226 dl 1.1 }