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