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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.*; |
11 |
|
/** |
12 |
|
* A micro-benchmark with key types and operation mixes roughly |
13 |
|
* corresponding to some real programs. |
14 |
< |
* |
14 |
> |
* |
15 |
|
* The main results are a table of approximate nanoseconds per |
16 |
|
* element-operation (averaged across get, put etc) for each type, |
17 |
|
* across a range of map sizes. It also includes category "Mixed" |
24 |
|
* dynamic type specialization. Some test classes, like Float and |
25 |
|
* BigDecimal are included not because they are commonly used as keys, |
26 |
|
* but because they can be problematic for some map implementations. |
27 |
< |
* |
27 |
> |
* |
28 |
|
* By default, it creates and inserts in order dense numerical keys |
29 |
|
* and searches for keys in scrambled order. Use "s" as second arg to |
30 |
|
* instead insert and search in unscrambled order. |
31 |
|
* |
32 |
|
* For String keys, the program tries to use file "testwords.txt", which |
33 |
|
* is best used with real words. We can't check in this file, but you |
34 |
< |
* can create one from a real dictonary (1 line per word) and then run |
34 |
> |
* can create one from a real dictionary (1 line per word) and then run |
35 |
|
* linux "shuf" to randomize entries. If no file exists, it uses |
36 |
|
* String.valueOf(i) for element i. |
37 |
|
*/ |
38 |
|
public class MapMicroBenchmark { |
39 |
|
static final String wordFile = "testwords.txt"; |
40 |
|
|
41 |
< |
static Class mapClass; |
41 |
> |
static Class<?> mapClass; |
42 |
|
static boolean randomSearches = true; |
43 |
|
|
44 |
|
// Nanoseconds per run |
61 |
|
System.out.println("Usage: java MapMicroBenchmark className [r|s]keys [r|s]searches"); |
62 |
|
return; |
63 |
|
} |
64 |
< |
|
64 |
> |
|
65 |
|
mapClass = Class.forName(args[0]); |
66 |
|
|
67 |
|
if (args.length > 1) { |
102 |
|
jobs[2] = new Job("Integer ", is, Integer.class); |
103 |
|
|
104 |
|
Object[] ls = new Object[n]; |
105 |
< |
for (int i = 0; i < n; i++) ls[i] = Long.valueOf((long)i); |
105 |
> |
for (int i = 0; i < n; i++) ls[i] = Long.valueOf((long) i); |
106 |
|
jobs[3] = new Job("Long ", ls, Long.class); |
107 |
|
|
108 |
|
Object[] fs = new Object[n]; |
109 |
< |
for (int i = 0; i < n; i++) fs[i] = Float.valueOf((float)i); |
109 |
> |
for (int i = 0; i < n; i++) fs[i] = Float.valueOf((float) i); |
110 |
|
jobs[4] = new Job("Float ", fs, Float.class); |
111 |
|
|
112 |
|
Object[] ds = new Object[n]; |
113 |
< |
for (int i = 0; i < n; i++) ds[i] = Double.valueOf((double)i); |
113 |
> |
for (int i = 0; i < n; i++) ds[i] = Double.valueOf((double) i); |
114 |
|
jobs[5] = new Job("Double ", ds, Double.class); |
115 |
|
|
116 |
|
Object[] bs = new Object[n]; |
143 |
|
warmup1(mixed); |
144 |
|
warmup3(jobs); |
145 |
|
Thread.sleep(500); |
146 |
< |
time(jobs); |
146 |
> |
time(jobs); |
147 |
|
} |
148 |
|
|
149 |
|
static void runWork(Job[] jobs, int minIters, int maxIters, long timeLimit) throws Throwable { |
150 |
< |
for (int k = 0; k < nsizes; ++k) { |
150 |
> |
for (int k = 0; k < nsizes; ++k) { |
151 |
|
int len = sizes[k]; |
152 |
|
for (int i = 0; i < jobs.length; i++) { |
153 |
|
Thread.sleep(50); |
160 |
|
|
161 |
|
// First warmup -- run only mixed job to discourage type specialization |
162 |
|
static void warmup1(Job job) throws Throwable { |
163 |
< |
for (int k = 0; k < nsizes; ++k) |
163 |
> |
for (int k = 0; k < nsizes; ++k) |
164 |
|
job.work(sizes[k], 1, 1, 0); |
165 |
|
} |
166 |
|
|
193 |
|
long[] aves = new long[nsizes]; |
194 |
|
int njobs = jobs.length; |
195 |
|
|
196 |
< |
for (int i = 0; i < njobs; i++) { |
196 |
> |
for (int i = 0; i < njobs; i++) { |
197 |
|
System.out.print(jobs[i].name); |
198 |
|
for (int k = 0; k < nsizes; ++k) { |
199 |
|
long nanos = jobs[i].nanos[k]; |
212 |
|
|
213 |
|
|
214 |
|
static final class Job { |
215 |
< |
final String name; |
216 |
< |
final Class elementClass; |
215 |
> |
final String name; |
216 |
> |
final Class<?> elementClass; |
217 |
|
long[] nanos = new long[nsizes]; |
218 |
|
final Object[] items; |
219 |
|
Object[] searches; |
220 |
|
volatile long checkSum; |
221 |
|
volatile int lastSum; |
222 |
< |
Job(String name, Object[] items, Class elementClass) { |
222 |
> |
Job(String name, Object[] items, Class<?> elementClass) { |
223 |
|
this.name = name; |
224 |
|
this.items = items; |
225 |
|
this.elementClass = elementClass; |
236 |
|
public long work(int len, int minIters, int maxIters, long timeLimit) { |
237 |
|
Map m; |
238 |
|
try { |
239 |
< |
m = (Map)mapClass.newInstance(); |
240 |
< |
} catch(Exception e) { |
239 |
> |
m = (Map) mapClass.newInstance(); |
240 |
> |
} catch (Exception e) { |
241 |
|
throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); |
242 |
|
} |
243 |
|
Object[] ins = items; |
261 |
|
sum += len - half; |
262 |
|
for (int i = 0; i < len; ++i) { |
263 |
|
Object x = keys[i]; |
264 |
< |
Object v = m.get(x); |
264 |
> |
Object v = m.get(x); |
265 |
|
if (elementClass.isInstance(v)) // touch v |
266 |
|
++sum; |
267 |
|
} |
273 |
|
} |
274 |
|
checkSum += sum ^ (sum << 3); |
275 |
|
for (Object e : m.keySet()) { |
276 |
< |
if (elementClass.isInstance(e)) |
276 |
> |
if (elementClass.isInstance(e)) |
277 |
|
++sum; |
278 |
|
} |
279 |
|
checkSum += sum ^ (sum << 4); |
280 |
|
for (Object e : m.values()) { |
281 |
< |
if (elementClass.isInstance(e)) |
281 |
> |
if (elementClass.isInstance(e)) |
282 |
|
++sum; |
283 |
|
} |
284 |
|
checkSum += sum ^ (sum << 5); |
334 |
|
if (m.put(x, x) == null) |
335 |
|
++sum; |
336 |
|
} |
337 |
+ |
/* // uncomment to avoid calling clear() |
338 |
+ |
for (int i = 0; i < len; ++i) { |
339 |
+ |
Object x = keys[i]; |
340 |
+ |
m.remove(x); |
341 |
+ |
} |
342 |
+ |
*/ |
343 |
|
m.clear(); |
344 |
|
sum += len - (quarter * 2); |
345 |
|
checkSum += sum ^ (sum << 12); |
346 |
|
|
347 |
|
if (j == 0 && sum != lastSum + len * OPS_PER_ITER) |
348 |
|
throw new Error(name); |
349 |
< |
|
349 |
> |
|
350 |
|
elapsed = System.nanoTime() - startTime; |
351 |
|
++j; |
352 |
|
if (j >= minIters && |
353 |
|
(j >= maxIters || elapsed >= timeLimit)) |
354 |
|
break; |
355 |
|
// non-warmup - swap some keys for next insert |
356 |
< |
if (minIters != 1 && randomSearches) |
356 |
> |
if (minIters != 1 && randomSearches) |
357 |
|
shuffleSome(ins, len, len >>> 3); |
358 |
|
} |
359 |
< |
long ops = ((long)j) * len * OPS_PER_ITER; |
359 |
> |
long ops = ((long) j) * len * OPS_PER_ITER; |
360 |
|
lastSum = sum; |
361 |
|
return elapsed / ops; |
362 |
|
} |
371 |
|
// more realistic |
372 |
|
static void scramble(Object[] a) { |
373 |
|
for (int k = 0; k < sizes.length; ++k) { |
374 |
< |
int origin = k == 0? 0 : sizes[k-1]; |
374 |
> |
int origin = (k == 0) ? 0 : sizes[k-1]; |
375 |
|
for (int i = sizes[k]; i > origin + 1; i--) { |
376 |
|
Object t = a[i-1]; |
377 |
|
int r = rng.nextInt(i - origin) + origin; |
379 |
|
a[r] = t; |
380 |
|
} |
381 |
|
} |
382 |
< |
} |
382 |
> |
} |
383 |
|
|
384 |
|
// plain array shuffle |
385 |
|
static void shuffle(Object[] a, int size) { |
386 |
< |
for (int i= size; i>1; i--) { |
386 |
> |
for (int i = size; i > 1; i--) { |
387 |
|
Object t = a[i-1]; |
388 |
|
int r = rng.nextInt(i); |
389 |
|
a[i-1] = a[r]; |
431 |
|
} |
432 |
|
|
433 |
|
// Read in String keys from file if possible |
434 |
< |
static void initStringKeys(Object[] keys, int n) throws Exception { |
434 |
> |
static void initStringKeys(Object[] keys, int n) throws Exception { |
435 |
|
FileInputStream fr = null; |
436 |
|
try { |
437 |
|
fr = new FileInputStream(wordFile); |
444 |
|
|
445 |
|
BufferedInputStream in = new BufferedInputStream(fr); |
446 |
|
int k = 0; |
447 |
< |
outer:while (k < n) { |
447 |
> |
outer: while (k < n) { |
448 |
|
StringBuffer sb = new StringBuffer(); |
449 |
|
for (;;) { |
450 |
|
int c = in.read(); |
451 |
< |
if (c < 0) |
451 |
> |
if (c < 0) |
452 |
|
break outer; |
453 |
< |
char ch = (char)c; |
453 |
> |
char ch = (char) c; |
454 |
|
if (ch == '\n') { |
455 |
|
keys[k++] = sb.toString(); |
456 |
|
break; |
464 |
|
// fill up remaining keys with path-like compounds of previous pairs |
465 |
|
int j = 0; |
466 |
|
while (k < n) |
467 |
< |
keys[k++] = (String)keys[j++] + "/" + (String)keys[j]; |
467 |
> |
keys[k++] = (String) keys[j++] + "/" + (String) keys[j]; |
468 |
|
} |
469 |
|
|
470 |
|
} |
465 |
– |
|