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 |
|
* @test |
8 |
|
* @synopsis Times and checks basic set operations |
25 |
|
} |
26 |
|
|
27 |
|
public static void main(String[] args) throws Exception { |
28 |
< |
Class setClass = null; |
28 |
> |
Class<?> setClass = null; |
29 |
|
int numTests = 50; |
30 |
|
int size = 50000; |
31 |
|
|
32 |
|
if (args.length > 0) { |
33 |
|
try { |
34 |
|
setClass = Class.forName(args[0]); |
35 |
< |
} catch(ClassNotFoundException e) { |
35 |
> |
} catch (ClassNotFoundException e) { |
36 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
37 |
|
} |
38 |
|
} |
39 |
|
|
40 |
|
|
41 |
< |
if (args.length > 1) |
41 |
> |
if (args.length > 1) |
42 |
|
numTests = Integer.parseInt(args[1]); |
43 |
|
|
44 |
< |
if (args.length > 2) |
44 |
> |
if (args.length > 2) |
45 |
|
size = Integer.parseInt(args[2]); |
46 |
|
|
47 |
|
System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size); |
52 |
|
absentMask = absentSize - 1; |
53 |
|
absent = new Integer[absentSize]; |
54 |
|
|
55 |
< |
for (int i = 0; i < absentSize; ++i) |
55 |
> |
for (int i = 0; i < absentSize; ++i) |
56 |
|
absent[i] = new Integer(i * 2); |
57 |
|
|
58 |
|
Integer[] key = new Integer[size]; |
59 |
< |
for (int i = 0; i < size; ++i) |
59 |
> |
for (int i = 0; i < size; ++i) |
60 |
|
key[i] = new Integer(i * 2 + 1); |
61 |
|
|
62 |
|
|
68 |
|
|
69 |
|
} |
70 |
|
|
71 |
< |
static NavigableSet newSet(Class cl) { |
71 |
> |
static NavigableSet newSet(Class<?> cl) { |
72 |
|
try { |
73 |
< |
NavigableSet m = (NavigableSet)cl.newInstance(); |
73 |
> |
NavigableSet m = (NavigableSet) cl.newInstance(); |
74 |
|
return m; |
75 |
< |
} catch(Exception e) { |
75 |
> |
} catch (Exception e) { |
76 |
|
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
77 |
|
} |
78 |
|
} |
95 |
|
if (s.contains(key[i])) ++sum; |
96 |
|
} |
97 |
|
} |
98 |
< |
timer.finish(); |
99 |
< |
reallyAssert (sum == expect * iters); |
98 |
> |
timer.finish(); |
99 |
> |
reallyAssert(sum == expect * iters); |
100 |
|
} |
101 |
|
|
102 |
|
static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
105 |
|
for (int i = 0; i < n; i++) { |
106 |
|
if (s.remove(key[i])) ++sum; |
107 |
|
} |
108 |
< |
timer.finish(); |
109 |
< |
reallyAssert (sum == expect); |
108 |
> |
timer.finish(); |
109 |
> |
reallyAssert(sum == expect); |
110 |
|
} |
111 |
|
|
112 |
|
static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
115 |
|
for (int i = 0; i < n; i++) { |
116 |
|
if (s.add(key[i])) ++sum; |
117 |
|
} |
118 |
< |
timer.finish(); |
119 |
< |
reallyAssert (sum == expect); |
118 |
> |
timer.finish(); |
119 |
> |
reallyAssert(sum == expect); |
120 |
|
} |
121 |
|
|
122 |
|
static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
125 |
|
for (int i = 0; i < n; i++) { |
126 |
|
if (s.contains(key[i])) ++sum; |
127 |
|
} |
128 |
< |
timer.finish(); |
129 |
< |
reallyAssert (sum == expect); |
128 |
> |
timer.finish(); |
129 |
> |
reallyAssert(sum == expect); |
130 |
|
} |
131 |
|
|
132 |
|
static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
135 |
|
for (int i = n-2; i >= 0; i-=2) { |
136 |
|
if (s.remove(key[i])) ++sum; |
137 |
|
} |
138 |
< |
timer.finish(); |
139 |
< |
reallyAssert (sum == expect); |
138 |
> |
timer.finish(); |
139 |
> |
reallyAssert(sum == expect); |
140 |
|
} |
141 |
|
|
142 |
|
static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
146 |
|
if (s.contains(k1[i])) ++sum; |
147 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
148 |
|
} |
149 |
< |
timer.finish(); |
150 |
< |
reallyAssert (sum == n); |
149 |
> |
timer.finish(); |
150 |
> |
reallyAssert(sum == n); |
151 |
|
} |
152 |
|
|
153 |
|
static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
157 |
|
if (s.contains(k1[i])) ++sum; |
158 |
|
if (s.contains(k2[i & absentMask])) ++sum; |
159 |
|
} |
160 |
< |
timer.finish(); |
161 |
< |
reallyAssert (sum == n); |
160 |
> |
timer.finish(); |
161 |
> |
reallyAssert(sum == n); |
162 |
|
} |
163 |
|
|
164 |
|
static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
167 |
|
for (int i = 0; i < n; i++) { |
168 |
|
if (s.contains(key[i])) ++sum; |
169 |
|
} |
170 |
< |
timer.finish(); |
171 |
< |
reallyAssert (sum == expect); |
170 |
> |
timer.finish(); |
171 |
> |
reallyAssert(sum == expect); |
172 |
|
} |
173 |
|
|
174 |
|
|
181 |
|
++sum; |
182 |
|
e = s.higher(e); |
183 |
|
} |
184 |
< |
timer.finish(); |
185 |
< |
reallyAssert (sum == iters); |
184 |
> |
timer.finish(); |
185 |
> |
reallyAssert(sum == iters); |
186 |
|
} |
187 |
|
|
188 |
|
static void lowerTest(NavigableSet s) { |
194 |
|
++sum; |
195 |
|
e = s.higher(e); |
196 |
|
} |
197 |
< |
timer.finish(); |
198 |
< |
reallyAssert (sum == iters); |
197 |
> |
timer.finish(); |
198 |
> |
reallyAssert(sum == iters); |
199 |
|
} |
200 |
|
|
201 |
|
static void ceilingTest(NavigableSet s) { |
208 |
|
if (e != null) |
209 |
|
++sum; |
210 |
|
} |
211 |
< |
timer.finish(); |
212 |
< |
reallyAssert (sum == iters); |
211 |
> |
timer.finish(); |
212 |
> |
reallyAssert(sum == iters); |
213 |
|
} |
214 |
|
|
215 |
|
static void floorTest(NavigableSet s) { |
222 |
|
if (e != null) |
223 |
|
++sum; |
224 |
|
} |
225 |
< |
timer.finish(); |
226 |
< |
reallyAssert (sum == iters-1); |
225 |
> |
timer.finish(); |
226 |
> |
reallyAssert(sum == iters-1); |
227 |
|
} |
228 |
|
|
229 |
|
|
233 |
|
for (int i = 0; i < size; i++) { |
234 |
|
if (s.contains(key[i])) ++sum; |
235 |
|
} |
236 |
< |
timer.finish(); |
237 |
< |
reallyAssert (sum == size); |
236 |
> |
timer.finish(); |
237 |
> |
reallyAssert(sum == size); |
238 |
|
} |
239 |
|
|
240 |
|
|
242 |
|
int sum = 0; |
243 |
|
timer.start("Iter Key ", size); |
244 |
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
245 |
< |
if(it.next() != MISSING) |
245 |
> |
if (it.next() != MISSING) |
246 |
|
++sum; |
247 |
|
} |
248 |
< |
timer.finish(); |
249 |
< |
reallyAssert (sum == size); |
248 |
> |
timer.finish(); |
249 |
> |
reallyAssert(sum == size); |
250 |
|
} |
251 |
|
|
252 |
|
static void ittest(NavigableSet s, int size) { |
257 |
|
int sum = 0; |
258 |
|
timer.start("Desc Iter Key ", size); |
259 |
|
for (Iterator it = s.descendingIterator(); it.hasNext(); ) { |
260 |
< |
if(it.next() != MISSING) |
260 |
> |
if (it.next() != MISSING) |
261 |
|
++sum; |
262 |
|
} |
263 |
< |
timer.finish(); |
264 |
< |
reallyAssert (sum == size); |
263 |
> |
timer.finish(); |
264 |
> |
reallyAssert(sum == size); |
265 |
|
} |
266 |
|
|
267 |
|
static void rittest(NavigableSet s, int size) { |
275 |
|
it.next(); |
276 |
|
it.remove(); |
277 |
|
} |
278 |
< |
timer.finish(); |
278 |
> |
timer.finish(); |
279 |
|
} |
280 |
|
|
281 |
|
|
288 |
|
s2.addAll(s); |
289 |
|
} |
290 |
|
catch (Exception e) { e.printStackTrace(); return; } |
291 |
< |
timer.finish(); |
292 |
< |
|
291 |
> |
timer.finish(); |
292 |
> |
|
293 |
|
timer.start("Iter Equals ", size * 2); |
294 |
|
boolean eqt = s2.equals(s) && s.equals(s2); |
295 |
< |
reallyAssert (eqt); |
296 |
< |
timer.finish(); |
295 |
> |
reallyAssert(eqt); |
296 |
> |
timer.finish(); |
297 |
|
|
298 |
|
timer.start("Iter HashCode ", size * 2); |
299 |
|
int shc = s.hashCode(); |
300 |
|
int s2hc = s2.hashCode(); |
301 |
< |
reallyAssert (shc == s2hc); |
302 |
< |
timer.finish(); |
301 |
> |
reallyAssert(shc == s2hc); |
302 |
> |
timer.finish(); |
303 |
|
|
304 |
|
timer.start("Add (present) ", size); |
305 |
|
s2.addAll(s); |
306 |
< |
timer.finish(); |
306 |
> |
timer.finish(); |
307 |
|
|
308 |
|
t6("Contains ", size, s2, key, absent); |
309 |
|
|
312 |
|
timer.start("Iter Equals ", size * 2); |
313 |
|
eqt = s2.equals(s) && s.equals(s2); |
314 |
|
if (as2) |
315 |
< |
reallyAssert (!eqt); |
316 |
< |
timer.finish(); |
315 |
> |
reallyAssert(!eqt); |
316 |
> |
timer.finish(); |
317 |
|
|
318 |
|
timer.start("Iter HashCode ", size * 2); |
319 |
|
int s1h = s.hashCode(); |
320 |
|
int s2h = s2.hashCode(); |
321 |
|
if (as2) |
322 |
< |
reallyAssert (s1h != s2h); |
323 |
< |
timer.finish(); |
322 |
> |
reallyAssert(s1h != s2h); |
323 |
> |
timer.finish(); |
324 |
|
|
325 |
|
timer.start("Clear ", size); |
326 |
|
s.clear(); |
327 |
|
s2.clear(); |
328 |
< |
timer.finish(); |
329 |
< |
reallyAssert (s2.isEmpty() && s.isEmpty()); |
328 |
> |
timer.finish(); |
329 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
< |
|
333 |
> |
|
334 |
|
static void test(NavigableSet s, Integer[] key) { |
335 |
|
int size = key.length; |
336 |
|
|
368 |
|
private String cname; |
369 |
|
|
370 |
|
static final java.util.TreeMap accum = new java.util.TreeMap(); |
371 |
< |
|
371 |
> |
|
372 |
|
static void printStats() { |
373 |
|
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
374 |
< |
Map.Entry e = (Map.Entry)(it.next()); |
375 |
< |
Stats stats = ((Stats)(e.getValue())); |
374 |
> |
Map.Entry e = (Map.Entry) it.next(); |
375 |
> |
Stats stats = (Stats) e.getValue(); |
376 |
|
int n = stats.number; |
377 |
|
double t; |
378 |
< |
if (n > 0) |
378 |
> |
if (n > 0) |
379 |
|
t = stats.sum / n; |
380 |
|
else |
381 |
|
t = stats.least; |
383 |
|
System.out.println(e.getKey() + ": " + nano); |
384 |
|
} |
385 |
|
} |
386 |
< |
|
386 |
> |
|
387 |
|
void start(String name, long numOps) { |
388 |
|
this.name = name; |
389 |
|
this.cname = classify(); |
390 |
|
this.numOps = numOps; |
391 |
|
startTime = System.currentTimeMillis(); |
392 |
|
} |
393 |
< |
|
393 |
> |
|
394 |
|
|
395 |
|
String classify() { |
396 |
|
if (name.startsWith("Contains")) |
401 |
|
return "Remove "; |
402 |
|
else if (name.startsWith("Iter")) |
403 |
|
return "Iter "; |
404 |
< |
else |
404 |
> |
else |
405 |
|
return null; |
406 |
|
} |
407 |
|
|
408 |
|
void finish() { |
409 |
|
long endTime = System.currentTimeMillis(); |
410 |
|
long time = endTime - startTime; |
411 |
< |
double timePerOp = ((double)time)/numOps; |
411 |
> |
double timePerOp = (double) time / numOps; |
412 |
|
|
413 |
|
Object st = accum.get(name); |
414 |
|
if (st == null) |
447 |
|
|
448 |
|
static void shuffle(Integer[] keys) { |
449 |
|
int size = keys.length; |
450 |
< |
for (int i=size; i>1; i--) { |
450 |
> |
for (int i = size; i > 1; i--) { |
451 |
|
int r = rng.nextInt(i); |
452 |
|
Integer t = keys[i-1]; |
453 |
|
keys[i-1] = keys[r]; |
456 |
|
} |
457 |
|
|
458 |
|
} |
454 |
– |
|