ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutExchangerLoops.java
Revision: 1.9
Committed: Sat Sep 12 20:32:48 2015 UTC (8 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +3 -2 lines
Log Message:
Shrink timeouts

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Bill Scherer and Doug Lea with assistance from members
3 dl 1.3 * of JCP JSR-166 Expert Group and released to the public domain, as
4 jsr166 1.7 * explained at http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.concurrent.*;
8     import java.util.concurrent.atomic.*;
9     import java.util.concurrent.locks.*;
10    
11     public class TimeoutExchangerLoops {
12 dl 1.2 static final int NCPUS = Runtime.getRuntime().availableProcessors();
13    
14     static final int DEFAULT_THREADS = NCPUS + 2;
15 dl 1.9 static final long DEFAULT_PATIENCE_NANOS = 100000;
16 dl 1.2 static final long DEFAULT_TRIAL_MILLIS = 10000;
17 dl 1.1
18     public static void main(String[] args) throws Exception {
19     int maxThreads = DEFAULT_THREADS;
20     long trialMillis = DEFAULT_TRIAL_MILLIS;
21     long patienceNanos = DEFAULT_PATIENCE_NANOS;
22 dl 1.2 int nReps = 3;
23 dl 1.1
24     // Parse and check args
25     int argc = 0;
26 dl 1.2 while (argc < args.length) {
27     String option = args[argc++];
28     if (option.equals("-t"))
29     trialMillis = Integer.parseInt(args[argc]);
30     else if (option.equals("-p"))
31     patienceNanos = Long.parseLong(args[argc]);
32     else if (option.equals("-r"))
33     nReps = Integer.parseInt(args[argc]);
34 jsr166 1.4 else
35 dl 1.2 maxThreads = Integer.parseInt(option);
36     argc++;
37 dl 1.1 }
38    
39 jsr166 1.6 // Display runtime parameters
40     System.out.print("TimeoutExchangerTest");
41     System.out.print(" -t " + trialMillis);
42     System.out.print(" -p " + patienceNanos);
43 dl 1.2 System.out.print(" -r " + nReps);
44 jsr166 1.6 System.out.print(" max threads " + maxThreads);
45     System.out.println();
46 dl 1.1
47 dl 1.2 System.out.println("Warmups..");
48     long warmupTime = 1000;
49     long sleepTime = 500;
50     if (false) {
51     for (int k = 0; k < 10; ++k) {
52     for (int j = 0; j < 10; ++j) {
53     oneRun(2, (j + 1) * 1000, patienceNanos);
54     Thread.sleep(sleepTime);
55     }
56     }
57     }
58    
59     oneRun(3, warmupTime, patienceNanos);
60     Thread.sleep(sleepTime);
61    
62     for (int i = maxThreads; i >= 2; i -= 1) {
63     oneRun(i, warmupTime, patienceNanos);
64     Thread.sleep(sleepTime);
65     }
66    
67     for (int j = 0; j < nReps; ++j) {
68     System.out.println("Replication " + j);
69     for (int i = 2; i <= maxThreads; i += 2) {
70     oneRun(i, trialMillis, patienceNanos);
71     Thread.sleep(sleepTime);
72     }
73 dl 1.1 }
74     }
75    
76 jsr166 1.4 static void oneRun(int nThreads, long trialMillis, long patienceNanos)
77 dl 1.1 throws Exception {
78 dl 1.2 System.out.printf("%4d threads", nThreads);
79     System.out.printf("%9dms", trialMillis);
80     final CountDownLatch start = new CountDownLatch(1);
81     Exchanger x = new Exchanger();
82 dl 1.1 Runner[] runners = new Runner[nThreads];
83 dl 1.2 Thread[] threads = new Thread[nThreads];
84     for (int i = 0; i < nThreads; ++i) {
85     runners[i] = new Runner(x, patienceNanos, start);
86     threads[i] = new Thread(runners[i]);
87     threads[i].start();
88     }
89     long startTime = System.nanoTime();
90     start.countDown();
91     Thread.sleep(trialMillis);
92 jsr166 1.4 for (int i = 0; i < nThreads; ++i)
93 dl 1.2 threads[i].interrupt();
94     long elapsed = System.nanoTime() - startTime;
95 jsr166 1.4 for (int i = 0; i < nThreads; ++i)
96 dl 1.2 threads[i].join();
97     int iters = 0;
98 dl 1.1 long fails = 0;
99     for (int i = 0; i < nThreads; ++i) {
100 dl 1.2 iters += runners[i].iters;
101 dl 1.1 fails += runners[i].failures;
102     }
103 dl 1.2 if (iters <= 0) iters = 1;
104     long rate = iters * 1000L * 1000L * 1000L / elapsed;
105     long npt = elapsed / iters;
106 jsr166 1.5 double failRate = (fails * 100.0) / (double) iters;
107 dl 1.2 System.out.printf("%9d it/s ", rate);
108     System.out.printf("%9d ns/it", npt);
109 dl 1.9 System.out.printf("%9.5f%% fails ", failRate);
110     System.out.print(fails);
111 dl 1.1 System.out.println();
112 dl 1.2 // x.printStats();
113 dl 1.1 }
114    
115     static final class Runner implements Runnable {
116 dl 1.2 final Exchanger exchanger;
117     final CountDownLatch start;
118 dl 1.1 final long patience;
119 dl 1.2 volatile int iters;
120     volatile int failures;
121     Runner(Exchanger x, long patience, CountDownLatch start) {
122     this.exchanger = x;
123 dl 1.1 this.patience = patience;
124 dl 1.2 this.start = start;
125 dl 1.1 }
126    
127     public void run() {
128 dl 1.2 int i = 0;
129 dl 1.1 try {
130 dl 1.2 Exchanger x = exchanger;
131     Object m = new Integer(17);
132     long p = patience;
133     start.await();
134     for (;;) {
135 dl 1.1 try {
136 dl 1.2 Object e = x.exchange(m, p, TimeUnit.NANOSECONDS);
137     if (e == null || e == m)
138     throw new Error();
139     m = e;
140 dl 1.1 ++i;
141     } catch (TimeoutException to) {
142 dl 1.2 if (Thread.interrupted()) {
143     iters = i;
144     return;
145     }
146     ++i;
147     ++failures;
148 dl 1.1 }
149 dl 1.2 }
150     } catch (InterruptedException ie) {
151     iters = i;
152 dl 1.1 }
153     }
154     }
155     }