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 |
5 |
*/ |
6 |
|
7 |
import java.util.*; |
8 |
import java.util.concurrent.*; |
9 |
|
10 |
public class MapLoops { |
11 |
static int nkeys = 1000; |
12 |
static int pinsert = 60; |
13 |
static int premove = 2; |
14 |
static int maxThreads = 100; |
15 |
static int nops = 1000000; |
16 |
static int removesPerMaxRandom; |
17 |
static int insertsPerMaxRandom; |
18 |
|
19 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
20 |
|
21 |
public static void main(String[] args) throws Exception { |
22 |
|
23 |
Class mapClass = null; |
24 |
if (args.length > 0) { |
25 |
try { |
26 |
mapClass = Class.forName(args[0]); |
27 |
} catch(ClassNotFoundException e) { |
28 |
throw new RuntimeException("Class " + args[0] + " not found."); |
29 |
} |
30 |
} |
31 |
else |
32 |
mapClass = java.util.concurrent.ConcurrentHashMap.class; |
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: " + mapClass.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 |
int k = 1; |
62 |
int warmups = 2; |
63 |
for (int i = 1; i <= maxThreads;) { |
64 |
Thread.sleep(100); |
65 |
test(i, nkeys, mapClass); |
66 |
if (warmups > 0) |
67 |
--warmups; |
68 |
else if (i == k) { |
69 |
k = i << 1; |
70 |
i = i + (i >>> 1); |
71 |
} |
72 |
else if (i == 1 && k == 2) { |
73 |
i = k; |
74 |
warmups = 1; |
75 |
} |
76 |
else |
77 |
i = k; |
78 |
} |
79 |
pool.shutdown(); |
80 |
} |
81 |
|
82 |
static Integer[] makeKeys(int n) { |
83 |
LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
84 |
Integer[] key = new Integer[n]; |
85 |
for (int i = 0; i < key.length; ++i) |
86 |
key[i] = new Integer(rng.next()); |
87 |
return key; |
88 |
} |
89 |
|
90 |
static void shuffleKeys(Integer[] key) { |
91 |
Random rng = new Random(); |
92 |
for (int i = key.length; i > 1; --i) { |
93 |
int j = rng.nextInt(i); |
94 |
Integer tmp = key[j]; |
95 |
key[j] = key[i-1]; |
96 |
key[i-1] = tmp; |
97 |
} |
98 |
} |
99 |
|
100 |
static void test(int i, int nkeys, Class mapClass) throws Exception { |
101 |
System.out.print("Threads: " + i + "\t:"); |
102 |
Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance(); |
103 |
Integer[] key = makeKeys(nkeys); |
104 |
// Uncomment to start with a non-empty table |
105 |
// for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied |
106 |
// map.put(key[j], key[j]); |
107 |
shuffleKeys(key); |
108 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
109 |
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
110 |
for (int t = 0; t < i; ++t) |
111 |
pool.execute(new Runner(t, map, key, barrier)); |
112 |
barrier.await(); |
113 |
barrier.await(); |
114 |
long time = timer.getTime(); |
115 |
long tpo = time / (i * (long)nops); |
116 |
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
117 |
double secs = (double)(time) / 1000000000.0; |
118 |
System.out.println("\t " + secs + "s run time"); |
119 |
map.clear(); |
120 |
} |
121 |
|
122 |
static class Runner implements Runnable { |
123 |
final Map<Integer,Integer> map; |
124 |
final Integer[] key; |
125 |
final LoopHelpers.SimpleRandom rng; |
126 |
final CyclicBarrier barrier; |
127 |
int position; |
128 |
int total; |
129 |
|
130 |
Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) { |
131 |
this.map = map; |
132 |
this.key = key; |
133 |
this.barrier = barrier; |
134 |
position = key.length / 2; |
135 |
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
136 |
rng.next(); |
137 |
} |
138 |
|
139 |
int step() { |
140 |
// random-walk around key positions, bunching accesses |
141 |
int r = rng.next(); |
142 |
position += (r & 7) - 3; |
143 |
while (position >= key.length) position -= key.length; |
144 |
while (position < 0) position += key.length; |
145 |
|
146 |
Integer k = key[position]; |
147 |
Integer x = map.get(k); |
148 |
|
149 |
if (x != null) { |
150 |
if (x.intValue() != k.intValue()) |
151 |
throw new Error("bad mapping: " + x + " to " + k); |
152 |
|
153 |
if (r < removesPerMaxRandom) { |
154 |
if (map.remove(k) != null) { |
155 |
position = total % key.length; // move from position |
156 |
return 2; |
157 |
} |
158 |
} |
159 |
} |
160 |
else if (r < insertsPerMaxRandom) { |
161 |
++position; |
162 |
map.put(k, k); |
163 |
return 2; |
164 |
} |
165 |
|
166 |
// Uncomment to add a little computation between accesses |
167 |
// total += LoopHelpers.compute1(k.intValue()); |
168 |
total += r; |
169 |
return 1; |
170 |
} |
171 |
|
172 |
public void run() { |
173 |
try { |
174 |
barrier.await(); |
175 |
int ops = nops; |
176 |
while (ops > 0) |
177 |
ops -= step(); |
178 |
barrier.await(); |
179 |
} |
180 |
catch (Exception ex) { |
181 |
ex.printStackTrace(); |
182 |
} |
183 |
} |
184 |
} |
185 |
} |