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, 6 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +3 -2 lines
Log Message:
Shrink timeouts

File Contents

# Content
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, as
4 * explained at http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 import java.util.concurrent.*;
8 import java.util.concurrent.atomic.*;
9 import java.util.concurrent.locks.*;
10
11 public class TimeoutExchangerLoops {
12 static final int NCPUS = Runtime.getRuntime().availableProcessors();
13
14 static final int DEFAULT_THREADS = NCPUS + 2;
15 static final long DEFAULT_PATIENCE_NANOS = 100000;
16 static final long DEFAULT_TRIAL_MILLIS = 10000;
17
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 int nReps = 3;
23
24 // Parse and check args
25 int argc = 0;
26 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 else
35 maxThreads = Integer.parseInt(option);
36 argc++;
37 }
38
39 // Display runtime parameters
40 System.out.print("TimeoutExchangerTest");
41 System.out.print(" -t " + trialMillis);
42 System.out.print(" -p " + patienceNanos);
43 System.out.print(" -r " + nReps);
44 System.out.print(" max threads " + maxThreads);
45 System.out.println();
46
47 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 }
74 }
75
76 static void oneRun(int nThreads, long trialMillis, long patienceNanos)
77 throws Exception {
78 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 Runner[] runners = new Runner[nThreads];
83 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 for (int i = 0; i < nThreads; ++i)
93 threads[i].interrupt();
94 long elapsed = System.nanoTime() - startTime;
95 for (int i = 0; i < nThreads; ++i)
96 threads[i].join();
97 int iters = 0;
98 long fails = 0;
99 for (int i = 0; i < nThreads; ++i) {
100 iters += runners[i].iters;
101 fails += runners[i].failures;
102 }
103 if (iters <= 0) iters = 1;
104 long rate = iters * 1000L * 1000L * 1000L / elapsed;
105 long npt = elapsed / iters;
106 double failRate = (fails * 100.0) / (double) iters;
107 System.out.printf("%9d it/s ", rate);
108 System.out.printf("%9d ns/it", npt);
109 System.out.printf("%9.5f%% fails ", failRate);
110 System.out.print(fails);
111 System.out.println();
112 // x.printStats();
113 }
114
115 static final class Runner implements Runnable {
116 final Exchanger exchanger;
117 final CountDownLatch start;
118 final long patience;
119 volatile int iters;
120 volatile int failures;
121 Runner(Exchanger x, long patience, CountDownLatch start) {
122 this.exchanger = x;
123 this.patience = patience;
124 this.start = start;
125 }
126
127 public void run() {
128 int i = 0;
129 try {
130 Exchanger x = exchanger;
131 Object m = new Integer(17);
132 long p = patience;
133 start.await();
134 for (;;) {
135 try {
136 Object e = x.exchange(m, p, TimeUnit.NANOSECONDS);
137 if (e == null || e == m)
138 throw new Error();
139 m = e;
140 ++i;
141 } catch (TimeoutException to) {
142 if (Thread.interrupted()) {
143 iters = i;
144 return;
145 }
146 ++i;
147 ++failures;
148 }
149 }
150 } catch (InterruptedException ie) {
151 iters = i;
152 }
153 }
154 }
155 }