ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MultipleProducersSingleConsumerLoops.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# User Rev Content
1 dl 1.1 /*
2     * @test
3     * @synopsis multiple producers and single consumer using blocking queues
4     */
5     /*
6     * Written by Doug Lea with assistance from members of JCP JSR-166
7     * Expert Group and released to the public domain. Use, modify, and
8     * redistribute this code in any way without acknowledgement.
9     */
10    
11     import java.util.concurrent.*;
12    
13     public class MultipleProducersSingleConsumerLoops {
14     static final int CAPACITY = 100;
15     static final ExecutorService pool = Executors.newCachedThreadPool();
16     static boolean print = false;
17     static int producerSum;
18     static int consumerSum;
19    
20     static synchronized void addProducerSum(int x) {
21     producerSum += x;
22     }
23    
24     static synchronized void addConsumerSum(int x) {
25     consumerSum += x;
26     }
27    
28     static synchronized void checkSum() {
29     if (producerSum != consumerSum)
30     throw new Error("CheckSum mismatch");
31     }
32    
33     public static void main(String[] args) throws Exception {
34     int maxProducers = 100;
35     int iters = 100000;
36    
37     if (args.length > 0)
38     maxProducers = Integer.parseInt(args[0]);
39    
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     print = true;
47    
48     for (int i = 1; i <= maxProducers; i += (i+1) >>> 1) {
49     System.out.println("Producers:" + i);
50     oneTest(i, iters);
51     Thread.sleep(100);
52     }
53     pool.shutdown();
54     }
55    
56     static void oneTest(int producers, int iters) throws Exception {
57     if (print)
58     System.out.print("ArrayBlockingQueue ");
59     oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), producers, iters);
60    
61     if (print)
62     System.out.print("LinkedBlockingQueue ");
63     oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), producers, iters);
64    
65     // Don't run PBQ since can legitimately run out of memory
66     // if (print)
67     // System.out.print("PriorityBlockingQueue ");
68     // oneRun(new PriorityBlockingQueue<Integer>(), producers, iters);
69    
70     if (print)
71     System.out.print("SynchronousQueue ");
72     oneRun(new SynchronousQueue<Integer>(), producers, iters);
73    
74     if (print)
75     System.out.print("ArrayBlockingQueue(fair)");
76     oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), producers, iters/10);
77     }
78    
79     static abstract class Stage implements Runnable {
80     final int iters;
81     final BlockingQueue<Integer> queue;
82     final CyclicBarrier barrier;
83     Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
84     queue = q;
85     barrier = b;
86     this.iters = iters;
87     }
88     }
89    
90     static class Producer extends Stage {
91     Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
92     super(q, b, iters);
93     }
94    
95     public void run() {
96     try {
97     barrier.await();
98     int s = 0;
99     int l = hashCode();
100     for (int i = 0; i < iters; ++i) {
101     l = LoopHelpers.compute1(l);
102     l = LoopHelpers.compute2(l);
103     queue.put(new Integer(l));
104     s += l;
105     }
106     addProducerSum(s);
107     barrier.await();
108     }
109     catch (Exception ie) {
110     ie.printStackTrace();
111     return;
112     }
113     }
114     }
115    
116     static class Consumer extends Stage {
117     Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
118     super(q, b, iters);
119     }
120    
121     public void run() {
122     try {
123     barrier.await();
124     int s = 0;
125     for (int i = 0; i < iters; ++i) {
126     s += queue.take().intValue();
127     }
128     addConsumerSum(s);
129     barrier.await();
130     }
131     catch (Exception ie) {
132     ie.printStackTrace();
133     return;
134     }
135     }
136    
137     }
138    
139     static void oneRun(BlockingQueue<Integer> q, int nproducers, int iters) throws Exception {
140     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
141     CyclicBarrier barrier = new CyclicBarrier(nproducers + 2, timer);
142     for (int i = 0; i < nproducers; ++i) {
143     pool.execute(new Producer(q, barrier, iters));
144     }
145     pool.execute(new Consumer(q, barrier, iters * nproducers));
146     barrier.await();
147     barrier.await();
148     long time = timer.getTime();
149     checkSum();
150     if (print)
151     System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * nproducers)) + " ns per transfer");
152     }
153    
154     }