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

Comparing jsr166/src/test/loops/TimeoutProducerConsumerLoops.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.10 by jsr166, Thu Apr 14 23:16:10 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines