ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutExchangerLoops.java
Revision: 1.1
Committed: Sun Aug 7 19:25:55 2005 UTC (18 years, 9 months ago) by dl
Branch: MAIN
Log Message:
Add exchanger performance tests; update others

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Bill Scherer and Doug Lea with assistance from members
3     * of JCP JSR-166 Expert Group and released to the public domain. Use,
4     * modify, and redistribute this code in any way without
5     * acknowledgement.
6     */
7    
8     import java.util.concurrent.*;
9     import java.util.concurrent.atomic.*;
10     import java.util.concurrent.locks.*;
11    
12     public class TimeoutExchangerLoops {
13     static final int DEFAULT_THREADS = 32;
14     static final long DEFAULT_TRIAL_MILLIS = 5000;
15     static final long DEFAULT_PATIENCE_NANOS = 500000;
16    
17     static final ExecutorService pool = Executors.newCachedThreadPool();
18    
19     public static void main(String[] args) throws Exception {
20     int maxThreads = DEFAULT_THREADS;
21     long trialMillis = DEFAULT_TRIAL_MILLIS;
22     long patienceNanos = DEFAULT_PATIENCE_NANOS;
23    
24     // Parse and check args
25     int argc = 0;
26     try {
27     while (argc < args.length) {
28     String option = args[argc++];
29     if (option.equals("-t"))
30     trialMillis = Integer.parseInt(args[argc]);
31     else if (option.equals("-p"))
32     patienceNanos = Long.parseLong(args[argc]);
33     else
34     maxThreads = Integer.parseInt(option);
35     argc++;
36     }
37     }
38     catch (Exception e) {
39     e.printStackTrace();
40     System.exit(0);
41     }
42    
43     // Display runtime parameters
44     System.out.print("TimeoutExchangerTest");
45     System.out.print(" -t " + trialMillis);
46     System.out.print(" -p " + patienceNanos);
47     System.out.print(" max threads " + maxThreads);
48     System.out.println();
49    
50     // warmup
51     System.out.print("Threads: " + 2 + "\t");
52     oneRun(2, trialMillis, patienceNanos);
53     Thread.sleep(100);
54    
55     int k = 4;
56     for (int i = 2; i <= maxThreads;) {
57     System.out.print("Threads: " + i + "\t");
58     oneRun(i, trialMillis, patienceNanos);
59     Thread.sleep(100);
60     if (i == k) {
61     k = i << 1;
62     i = i + (i >>> 1);
63     }
64     else
65     i = k;
66     }
67     pool.shutdown();
68     }
69    
70     static void oneRun(int nThreads, long trialMillis, long patienceNanos)
71     throws Exception {
72     CyclicBarrier barrier = new CyclicBarrier(nThreads+1);
73     long stopTime = System.currentTimeMillis() + trialMillis;
74     Exchanger<MutableInt> x = new Exchanger<MutableInt>();
75     Runner[] runners = new Runner[nThreads];
76     for (int i = 0; i < nThreads; ++i)
77     runners[i] = new Runner(x, stopTime, patienceNanos, barrier);
78     for (int i = 0; i < nThreads; ++i)
79     pool.execute(runners[i]);
80     barrier.await();
81     barrier.await();
82     long iters = 0;
83     long fails = 0;
84     long check = 0;
85     for (int i = 0; i < nThreads; ++i) {
86     iters += runners[i].iterations;
87     fails += runners[i].failures;
88     check += runners[i].mine.value;
89     }
90     if (check != iters)
91     throw new Error("bad checksum " + iters + "/" + check);
92     long rate = (iters * 1000) / trialMillis;
93     double failRate = (fails * 100.0) / (double)iters;
94     System.out.print(LoopHelpers.rightJustify(rate) + " iterations/s ");
95     System.out.printf("%9.5f", failRate);
96     System.out.print("% timeouts");
97     System.out.println();
98     }
99    
100     static final class MutableInt {
101     int value;
102     }
103    
104     static final class Runner implements Runnable {
105     final Exchanger<MutableInt> x;
106     volatile long iterations;
107     volatile long failures;
108     volatile MutableInt mine;
109     final long stopTime;
110     final long patience;
111     final CyclicBarrier barrier;
112     Runner(Exchanger<MutableInt> x, long stopTime,
113     long patience, CyclicBarrier b) {
114     this.x = x;
115     this.stopTime = stopTime;
116     this.patience = patience;
117     this.barrier = b;
118     mine = new MutableInt();
119     }
120    
121     public void run() {
122     try {
123     barrier.await();
124     MutableInt m = mine;
125     int i = 0;
126     int fails = 0;
127     do {
128     try {
129     ++i;
130     m.value++;
131     m = x.exchange(m, patience, TimeUnit.NANOSECONDS);
132     } catch (TimeoutException to) {
133     if (System.currentTimeMillis() >= stopTime)
134     break;
135     else
136     ++fails;
137     }
138     } while ((i & 127) != 0 || // only check time periodically
139     System.currentTimeMillis() < stopTime);
140    
141     mine = m;
142     iterations = i;
143     failures = fails;
144     barrier.await();
145     } catch(Exception e) {
146     e.printStackTrace();
147     return;
148     }
149     }
150     }
151     }
152