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