ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutExchangerLoops.java
Revision: 1.2
Committed: Mon Feb 13 12:39:23 2006 UTC (18 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.1: +95 -90 lines
Log Message:
Update exchanger tests

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. 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 NCPUS = Runtime.getRuntime().availableProcessors();
14
15 static final int DEFAULT_THREADS = NCPUS + 2;
16 static final long DEFAULT_PATIENCE_NANOS = 500000;
17 static final long DEFAULT_TRIAL_MILLIS = 10000;
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 int nReps = 3;
24
25 // Parse and check args
26 int argc = 0;
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 if (option.equals("-r"))
34 nReps = Integer.parseInt(args[argc]);
35 else
36 maxThreads = Integer.parseInt(option);
37 argc++;
38 }
39
40 // Display runtime parameters
41 System.out.print("TimeoutExchangerTest");
42 System.out.print(" -t " + trialMillis);
43 System.out.print(" -p " + patienceNanos);
44 System.out.print(" -r " + nReps);
45 System.out.print(" max threads " + maxThreads);
46 System.out.println();
47
48 System.out.println("Warmups..");
49 long warmupTime = 1000;
50 long sleepTime = 500;
51 if (false) {
52 for (int k = 0; k < 10; ++k) {
53 for (int j = 0; j < 10; ++j) {
54 oneRun(2, (j + 1) * 1000, patienceNanos);
55 Thread.sleep(sleepTime);
56 }
57 }
58 }
59
60 oneRun(3, warmupTime, patienceNanos);
61 Thread.sleep(sleepTime);
62
63 for (int i = maxThreads; i >= 2; i -= 1) {
64 oneRun(i, warmupTime, patienceNanos);
65 Thread.sleep(sleepTime);
66 }
67
68 for (int j = 0; j < nReps; ++j) {
69 System.out.println("Replication " + j);
70 for (int i = 2; i <= maxThreads; i += 2) {
71 oneRun(i, trialMillis, patienceNanos);
72 Thread.sleep(sleepTime);
73 }
74 }
75 }
76
77 static void oneRun(int nThreads, long trialMillis, long patienceNanos)
78 throws Exception {
79 System.out.printf("%4d threads", nThreads);
80 System.out.printf("%9dms", trialMillis);
81 final CountDownLatch start = new CountDownLatch(1);
82 Exchanger x = new Exchanger();
83 Runner[] runners = new Runner[nThreads];
84 Thread[] threads = new Thread[nThreads];
85 for (int i = 0; i < nThreads; ++i) {
86 runners[i] = new Runner(x, patienceNanos, start);
87 threads[i] = new Thread(runners[i]);
88 threads[i].start();
89 }
90 long startTime = System.nanoTime();
91 start.countDown();
92 Thread.sleep(trialMillis);
93 for (int i = 0; i < nThreads; ++i)
94 threads[i].interrupt();
95 long elapsed = System.nanoTime() - startTime;
96 for (int i = 0; i < nThreads; ++i)
97 threads[i].join();
98 int iters = 0;
99 long fails = 0;
100 for (int i = 0; i < nThreads; ++i) {
101 iters += runners[i].iters;
102 fails += runners[i].failures;
103 }
104 if (iters <= 0) iters = 1;
105 long rate = iters * 1000L * 1000L * 1000L / elapsed;
106 long npt = elapsed / iters;
107 double failRate = (fails * 100.0) / (double)iters;
108 System.out.printf("%9d it/s ", rate);
109 System.out.printf("%9d ns/it", npt);
110 System.out.printf("%9.5f%% fails", failRate);
111 System.out.println();
112 // x.printStats();
113 }
114
115
116 static final class Runner implements Runnable {
117 final Exchanger exchanger;
118 final CountDownLatch start;
119 final long patience;
120 volatile int iters;
121 volatile int failures;
122 Runner(Exchanger x, long patience, CountDownLatch start) {
123 this.exchanger = x;
124 this.patience = patience;
125 this.start = start;
126 }
127
128 public void run() {
129 int i = 0;
130 try {
131 Exchanger x = exchanger;
132 Object m = new Integer(17);
133 long p = patience;
134 start.await();
135 for (;;) {
136 try {
137 Object e = x.exchange(m, p, TimeUnit.NANOSECONDS);
138 if (e == null || e == m)
139 throw new Error();
140 m = e;
141 ++i;
142 } catch (TimeoutException to) {
143 if (Thread.interrupted()) {
144 iters = i;
145 return;
146 }
147 ++i;
148 ++failures;
149 }
150 }
151 } catch (InterruptedException ie) {
152 iters = i;
153 }
154 }
155 }
156 }
157