ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ProducerConsumerLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/ProducerConsumerLoops.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.9 by jsr166, Thu Jan 15 18:34:19 2015 UTC

# Line 1 | Line 1
1   /*
2 * @test
3 * @synopsis  multiple producers and consumers using blocking queues
4 */
5 /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 + import java.util.*;
8   import java.util.concurrent.*;
9 + //import jsr166y.*;
10  
11   public class ProducerConsumerLoops {
12 <    static final int CAPACITY =      100;
13 <
12 >    static final int NCPUS = Runtime.getRuntime().availableProcessors();
13 >    static final Random rng = new Random();
14      static final ExecutorService pool = Executors.newCachedThreadPool();
15      static boolean print = false;
16      static int producerSum;
# Line 30 | Line 28 | public class ProducerConsumerLoops {
28              throw new Error("CheckSum mismatch");
29      }
30  
31 +    // Number of elements passed around -- must be power of two
32 +    // Elements are reused from pool to minimize alloc impact
33 +    static final int POOL_SIZE = 1 << 7;
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)
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
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;
48 +
49      public static void main(String[] args) throws Exception {
50 <        int maxPairs = 100;
35 <        int iters = 100000;
50 >        int maxPairs = NCPUS * 3 / 2;
51  
52 <        if (args.length > 0)
52 >        if (args.length > 0)
53              maxPairs = Integer.parseInt(args[0]);
54  
55 <        print = false;
41 <        System.out.println("Warmup...");
42 <        oneTest(1, 10000);
43 <        Thread.sleep(100);
44 <        oneTest(2, 10000);
45 <        Thread.sleep(100);
46 <        oneTest(2, 10000);
47 <        Thread.sleep(100);
55 >        warmup();
56          print = true;
57 <        
50 <        int k = 1;
51 <        for (int i = 1; i <= maxPairs;) {
57 >        for (int k = 1, i = 1; i <= maxPairs;) {
58              System.out.println("Pairs:" + i);
59 <            oneTest(i, iters);
54 <            Thread.sleep(100);
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();
68 <   }
68 >    }
69 >
70 >    static void warmup() throws Exception {
71 >        print = false;
72 >        System.out.print("Warmup ");
73 >        int it = 2000;
74 >        for (int j = 5; j > 0; --j) {
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);
83 >            System.out.print(".");
84 >            it += 10000;
85 >        }
86 >        System.gc();
87 >        System.out.println();
88 >    }
89  
90 <    static void oneTest(int pairs, int iters) throws Exception {
91 <        int fairIters = iters/20;
90 >    static void oneTest(int n, int iters) throws Exception {
91 >        int fairIters = iters/16;
92 >
93 >        Thread.sleep(100); // System.gc();
94          if (print)
95 <            System.out.print("ArrayBlockingQueue      ");
96 <        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
95 >            System.out.print("LinkedTransferQueue     ");
96 >        oneRun(new LinkedTransferQueue<Integer>(), n, iters);
97  
98 +        Thread.sleep(100); // System.gc();
99          if (print)
100              System.out.print("LinkedBlockingQueue     ");
101 <        oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
101 >        oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
102  
103 +        Thread.sleep(100); // System.gc();
104 +        if (print)
105 +            System.out.print("LinkedBlockingQueue(cap)");
106 +        oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
107 +
108 +        Thread.sleep(100); // System.gc();
109          if (print)
110              System.out.print("LinkedBlockingDeque     ");
111 <        oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
111 >        oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
112 >
113 >        Thread.sleep(100); // System.gc();
114 >        if (print)
115 >            System.out.print("ArrayBlockingQueue      ");
116 >        oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
117  
118 +        Thread.sleep(100); // System.gc();
119          if (print)
120              System.out.print("SynchronousQueue        ");
121 <        oneRun(new SynchronousQueue<Integer>(), pairs, iters);
121 >        oneRun(new SynchronousQueue<Integer>(), n, iters);
122  
123 +        Thread.sleep(100); // System.gc();
124          if (print)
125              System.out.print("SynchronousQueue(fair)  ");
126 <        oneRun(new SynchronousQueue<Integer>(true), pairs, fairIters);
126 >        oneRun(new SynchronousQueue<Integer>(true), n, iters);
127 >
128 >        Thread.sleep(100); // System.gc();
129 >        if (print)
130 >            System.out.print("LinkedTransferQueue(xfer)");
131 >        oneRun(new LTQasSQ<Integer>(), n, iters);
132 >
133 >        Thread.sleep(100); // System.gc();
134 >        if (print)
135 >            System.out.print("LinkedTransferQueue(half)");
136 >        oneRun(new HalfSyncLTQ<Integer>(), n, iters);
137  
138 +        Thread.sleep(100); // System.gc();
139          if (print)
140              System.out.print("PriorityBlockingQueue   ");
141 <        oneRun(new PriorityBlockingQueue<Integer>(), pairs, fairIters);
141 >        oneRun(new PriorityBlockingQueue<Integer>(), n, fairIters);
142  
143 +        Thread.sleep(100); // System.gc();
144          if (print)
145              System.out.print("ArrayBlockingQueue(fair)");
146 <        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, fairIters);
146 >        oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
147  
148      }
149 <    
150 <    static abstract class Stage implements Runnable {
149 >
150 >    abstract static class Stage implements Runnable {
151          final int iters;
152          final BlockingQueue<Integer> queue;
153          final CyclicBarrier barrier;
154 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
155 <            queue = q;
154 >        final Phaser lagPhaser;
155 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
156 >            queue = q;
157              barrier = b;
158 +            lagPhaser = s;
159              this.iters = iters;
160          }
161      }
162  
163      static class Producer extends Stage {
164 <        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
165 <            super(q, b, iters);
164 >        Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
165 >                 int iters) {
166 >            super(q, b, s, iters);
167          }
168  
169          public void run() {
170              try {
171                  barrier.await();
172 <                int s = 0;
173 <                int l = hashCode();
172 >                int ps = 0;
173 >                int r = hashCode();
174                  for (int i = 0; i < iters; ++i) {
175 <                    l = LoopHelpers.compute4(l);
176 <                    queue.put(new Integer(l));
177 <                    s += LoopHelpers.compute4(l);
175 >                    r = LoopHelpers.compute7(r);
176 >                    Integer v = intPool[r & POOL_MASK];
177 >                    int k = v.intValue();
178 >                    queue.put(v);
179 >                    ps += k;
180 >                    if ((i & LAG_MASK) == LAG_MASK)
181 >                        lagPhaser.arriveAndAwaitAdvance();
182                  }
183 <                addProducerSum(s);
183 >                addProducerSum(ps);
184                  barrier.await();
185              }
186 <            catch (Exception ie) {
187 <                ie.printStackTrace();
188 <                return;
186 >            catch (Exception ie) {
187 >                ie.printStackTrace();
188 >                return;
189              }
190          }
191      }
192  
193      static class Consumer extends Stage {
194 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
195 <            super(q, b, iters);
194 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
195 >                 int iters) {
196 >            super(q, b, s, iters);
197          }
198  
199          public void run() {
200              try {
201                  barrier.await();
202 <                int l = 0;
142 <                int s = 0;
202 >                int cs = 0;
203                  for (int i = 0; i < iters; ++i) {
204 <                    l = LoopHelpers.compute4(queue.take().intValue());
205 <                    s += l;
204 >                    Integer v = queue.take();
205 >                    int k = v.intValue();
206 >                    cs += k;
207 >                    if ((i & LAG_MASK) == LAG_MASK)
208 >                        lagPhaser.arriveAndAwaitAdvance();
209                  }
210 <                addConsumerSum(s);
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  
219      }
220  
221 <    static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
221 >    static void oneRun(BlockingQueue<Integer> q, int n, int iters) throws Exception {
222          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
223 <        CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
224 <        for (int i = 0; i < npairs; ++i) {
225 <            pool.execute(new Producer(q, barrier, iters));
226 <            pool.execute(new Consumer(q, barrier, iters));
223 >        CyclicBarrier barrier = new CyclicBarrier(n * 2 + 1, timer);
224 >        for (int i = 0; i < n; ++i) {
225 >            Phaser s = new Phaser(2);
226 >            pool.execute(new Producer(q, barrier, s, iters));
227 >            pool.execute(new Consumer(q, barrier, s, iters));
228          }
229          barrier.await();
230          barrier.await();
231          long time = timer.getTime();
232          checkSum();
233          if (print)
234 <            System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
234 >            System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer");
235 >    }
236 >
237 >    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
238 >        LTQasSQ() { super(); }
239 >        public void put(T x) {
240 >            try { super.transfer(x);
241 >            } catch (InterruptedException ex) { throw new Error(); }
242 >        }
243      }
244  
245 +    static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
246 +        int calls;
247 +        HalfSyncLTQ() { super(); }
248 +        public void put(T x) {
249 +            if ((++calls & 1) == 0)
250 +                super.put(x);
251 +            else {
252 +                try { super.transfer(x);
253 +                } catch (InterruptedException ex) {
254 +                    throw new Error();
255 +                }
256 +            }
257 +        }
258 +    }
259   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines