1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.3 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.7 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
import java.util.*; |
8 |
|
|
import java.util.concurrent.*; |
9 |
|
|
|
10 |
|
|
public class MapLoops { |
11 |
jsr166 |
1.4 |
static int nkeys = 1000; |
12 |
dl |
1.1 |
static int pinsert = 60; |
13 |
|
|
static int premove = 2; |
14 |
|
|
static int maxThreads = 100; |
15 |
dl |
1.2 |
static int nops = 1000000; |
16 |
dl |
1.1 |
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 |
jsr166 |
1.11 |
Class<?> mapClass = null; |
24 |
dl |
1.1 |
if (args.length > 0) { |
25 |
|
|
try { |
26 |
|
|
mapClass = Class.forName(args[0]); |
27 |
jsr166 |
1.5 |
} catch (ClassNotFoundException e) { |
28 |
dl |
1.1 |
throw new RuntimeException("Class " + args[0] + " not found."); |
29 |
|
|
} |
30 |
|
|
} |
31 |
jsr166 |
1.4 |
else |
32 |
dl |
1.1 |
mapClass = java.util.concurrent.ConcurrentHashMap.class; |
33 |
|
|
|
34 |
jsr166 |
1.4 |
if (args.length > 1) |
35 |
dl |
1.1 |
maxThreads = Integer.parseInt(args[1]); |
36 |
|
|
|
37 |
jsr166 |
1.4 |
if (args.length > 2) |
38 |
dl |
1.1 |
nkeys = Integer.parseInt(args[2]); |
39 |
|
|
|
40 |
jsr166 |
1.4 |
if (args.length > 3) |
41 |
dl |
1.1 |
pinsert = Integer.parseInt(args[3]); |
42 |
|
|
|
43 |
jsr166 |
1.4 |
if (args.length > 4) |
44 |
dl |
1.1 |
premove = Integer.parseInt(args[4]); |
45 |
|
|
|
46 |
jsr166 |
1.4 |
if (args.length > 5) |
47 |
dl |
1.1 |
nops = Integer.parseInt(args[5]); |
48 |
|
|
|
49 |
dl |
1.12 |
if (nops < nkeys) |
50 |
|
|
nops = nkeys; |
51 |
|
|
|
52 |
dl |
1.1 |
// normalize probabilities wrt random number generator |
53 |
|
|
removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); |
54 |
|
|
insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); |
55 |
jsr166 |
1.4 |
|
56 |
dl |
1.1 |
System.out.print("Class: " + mapClass.getName()); |
57 |
|
|
System.out.print(" threads: " + maxThreads); |
58 |
|
|
System.out.print(" size: " + nkeys); |
59 |
|
|
System.out.print(" ins: " + pinsert); |
60 |
|
|
System.out.print(" rem: " + premove); |
61 |
|
|
System.out.print(" ops: " + nops); |
62 |
|
|
System.out.println(); |
63 |
|
|
|
64 |
|
|
int warmups = 2; |
65 |
dl |
1.12 |
for (int reps = 0; reps < 3; ++reps) { |
66 |
|
|
int k = 1; |
67 |
|
|
for (int i = 1; i <= maxThreads;) { |
68 |
|
|
Thread.sleep(100); |
69 |
|
|
test(i, nkeys, mapClass); |
70 |
|
|
if (warmups > 0) |
71 |
|
|
--warmups; |
72 |
|
|
else if (i == k) { |
73 |
|
|
k = i << 1; |
74 |
|
|
i = i + (i >>> 1); |
75 |
|
|
} |
76 |
|
|
else if (i == 1 && k == 2) { |
77 |
|
|
i = k; |
78 |
|
|
warmups = 1; |
79 |
|
|
} |
80 |
|
|
else |
81 |
|
|
i = k; |
82 |
dl |
1.1 |
} |
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 |
jsr166 |
1.4 |
for (int i = 0; i < key.length; ++i) |
91 |
dl |
1.1 |
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 |
jsr166 |
1.11 |
static void test(int i, int nkeys, Class<?> mapClass) throws Exception { |
106 |
dl |
1.1 |
System.out.print("Threads: " + i + "\t:"); |
107 |
jsr166 |
1.13 |
Map<Integer, Integer> map = |
108 |
|
|
(Map<Integer,Integer>) mapClass.getConstructor().newInstance(); |
109 |
dl |
1.1 |
Integer[] key = makeKeys(nkeys); |
110 |
|
|
// Uncomment to start with a non-empty table |
111 |
|
|
// for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied |
112 |
|
|
// map.put(key[j], key[j]); |
113 |
dl |
1.2 |
shuffleKeys(key); |
114 |
dl |
1.1 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
115 |
|
|
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
116 |
jsr166 |
1.4 |
for (int t = 0; t < i; ++t) |
117 |
dl |
1.2 |
pool.execute(new Runner(t, map, key, barrier)); |
118 |
dl |
1.1 |
barrier.await(); |
119 |
|
|
barrier.await(); |
120 |
|
|
long time = timer.getTime(); |
121 |
jsr166 |
1.6 |
long tpo = time / (i * (long) nops); |
122 |
dl |
1.1 |
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
123 |
jsr166 |
1.6 |
double secs = (double) time / 1000000000.0; |
124 |
dl |
1.1 |
System.out.println("\t " + secs + "s run time"); |
125 |
|
|
map.clear(); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
static class Runner implements Runnable { |
129 |
|
|
final Map<Integer,Integer> map; |
130 |
|
|
final Integer[] key; |
131 |
dl |
1.2 |
final LoopHelpers.SimpleRandom rng; |
132 |
dl |
1.1 |
final CyclicBarrier barrier; |
133 |
|
|
int position; |
134 |
|
|
int total; |
135 |
|
|
|
136 |
dl |
1.12 |
Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) { |
137 |
jsr166 |
1.4 |
this.map = map; |
138 |
|
|
this.key = key; |
139 |
dl |
1.1 |
this.barrier = barrier; |
140 |
jsr166 |
1.4 |
position = key.length / 2; |
141 |
dl |
1.2 |
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
142 |
|
|
rng.next(); |
143 |
dl |
1.1 |
} |
144 |
|
|
|
145 |
|
|
int step() { |
146 |
jsr166 |
1.9 |
// random-walk around key positions, bunching accesses |
147 |
dl |
1.1 |
int r = rng.next(); |
148 |
|
|
position += (r & 7) - 3; |
149 |
jsr166 |
1.4 |
while (position >= key.length) position -= key.length; |
150 |
dl |
1.1 |
while (position < 0) position += key.length; |
151 |
|
|
|
152 |
|
|
Integer k = key[position]; |
153 |
|
|
Integer x = map.get(k); |
154 |
|
|
|
155 |
|
|
if (x != null) { |
156 |
jsr166 |
1.4 |
if (x.intValue() != k.intValue()) |
157 |
dl |
1.1 |
throw new Error("bad mapping: " + x + " to " + k); |
158 |
|
|
|
159 |
|
|
if (r < removesPerMaxRandom) { |
160 |
|
|
if (map.remove(k) != null) { |
161 |
|
|
position = total % key.length; // move from position |
162 |
|
|
return 2; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
else if (r < insertsPerMaxRandom) { |
167 |
|
|
++position; |
168 |
dl |
1.12 |
map.putIfAbsent(k, k); |
169 |
dl |
1.1 |
return 2; |
170 |
jsr166 |
1.4 |
} |
171 |
dl |
1.1 |
|
172 |
|
|
// Uncomment to add a little computation between accesses |
173 |
|
|
// total += LoopHelpers.compute1(k.intValue()); |
174 |
|
|
total += r; |
175 |
|
|
return 1; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
public void run() { |
179 |
|
|
try { |
180 |
|
|
barrier.await(); |
181 |
|
|
int ops = nops; |
182 |
jsr166 |
1.4 |
while (ops > 0) |
183 |
dl |
1.1 |
ops -= step(); |
184 |
|
|
barrier.await(); |
185 |
|
|
} |
186 |
|
|
catch (Exception ex) { |
187 |
|
|
ex.printStackTrace(); |
188 |
|
|
} |
189 |
|
|
} |
190 |
|
|
} |
191 |
|
|
} |