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.4 by jsr166, Thu Oct 29 23:09:08 2009 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/licenses/publicdomain
5   */
6  
7   import java.util.concurrent.*;
8 + //import jsr166y.*;
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 +
25      static boolean print = false;
26      static int producerSum;
27      static int consumerSum;
# Line 31 | Line 40 | public class TimeoutProducerConsumerLoop
40      }
41  
42      public static void main(String[] args) throws Exception {
43 <        int maxPairs = 100;
44 <        int iters = 100000;
43 >        int maxPairs = NCPUS * 3 / 2;
44 >        int iters = 1000000;
45  
46 <        if (args.length > 0)
46 >        if (args.length > 0)
47              maxPairs = Integer.parseInt(args[0]);
48  
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);
49          print = true;
49        
50          int k = 1;
51          for (int i = 1; i <= maxPairs;) {
52              System.out.println("Pairs:" + i);
# Line 55 | Line 55 | public class TimeoutProducerConsumerLoop
55              if (i == k) {
56                  k = i << 1;
57                  i = i + (i >>> 1);
58 <            }
59 <            else
58 >            }
59 >            else
60                  i = k;
61          }
62          pool.shutdown();
63     }
64  
65 <    static void oneTest(int pairs, int iters) throws Exception {
66 <        int fairIters = iters/20;
65 >    static void oneTest(int n, int iters) throws Exception {
66 >        if (print)
67 >            System.out.print("LinkedTransferQueue      ");
68 >        oneRun(new LinkedTransferQueue<Integer>(), n, iters);
69 >
70 >        if (print)
71 >            System.out.print("LinkedTransferQueue(xfer)");
72 >        oneRun(new LTQasSQ<Integer>(), n, iters);
73 >
74 >        if (print)
75 >            System.out.print("LinkedBlockingQueue      ");
76 >        oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
77 >
78          if (print)
79 <            System.out.print("ArrayBlockingQueue      ");
80 <        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
79 >            System.out.print("LinkedBlockingQueue(cap) ");
80 >        oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
81  
82          if (print)
83 <            System.out.print("LinkedBlockingQueue     ");
84 <        oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
83 >            System.out.print("ArrayBlockingQueue(cap)  ");
84 >        oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
85  
86          if (print)
87 <            System.out.print("LinkedBlockingDeque     ");
88 <        oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
87 >            System.out.print("LinkedBlockingDeque      ");
88 >        oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
89  
90          if (print)
91 <            System.out.print("SynchronousQueue        ");
92 <        oneRun(new SynchronousQueue<Integer>(), pairs, iters);
91 >            System.out.print("SynchronousQueue         ");
92 >        oneRun(new SynchronousQueue<Integer>(), n, iters);
93  
94          if (print)
95 <            System.out.print("SynchronousQueue(fair)  ");
96 <        oneRun(new SynchronousQueue<Integer>(true), pairs, fairIters);
95 >            System.out.print("SynchronousQueue(fair)   ");
96 >        oneRun(new SynchronousQueue<Integer>(true), n, iters);
97  
98          if (print)
99 <            System.out.print("PriorityBlockingQueue   ");
100 <        oneRun(new PriorityBlockingQueue<Integer>(), pairs, fairIters);
99 >            System.out.print("PriorityBlockingQueue    ");
100 >        oneRun(new PriorityBlockingQueue<Integer>(), n, iters / 16);
101  
102          if (print)
103 <            System.out.print("ArrayBlockingQueue(fair)");
104 <        oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, fairIters);
103 >            System.out.print("ArrayBlockingQueue(fair) ");
104 >        oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16);
105  
106      }
107 <    
107 >
108      static abstract class Stage implements Runnable {
109          final int iters;
110          final BlockingQueue<Integer> queue;
111          final CyclicBarrier barrier;
112          Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
113 <            queue = q;
113 >            queue = q;
114              barrier = b;
115              this.iters = iters;
116          }
# Line 116 | Line 127 | public class TimeoutProducerConsumerLoop
127                  int s = 0;
128                  int l = hashCode();
129                  int i = 0;
130 <                long timeout = 1;
130 >                long timeout = 1000;
131                  while (i < iters) {
132                      l = LoopHelpers.compute4(l);
133 <                    if (queue.offer(new Integer(l),
134 <                                    timeout, TimeUnit.NANOSECONDS)) {
135 <                        s += LoopHelpers.compute4(l);
133 >                    Integer v = intPool[l & POOL_MASK];
134 >                    if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) {
135 >                        s += LoopHelpers.compute4(v.intValue());
136                          ++i;
137                          if (timeout > 1)
138                              timeout--;
# Line 132 | Line 143 | public class TimeoutProducerConsumerLoop
143                  addProducerSum(s);
144                  barrier.await();
145              }
146 <            catch (Exception ie) {
147 <                ie.printStackTrace();
148 <                return;
146 >            catch (Exception ie) {
147 >                ie.printStackTrace();
148 >                return;
149              }
150          }
151      }
152  
153      static class Consumer extends Stage {
154 <        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
154 >        Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
155              super(q, b, iters);
156          }
157  
# Line 150 | Line 161 | public class TimeoutProducerConsumerLoop
161                  int l = 0;
162                  int s = 0;
163                  int i = 0;
164 <                long timeout = 1;
164 >                long timeout = 1000;
165                  while (i < iters) {
166 <                    Integer e = queue.poll(timeout,
166 >                    Integer e = queue.poll(timeout,
167                                             TimeUnit.NANOSECONDS);
168                      if (e != null) {
169                          l = LoopHelpers.compute4(e.intValue());
# Line 167 | Line 178 | public class TimeoutProducerConsumerLoop
178                  addConsumerSum(s);
179                  barrier.await();
180              }
181 <            catch (Exception ie) {
182 <                ie.printStackTrace();
183 <                return;
181 >            catch (Exception ie) {
182 >                ie.printStackTrace();
183 >                return;
184              }
185          }
186  
# Line 186 | Line 197 | public class TimeoutProducerConsumerLoop
197          barrier.await();
198          long time = timer.getTime();
199          checkSum();
200 +        q.clear();
201          if (print)
202              System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
203      }
204  
205 +    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
206 +        LTQasSQ() { super(); }
207 +        public void put(T x) {
208 +            try { super.transfer(x);
209 +            } catch (InterruptedException ex) { throw new Error(); }
210 +        }
211 +
212 +        public boolean offer(T x, long timeout, TimeUnit unit) {
213 +            return super.offer(x, timeout, unit);
214 +        }
215 +
216 +    }
217 +
218   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines