ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SingleProducerMultipleConsumerLoops.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 check ordering for blocking queues with 1 producer and multiple consumers
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 SingleProducerMultipleConsumerLoops {
14     static final int CAPACITY = 100;
15    
16     static final ExecutorService pool = Executors.newCachedThreadPool();
17     static boolean print = false;
18    
19     public static void main(String[] args) throws Exception {
20     int maxConsumers = 100;
21     int iters = 10000;
22    
23     if (args.length > 0)
24     maxConsumers = Integer.parseInt(args[0]);
25    
26     print = false;
27     System.out.println("Warmup...");
28     oneTest(1, 10000);
29     Thread.sleep(100);
30     oneTest(2, 10000);
31     Thread.sleep(100);
32     print = true;
33    
34     for (int i = 1; i <= maxConsumers; i += (i+1) >>> 1) {
35     System.out.println("Consumers:" + i);
36     oneTest(i, iters);
37     Thread.sleep(100);
38     }
39     pool.shutdown();
40     }
41    
42     static void oneTest(int consumers, int iters) throws Exception {
43     if (print)
44     System.out.print("ArrayBlockingQueue ");
45     oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), consumers, iters);
46    
47     if (print)
48     System.out.print("LinkedBlockingQueue ");
49     oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), consumers, iters);
50    
51     if (print)
52     System.out.print("PriorityBlockingQueue ");
53     oneRun(new PriorityBlockingQueue<Integer>(), consumers, iters/10);
54    
55     if (print)
56     System.out.print("SynchronousQueue ");
57     oneRun(new SynchronousQueue<Integer>(), consumers, iters);
58    
59     if (print)
60     System.out.print("ArrayBlockingQueue(fair)");
61     oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), consumers, iters/10);
62     }
63    
64     static abstract class Stage implements Runnable {
65     final int iters;
66     final BlockingQueue<Integer> queue;
67     final CyclicBarrier barrier;
68     volatile int result;
69     Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
70     queue = q;
71     barrier = b;
72     this.iters = iters;
73     }
74     }
75    
76     static class Producer extends Stage {
77     Producer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
78     super(q, b, iters);
79     }
80    
81     public void run() {
82     try {
83     barrier.await();
84     for (int i = 0; i < iters; ++i) {
85     queue.put(new Integer(i));
86     }
87     barrier.await();
88     result = 432;
89     }
90     catch (Exception ie) {
91     ie.printStackTrace();
92     return;
93     }
94     }
95     }
96    
97     static class Consumer extends Stage {
98     Consumer(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
99     super(q, b, iters);
100     }
101    
102     public void run() {
103     try {
104     barrier.await();
105     int l = 0;
106     int s = 0;
107     int last = -1;
108     for (int i = 0; i < iters; ++i) {
109     Integer item = queue.take();
110     int v = item.intValue();
111     if (v < last)
112     throw new Error("Out-of-Order transfer");
113     last = v;
114     l = LoopHelpers.compute1(v);
115     s += l;
116     }
117     barrier.await();
118     result = s;
119     }
120     catch (Exception ie) {
121     ie.printStackTrace();
122     return;
123     }
124     }
125    
126     }
127    
128     static void oneRun(BlockingQueue<Integer> q, int nconsumers, int iters) throws Exception {
129     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
130     CyclicBarrier barrier = new CyclicBarrier(nconsumers + 2, timer);
131     pool.execute(new Producer(q, barrier, iters * nconsumers));
132     for (int i = 0; i < nconsumers; ++i) {
133     pool.execute(new Consumer(q, barrier, iters));
134     }
135     barrier.await();
136     barrier.await();
137     long time = timer.getTime();
138     if (print)
139     System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * nconsumers)) + " ns per transfer");
140     }
141    
142     }