1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
import java.util.Collection; |
8 |
import java.util.Random; |
9 |
import java.util.concurrent.CyclicBarrier; |
10 |
import java.util.concurrent.ExecutorService; |
11 |
import java.util.concurrent.Executors; |
12 |
|
13 |
public class CollectionLoops { |
14 |
static int pinsert = 100; |
15 |
static int premove = 1; |
16 |
static int maxThreads = 48; |
17 |
static int removesPerMaxRandom; |
18 |
static int insertsPerMaxRandom; |
19 |
static volatile int checkSum = 0; |
20 |
static boolean print = false; |
21 |
|
22 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
23 |
|
24 |
public static void main(String[] args) throws Exception { |
25 |
int nkeys = 10000; |
26 |
int nops = 100000; |
27 |
|
28 |
Class<?> collectionClass = null; |
29 |
if (args.length > 0) { |
30 |
try { |
31 |
collectionClass = Class.forName(args[0]); |
32 |
} catch (ClassNotFoundException e) { |
33 |
throw new RuntimeException("Class " + args[0] + " not found."); |
34 |
} |
35 |
} |
36 |
|
37 |
if (args.length > 1) |
38 |
maxThreads = Integer.parseInt(args[1]); |
39 |
|
40 |
if (args.length > 2) |
41 |
nkeys = Integer.parseInt(args[2]); |
42 |
|
43 |
if (args.length > 3) |
44 |
pinsert = Integer.parseInt(args[3]); |
45 |
|
46 |
if (args.length > 4) |
47 |
premove = Integer.parseInt(args[4]); |
48 |
|
49 |
if (args.length > 5) |
50 |
nops = Integer.parseInt(args[5]); |
51 |
|
52 |
// normalize probabilities wrt random number generator |
53 |
removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); |
54 |
insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); |
55 |
|
56 |
System.out.print("Class: " + collectionClass.getName()); |
57 |
System.out.print(" threads: " + maxThreads); |
58 |
System.out.print(" size: " + nkeys); |
59 |
System.out.print(" ins: " + pinsert); |
60 |
System.out.print(" rem: " + premove); |
61 |
System.out.print(" ops: " + nops); |
62 |
System.out.println(); |
63 |
|
64 |
// warmup |
65 |
test(1, 100, 100, collectionClass); |
66 |
test(2, 100, 100, collectionClass); |
67 |
test(4, 100, 100, collectionClass); |
68 |
print = true; |
69 |
|
70 |
int warmups = 2; |
71 |
for (int k = 1, i = 1; i <= maxThreads;) { |
72 |
Thread.sleep(100); |
73 |
test(i, nkeys, nops, collectionClass); |
74 |
if (warmups > 0) |
75 |
--warmups; |
76 |
else if (i == k) { |
77 |
k = i << 1; |
78 |
i = i + (i >>> 1); |
79 |
} |
80 |
else if (i == 1 && k == 2) { |
81 |
i = k; |
82 |
warmups = 1; |
83 |
} |
84 |
else |
85 |
i = k; |
86 |
} |
87 |
pool.shutdown(); |
88 |
} |
89 |
|
90 |
static Integer[] makeKeys(int n) { |
91 |
LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
92 |
Integer[] key = new Integer[n]; |
93 |
for (int i = 0; i < key.length; ++i) |
94 |
key[i] = new Integer(rng.next()); |
95 |
return key; |
96 |
} |
97 |
|
98 |
static void shuffleKeys(Integer[] key) { |
99 |
Random rng = new Random(); |
100 |
for (int i = key.length; i > 1; --i) { |
101 |
int j = rng.nextInt(i); |
102 |
Integer tmp = key[j]; |
103 |
key[j] = key[i-1]; |
104 |
key[i-1] = tmp; |
105 |
} |
106 |
} |
107 |
|
108 |
static void test(int i, int nk, int nops, Class<?> collectionClass) throws Exception { |
109 |
if (print) |
110 |
System.out.print("Threads: " + i + "\t:"); |
111 |
Collection<Integer> collection = |
112 |
(Collection<Integer>) collectionClass.getConstructor().newInstance(); |
113 |
Integer[] key = makeKeys(nk); |
114 |
// Uncomment to start with a non-empty table |
115 |
for (int j = 0; j < nk; j += 2) |
116 |
collection.add(key[j]); |
117 |
shuffleKeys(key); |
118 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
119 |
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
120 |
for (int t = 0; t < i; ++t) |
121 |
pool.execute(new Runner(t, collection, key, barrier, nops)); |
122 |
barrier.await(); |
123 |
barrier.await(); |
124 |
long time = timer.getTime(); |
125 |
long tpo = time / (i * (long) nops); |
126 |
if (print) |
127 |
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
128 |
double secs = (double) time / 1000000000.0; |
129 |
if (print) |
130 |
System.out.print("\t " + secs + "s run time"); |
131 |
if (checkSum == 0) System.out.print(" "); |
132 |
if (print) |
133 |
System.out.println(); |
134 |
collection.clear(); |
135 |
} |
136 |
|
137 |
static class Runner implements Runnable { |
138 |
final Collection<Integer> collection; |
139 |
final Integer[] key; |
140 |
final LoopHelpers.SimpleRandom rng; |
141 |
final CyclicBarrier barrier; |
142 |
int position; |
143 |
int total; |
144 |
int nops; |
145 |
|
146 |
Runner(int id, Collection<Integer> collection, Integer[] key, CyclicBarrier barrier, int nops) { |
147 |
this.collection = collection; |
148 |
this.key = key; |
149 |
this.barrier = barrier; |
150 |
this.nops = nops; |
151 |
position = key.length / (id + 1); |
152 |
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
153 |
rng.next(); |
154 |
} |
155 |
|
156 |
public void run() { |
157 |
try { |
158 |
barrier.await(); |
159 |
int p = position; |
160 |
int ops = nops; |
161 |
Collection<Integer> c = collection; |
162 |
while (ops > 0) { |
163 |
int r = rng.next(); |
164 |
p += (r & 7) - 3; |
165 |
while (p >= key.length) p -= key.length; |
166 |
while (p < 0) p += key.length; |
167 |
|
168 |
Integer k = key[p]; |
169 |
if (c.contains(k)) { |
170 |
if (r < removesPerMaxRandom) { |
171 |
if (c.remove(k)) { |
172 |
p = Math.abs(total % key.length); |
173 |
ops -= 2; |
174 |
continue; |
175 |
} |
176 |
} |
177 |
} |
178 |
else if (r < insertsPerMaxRandom) { |
179 |
++p; |
180 |
ops -= 2; |
181 |
c.add(k); |
182 |
continue; |
183 |
} |
184 |
|
185 |
total += LoopHelpers.compute6(k.intValue()); |
186 |
--ops; |
187 |
} |
188 |
checkSum += total; |
189 |
barrier.await(); |
190 |
} |
191 |
catch (Exception ex) { |
192 |
throw new RuntimeException(ex); |
193 |
} |
194 |
} |
195 |
} |
196 |
} |