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 StringMapLoops { |
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 |
String[] key = makeKeys(nkeys); |
62 |
|
63 |
int k = 1; |
64 |
int warmups = 2; |
65 |
for (int i = 1; i <= maxThreads;) { |
66 |
Thread.sleep(100); |
67 |
test(i, nkeys, key, mapClass); |
68 |
shuffleKeys(key); |
69 |
if (warmups > 0) |
70 |
--warmups; |
71 |
else if (i == k) { |
72 |
k = i << 1; |
73 |
i = i + (i >>> 1); |
74 |
} |
75 |
else if (i == 1 && k == 2) { |
76 |
i = k; |
77 |
warmups = 1; |
78 |
} |
79 |
else |
80 |
i = k; |
81 |
} |
82 |
pool.shutdown(); |
83 |
} |
84 |
|
85 |
static String[] makeKeys(int n) { |
86 |
LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
87 |
String[] key = new String[n]; |
88 |
for (int i = 0; i < key.length; ++i) { |
89 |
int k = 0; |
90 |
int len = 1 + (rng.next() & 0xf); |
91 |
char[] c = new char[len * 4]; |
92 |
for (int j = 0; j < len; ++j) { |
93 |
int r = rng.next(); |
94 |
c[k++] = (char)(' ' + (r & 0x7f)); |
95 |
r >>>= 8; |
96 |
c[k++] = (char)(' ' + (r & 0x7f)); |
97 |
r >>>= 8; |
98 |
c[k++] = (char)(' ' + (r & 0x7f)); |
99 |
r >>>= 8; |
100 |
c[k++] = (char)(' ' + (r & 0x7f)); |
101 |
} |
102 |
key[i] = new String(c); |
103 |
} |
104 |
return key; |
105 |
} |
106 |
|
107 |
static void shuffleKeys(String[] key) { |
108 |
Random rng = new Random(); |
109 |
for (int i = key.length; i > 1; --i) { |
110 |
int j = rng.nextInt(i); |
111 |
String tmp = key[j]; |
112 |
key[j] = key[i-1]; |
113 |
key[i-1] = tmp; |
114 |
} |
115 |
} |
116 |
|
117 |
static void test(int i, int nkeys, String[] key, Class mapClass) throws Exception { |
118 |
System.out.print("Threads: " + i + "\t:"); |
119 |
Map<String, String> map = (Map<String,String>)mapClass.newInstance(); |
120 |
// Uncomment to start with a non-empty table |
121 |
// for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied |
122 |
// map.put(key[j], key[j]); |
123 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
124 |
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
125 |
for (int t = 0; t < i; ++t) |
126 |
pool.execute(new Runner(t, map, key, barrier)); |
127 |
barrier.await(); |
128 |
barrier.await(); |
129 |
long time = timer.getTime(); |
130 |
long tpo = time / (i * (long)nops); |
131 |
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
132 |
double secs = (double)(time) / 1000000000.0; |
133 |
System.out.println("\t " + secs + "s run time"); |
134 |
map.clear(); |
135 |
} |
136 |
|
137 |
static class Runner implements Runnable { |
138 |
final Map<String,String> map; |
139 |
final String[] key; |
140 |
final LoopHelpers.SimpleRandom rng; |
141 |
final CyclicBarrier barrier; |
142 |
int position; |
143 |
int total; |
144 |
|
145 |
Runner(int id, Map<String,String> map, String[] key, CyclicBarrier barrier) { |
146 |
this.map = map; |
147 |
this.key = key; |
148 |
this.barrier = barrier; |
149 |
position = key.length / 2; |
150 |
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
151 |
rng.next(); |
152 |
} |
153 |
|
154 |
int step() { |
155 |
// random-walk around key positions, bunching accesses |
156 |
int r = rng.next(); |
157 |
position += (r & 7) - 3; |
158 |
while (position >= key.length) position -= key.length; |
159 |
while (position < 0) position += key.length; |
160 |
|
161 |
String k = key[position]; |
162 |
String x = map.get(k); |
163 |
|
164 |
if (x != null) { |
165 |
if (r < removesPerMaxRandom) { |
166 |
if (map.remove(k) != null) { |
167 |
position = total % key.length; // move from position |
168 |
return 2; |
169 |
} |
170 |
} |
171 |
} |
172 |
else if (r < insertsPerMaxRandom) { |
173 |
++position; |
174 |
map.put(k, k); |
175 |
return 2; |
176 |
} |
177 |
|
178 |
total += r; |
179 |
return 1; |
180 |
} |
181 |
|
182 |
public void run() { |
183 |
try { |
184 |
barrier.await(); |
185 |
int ops = nops; |
186 |
while (ops > 0) |
187 |
ops -= step(); |
188 |
barrier.await(); |
189 |
} |
190 |
catch (Exception ex) { |
191 |
ex.printStackTrace(); |
192 |
} |
193 |
} |
194 |
} |
195 |
} |
196 |
|