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.11 by jsr166, Thu Dec 18 18:13:06 2014 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;
54 <        
50 <        int k = 1;
51 <        for (int i = 1; i <= maxPairs;) {
54 >        for (int k = 1, i = 1; i <= maxPairs;) {
55              System.out.println("Pairs:" + i);
56              oneTest(i, iters);
57              Thread.sleep(100);
58              if (i == k) {
59                  k = i << 1;
60                  i = i + (i >>> 1);
61 <            }
62 <            else
61 >            }
62 >            else
63                  i = k;
64          }
65          pool.shutdown();
66 <   }
66 >    }
67 >
68 >    static void oneTest(int n, int iters) throws Exception {
69 >        if (print)
70 >            System.out.print("LinkedTransferQueue      ");
71 >        oneRun(new LinkedTransferQueue<Integer>(), n, iters);
72  
65    static void oneTest(int pairs, int iters) throws Exception {
66        int fairIters = iters/20;
73          if (print)
74 <            System.out.print("ArrayBlockingQueue      ");
75 <        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
74 >            System.out.print("LinkedTransferQueue(xfer)");
75 >        oneRun(new LTQasSQ<Integer>(), n, iters);
76  
77          if (print)
78 <            System.out.print("LinkedBlockingQueue     ");
79 <        oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
78 >            System.out.print("LinkedBlockingQueue      ");
79 >        oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
80  
81          if (print)
82 <            System.out.print("LinkedBlockingDeque     ");
83 <        oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
82 >            System.out.print("LinkedBlockingQueue(cap) ");
83 >        oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
84  
85          if (print)
86 <            System.out.print("SynchronousQueue        ");
87 <        oneRun(new SynchronousQueue<Integer>(), pairs, iters);
86 >            System.out.print("ArrayBlockingQueue(cap)  ");
87 >        oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
88  
89          if (print)
90 <            System.out.print("SynchronousQueue(fair)  ");
91 <        oneRun(new SynchronousQueue<Integer>(true), pairs, fairIters);
90 >            System.out.print("LinkedBlockingDeque      ");
91 >        oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
92  
93          if (print)
94 <            System.out.print("PriorityBlockingQueue   ");
95 <        oneRun(new PriorityBlockingQueue<Integer>(), pairs, fairIters);
94 >            System.out.print("SynchronousQueue         ");
95 >        oneRun(new SynchronousQueue<Integer>(), n, iters);
96  
97          if (print)
98 <            System.out.print("ArrayBlockingQueue(fair)");
99 <        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, fairIters);
98 >            System.out.print("SynchronousQueue(fair)   ");
99 >        oneRun(new SynchronousQueue<Integer>(true), n, iters);
100 >
101 >        if (print)
102 >            System.out.print("PriorityBlockingQueue    ");
103 >        oneRun(new PriorityBlockingQueue<Integer>(), n, iters / 16);
104 >
105 >        if (print)
106 >            System.out.print("ArrayBlockingQueue(fair) ");
107 >        oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16);
108  
109      }
110 <    
111 <    static abstract class Stage implements Runnable {
110 >
111 >    abstract static class Stage implements Runnable {
112          final int iters;
113          final BlockingQueue<Integer> queue;
114          final CyclicBarrier barrier;
115 <        Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
116 <            queue = q;
115 >        final Phaser lagPhaser;
116 >        Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
117 >            queue = q;
118              barrier = b;
119 +            lagPhaser = s;
120              this.iters = iters;
121          }
122      }
123  
124      static class Producer extends Stage {
125 <        Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
126 <            super(q, b, iters);
125 >        Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
126 >            super(q, b, s, iters);
127          }
128  
129          public void run() {
# Line 116 | Line 132 | public class TimeoutProducerConsumerLoop
132                  int s = 0;
133                  int l = hashCode();
134                  int i = 0;
135 <                long timeout = 1;
135 >                long timeout = 1000;
136                  while (i < iters) {
137                      l = LoopHelpers.compute4(l);
138 <                    if (queue.offer(new Integer(l),
139 <                                    timeout, TimeUnit.NANOSECONDS)) {
140 <                        s += LoopHelpers.compute4(l);
138 >                    Integer v = intPool[l & POOL_MASK];
139 >                    if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) {
140 >                        s += LoopHelpers.compute4(v.intValue());
141                          ++i;
142                          if (timeout > 1)
143                              timeout--;
144 +                        if ((i & LAG_MASK) == LAG_MASK)
145 +                            lagPhaser.arriveAndAwaitAdvance();
146                      }
147                      else
148                          timeout++;
# Line 132 | Line 150 | public class TimeoutProducerConsumerLoop
150                  addProducerSum(s);
151                  barrier.await();
152              }
153 <            catch (Exception ie) {
154 <                ie.printStackTrace();
155 <                return;
153 >            catch (Exception ie) {
154 >                ie.printStackTrace();
155 >                return;
156              }
157          }
158      }
159  
160      static class Consumer extends Stage {
161 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
162 <            super(q, b, iters);
161 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
162 >            super(q, b, s, iters);
163          }
164  
165          public void run() {
# Line 150 | Line 168 | public class TimeoutProducerConsumerLoop
168                  int l = 0;
169                  int s = 0;
170                  int i = 0;
171 <                long timeout = 1;
171 >                long timeout = 1000;
172                  while (i < iters) {
173 <                    Integer e = queue.poll(timeout,
173 >                    Integer e = queue.poll(timeout,
174                                             TimeUnit.NANOSECONDS);
175                      if (e != null) {
176                          l = LoopHelpers.compute4(e.intValue());
# Line 160 | Line 178 | public class TimeoutProducerConsumerLoop
178                          ++i;
179                          if (timeout > 1)
180                              --timeout;
181 +                        if ((i & LAG_MASK) == LAG_MASK)
182 +                            lagPhaser.arriveAndAwaitAdvance();
183                      }
184                      else
185                          ++timeout;
# Line 167 | Line 187 | public class TimeoutProducerConsumerLoop
187                  addConsumerSum(s);
188                  barrier.await();
189              }
190 <            catch (Exception ie) {
191 <                ie.printStackTrace();
192 <                return;
190 >            catch (Exception ie) {
191 >                ie.printStackTrace();
192 >                return;
193              }
194          }
195  
# Line 179 | Line 199 | public class TimeoutProducerConsumerLoop
199          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
200          CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer);
201          for (int i = 0; i < npairs; ++i) {
202 <            pool.execute(new Producer(q, barrier, iters));
203 <            pool.execute(new Consumer(q, barrier, iters));
202 >            Phaser s = new Phaser(2);
203 >            pool.execute(new Producer(q, barrier, s, iters));
204 >            pool.execute(new Consumer(q, barrier, s, iters));
205          }
206          barrier.await();
207          barrier.await();
208          long time = timer.getTime();
209          checkSum();
210 +        q.clear();
211          if (print)
212              System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
213      }
214  
215 +    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
216 +        LTQasSQ() { super(); }
217 +        public void put(T x) {
218 +            try { super.transfer(x);
219 +            } catch (InterruptedException ex) { throw new Error(); }
220 +        }
221 +
222 +        public boolean offer(T x, long timeout, TimeUnit unit) {
223 +            return super.offer(x, timeout, unit);
224 +        }
225 +
226 +    }
227 +
228   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines