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.io.*; |
8 |
+ |
import java.math.*; |
9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
|
12 |
|
public class StringMapLoops { |
13 |
< |
static int nkeys = 1000; |
13 |
> |
static int nkeys = 147456; |
14 |
|
static int pinsert = 60; |
15 |
< |
static int premove = 2; |
15 |
> |
static int premove = 5; |
16 |
|
static int maxThreads = 100; |
17 |
< |
static int nops = 1000000; |
17 |
> |
static int nops = 8000000; |
18 |
|
static int removesPerMaxRandom; |
19 |
|
static int insertsPerMaxRandom; |
20 |
|
|
22 |
|
|
23 |
|
public static void main(String[] args) throws Exception { |
24 |
|
|
25 |
< |
Class mapClass = null; |
25 |
> |
Class<?> mapClass = null; |
26 |
|
if (args.length > 0) { |
27 |
|
try { |
28 |
|
mapClass = Class.forName(args[0]); |
29 |
< |
} catch(ClassNotFoundException e) { |
29 |
> |
} catch (ClassNotFoundException e) { |
30 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
31 |
|
} |
32 |
|
} |
33 |
< |
else |
33 |
> |
else |
34 |
|
mapClass = java.util.concurrent.ConcurrentHashMap.class; |
35 |
|
|
36 |
< |
if (args.length > 1) |
36 |
> |
if (args.length > 1) |
37 |
|
maxThreads = Integer.parseInt(args[1]); |
38 |
|
|
39 |
< |
if (args.length > 2) |
39 |
> |
if (args.length > 2) |
40 |
|
nkeys = Integer.parseInt(args[2]); |
41 |
|
|
42 |
< |
if (args.length > 3) |
42 |
> |
if (args.length > 3) |
43 |
|
pinsert = Integer.parseInt(args[3]); |
44 |
|
|
45 |
< |
if (args.length > 4) |
45 |
> |
if (args.length > 4) |
46 |
|
premove = Integer.parseInt(args[4]); |
47 |
|
|
48 |
< |
if (args.length > 5) |
48 |
> |
if (args.length > 5) |
49 |
|
nops = Integer.parseInt(args[5]); |
50 |
|
|
51 |
|
// normalize probabilities wrt random number generator |
52 |
|
removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); |
53 |
|
insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); |
54 |
< |
|
54 |
> |
|
55 |
|
System.out.print("Class: " + mapClass.getName()); |
56 |
|
System.out.print(" threads: " + maxThreads); |
57 |
|
System.out.print(" size: " + nkeys); |
60 |
|
System.out.print(" ops: " + nops); |
61 |
|
System.out.println(); |
62 |
|
|
63 |
< |
String[] key = makeKeys(nkeys); |
63 |
> |
String[] key = new String[nkeys]; |
64 |
> |
initStringKeys(key, nkeys); |
65 |
> |
// String[] key = makeKeys(nkeys); |
66 |
|
|
63 |
– |
int k = 1; |
67 |
|
int warmups = 2; |
68 |
< |
for (int i = 1; i <= maxThreads;) { |
68 |
> |
for (int k = 1, i = 1; i <= maxThreads;) { |
69 |
|
Thread.sleep(100); |
70 |
|
test(i, nkeys, key, mapClass); |
71 |
|
shuffleKeys(key); |
74 |
|
else if (i == k) { |
75 |
|
k = i << 1; |
76 |
|
i = i + (i >>> 1); |
77 |
< |
} |
77 |
> |
} |
78 |
|
else if (i == 1 && k == 2) { |
79 |
|
i = k; |
80 |
|
warmups = 1; |
81 |
|
} |
82 |
< |
else |
82 |
> |
else |
83 |
|
i = k; |
84 |
|
} |
85 |
< |
pool.shutdown(); |
86 |
< |
} |
87 |
< |
|
88 |
< |
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); |
85 |
> |
for (int j = 0; j < 10; ++j) { |
86 |
> |
Thread.sleep(100); |
87 |
> |
test(1, nkeys, key, mapClass); |
88 |
> |
// shuffleKeys(key); |
89 |
|
} |
90 |
< |
return key; |
90 |
> |
pool.shutdown(); |
91 |
|
} |
92 |
|
|
93 |
|
static void shuffleKeys(String[] key) { |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
< |
static void test(int i, int nkeys, String[] key, Class mapClass) throws Exception { |
103 |
> |
static void test(int i, int nkeys, String[] key, Class<?> mapClass) throws Exception { |
104 |
|
System.out.print("Threads: " + i + "\t:"); |
105 |
|
Map<String, String> map = (Map<String,String>)mapClass.newInstance(); |
106 |
|
// Uncomment to start with a non-empty table |
107 |
|
// for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied |
108 |
|
// map.put(key[j], key[j]); |
109 |
+ |
|
110 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
111 |
|
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
112 |
< |
for (int t = 0; t < i; ++t) |
112 |
> |
for (int t = 0; t < i; ++t) |
113 |
|
pool.execute(new Runner(t, map, key, barrier)); |
114 |
|
barrier.await(); |
115 |
|
barrier.await(); |
116 |
|
long time = timer.getTime(); |
117 |
< |
long tpo = time / (i * (long)nops); |
117 |
> |
long tpo = time / (i * (long) nops); |
118 |
|
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
119 |
< |
double secs = (double)(time) / 1000000000.0; |
119 |
> |
double secs = (double) time / 1000000000.0; |
120 |
|
System.out.println("\t " + secs + "s run time"); |
121 |
|
map.clear(); |
122 |
|
} |
130 |
|
int total; |
131 |
|
|
132 |
|
Runner(int id, Map<String,String> map, String[] key, CyclicBarrier barrier) { |
133 |
< |
this.map = map; |
134 |
< |
this.key = key; |
133 |
> |
this.map = map; |
134 |
> |
this.key = key; |
135 |
|
this.barrier = barrier; |
136 |
< |
position = key.length / 2; |
136 |
> |
position = key.length / 2; |
137 |
|
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
138 |
|
rng.next(); |
139 |
|
} |
140 |
|
|
141 |
|
int step() { |
142 |
< |
// random-walk around key positions, bunching accesses |
142 |
> |
// random-walk around key positions, bunching accesses |
143 |
|
int r = rng.next(); |
144 |
|
position += (r & 7) - 3; |
145 |
< |
while (position >= key.length) position -= key.length; |
145 |
> |
while (position >= key.length) position -= key.length; |
146 |
|
while (position < 0) position += key.length; |
147 |
|
|
148 |
|
String k = key[position]; |
158 |
|
} |
159 |
|
else if (r < insertsPerMaxRandom) { |
160 |
|
++position; |
161 |
+ |
// map.putIfAbsent(k, k); |
162 |
|
map.put(k, k); |
163 |
|
return 2; |
164 |
< |
} |
164 |
> |
} |
165 |
|
|
166 |
|
total += r; |
167 |
|
return 1; |
171 |
|
try { |
172 |
|
barrier.await(); |
173 |
|
int ops = nops; |
174 |
< |
while (ops > 0) |
174 |
> |
while (ops > 0) |
175 |
|
ops -= step(); |
176 |
|
barrier.await(); |
177 |
|
} |
180 |
|
} |
181 |
|
} |
182 |
|
} |
195 |
– |
} |
183 |
|
|
184 |
+ |
static final String wordFile = "testwords.txt"; |
185 |
+ |
|
186 |
+ |
// Read in String keys from file if possible |
187 |
+ |
static void initStringKeys(String[] keys, int n) throws Exception { |
188 |
+ |
FileInputStream fr = null; |
189 |
+ |
try { |
190 |
+ |
fr = new FileInputStream(wordFile); |
191 |
+ |
} catch (IOException ex) { |
192 |
+ |
System.out.println("No word file. Using String.valueOf(i)"); |
193 |
+ |
for (int i = 0; i < n; i++) |
194 |
+ |
keys[i] = String.valueOf(i); |
195 |
+ |
return; |
196 |
+ |
} |
197 |
+ |
|
198 |
+ |
BufferedInputStream in = new BufferedInputStream(fr); |
199 |
+ |
int k = 0; |
200 |
+ |
outer:while (k < n) { |
201 |
+ |
StringBuffer sb = new StringBuffer(); |
202 |
+ |
for (;;) { |
203 |
+ |
int c = in.read(); |
204 |
+ |
if (c < 0) |
205 |
+ |
break outer; |
206 |
+ |
char ch = (char) c; |
207 |
+ |
if (ch == '\n') { |
208 |
+ |
keys[k++] = sb.toString(); |
209 |
+ |
break; |
210 |
+ |
} |
211 |
+ |
if (!Character.isWhitespace(ch)) |
212 |
+ |
sb.append(ch); |
213 |
+ |
} |
214 |
+ |
} |
215 |
+ |
in.close(); |
216 |
+ |
|
217 |
+ |
// fill up remaining keys with path-like compounds of previous pairs |
218 |
+ |
int j = 0; |
219 |
+ |
while (k < n) |
220 |
+ |
keys[k++] = (String) keys[j++] + "/" + (String) keys[j]; |
221 |
+ |
} |
222 |
+ |
} |