ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutProducerConsumerLoops.java
Revision: 1.11
Committed: Thu Dec 18 18:13:06 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +1 -2 lines
Log Message:
move k = 1 into loop initializer

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
10     public class TimeoutProducerConsumerLoops {
11 dl 1.3 static final int NCPUS = Runtime.getRuntime().availableProcessors();
12 dl 1.1 static final ExecutorService pool = Executors.newCachedThreadPool();
13 dl 1.3
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 jsr166 1.6 for (int i = 0; i < POOL_SIZE; ++i)
21 dl 1.3 intPool[i] = Integer.valueOf(i);
22     }
23    
24 jsr166 1.6 // max lag between a producer and consumer to avoid
25 dl 1.5 // 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 dl 1.3
29 dl 1.1 static boolean print = false;
30     static int producerSum;
31     static int consumerSum;
32     static synchronized void addProducerSum(int x) {
33     producerSum += x;
34     }
35    
36     static synchronized void addConsumerSum(int x) {
37     consumerSum += x;
38     }
39    
40     static synchronized void checkSum() {
41     if (producerSum != consumerSum) {
42     throw new Error("CheckSum mismatch");
43     }
44     }
45    
46     public static void main(String[] args) throws Exception {
47 dl 1.3 int maxPairs = NCPUS * 3 / 2;
48     int iters = 1000000;
49 dl 1.1
50 jsr166 1.6 if (args.length > 0)
51 dl 1.1 maxPairs = Integer.parseInt(args[0]);
52    
53     print = true;
54 jsr166 1.11 for (int k = 1, i = 1; i <= maxPairs;) {
55 dl 1.1 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 jsr166 1.6 }
62     else
63 dl 1.1 i = k;
64     }
65     pool.shutdown();
66 jsr166 1.10 }
67 dl 1.1
68 dl 1.3 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    
73     if (print)
74     System.out.print("LinkedTransferQueue(xfer)");
75     oneRun(new LTQasSQ<Integer>(), n, iters);
76    
77 dl 1.1 if (print)
78 dl 1.3 System.out.print("LinkedBlockingQueue ");
79     oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
80 dl 1.1
81     if (print)
82 dl 1.3 System.out.print("LinkedBlockingQueue(cap) ");
83     oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
84 dl 1.1
85     if (print)
86 dl 1.3 System.out.print("ArrayBlockingQueue(cap) ");
87     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
88 dl 1.1
89     if (print)
90 dl 1.3 System.out.print("LinkedBlockingDeque ");
91     oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
92 dl 1.1
93     if (print)
94 dl 1.3 System.out.print("SynchronousQueue ");
95     oneRun(new SynchronousQueue<Integer>(), n, iters);
96 dl 1.1
97     if (print)
98 dl 1.3 System.out.print("SynchronousQueue(fair) ");
99     oneRun(new SynchronousQueue<Integer>(true), n, iters);
100 dl 1.1
101     if (print)
102 dl 1.3 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 dl 1.1
109     }
110 jsr166 1.6
111 jsr166 1.8 abstract static class Stage implements Runnable {
112 dl 1.1 final int iters;
113     final BlockingQueue<Integer> queue;
114     final CyclicBarrier barrier;
115 dl 1.5 final Phaser lagPhaser;
116 jsr166 1.7 Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
117 jsr166 1.6 queue = q;
118 dl 1.1 barrier = b;
119 dl 1.5 lagPhaser = s;
120 dl 1.1 this.iters = iters;
121     }
122     }
123    
124     static class Producer extends Stage {
125 dl 1.5 Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
126     super(q, b, s, iters);
127 dl 1.1 }
128    
129     public void run() {
130     try {
131     barrier.await();
132     int s = 0;
133     int l = hashCode();
134     int i = 0;
135 dl 1.3 long timeout = 1000;
136 dl 1.1 while (i < iters) {
137     l = LoopHelpers.compute4(l);
138 dl 1.3 Integer v = intPool[l & POOL_MASK];
139     if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) {
140     s += LoopHelpers.compute4(v.intValue());
141 dl 1.1 ++i;
142     if (timeout > 1)
143     timeout--;
144 dl 1.5 if ((i & LAG_MASK) == LAG_MASK)
145     lagPhaser.arriveAndAwaitAdvance();
146 dl 1.1 }
147     else
148     timeout++;
149     }
150     addProducerSum(s);
151     barrier.await();
152     }
153 jsr166 1.6 catch (Exception ie) {
154     ie.printStackTrace();
155     return;
156 dl 1.1 }
157     }
158     }
159    
160     static class Consumer extends Stage {
161 jsr166 1.6 Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
162 dl 1.5 super(q, b, s, iters);
163 dl 1.1 }
164    
165     public void run() {
166     try {
167     barrier.await();
168     int l = 0;
169     int s = 0;
170     int i = 0;
171 dl 1.3 long timeout = 1000;
172 dl 1.1 while (i < iters) {
173 jsr166 1.6 Integer e = queue.poll(timeout,
174 dl 1.1 TimeUnit.NANOSECONDS);
175     if (e != null) {
176     l = LoopHelpers.compute4(e.intValue());
177     s += l;
178     ++i;
179     if (timeout > 1)
180     --timeout;
181 dl 1.5 if ((i & LAG_MASK) == LAG_MASK)
182     lagPhaser.arriveAndAwaitAdvance();
183 dl 1.1 }
184     else
185     ++timeout;
186     }
187     addConsumerSum(s);
188     barrier.await();
189     }
190 jsr166 1.6 catch (Exception ie) {
191     ie.printStackTrace();
192     return;
193 dl 1.1 }
194     }
195    
196     }
197    
198     static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception {
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 dl 1.5 Phaser s = new Phaser(2);
203     pool.execute(new Producer(q, barrier, s, iters));
204     pool.execute(new Consumer(q, barrier, s, iters));
205 dl 1.1 }
206     barrier.await();
207     barrier.await();
208     long time = timer.getTime();
209     checkSum();
210 dl 1.3 q.clear();
211 dl 1.1 if (print)
212     System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer");
213     }
214    
215 dl 1.3 static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
216     LTQasSQ() { super(); }
217     public void put(T x) {
218 jsr166 1.6 try { super.transfer(x);
219 dl 1.3 } catch (InterruptedException ex) { throw new Error(); }
220     }
221    
222     public boolean offer(T x, long timeout, TimeUnit unit) {
223 jsr166 1.6 return super.offer(x, timeout, unit);
224 dl 1.3 }
225    
226     }
227    
228 dl 1.1 }