KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ConcurrentHashMapLoadTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import com.tc.object.bytecode.Manageable;
7 import com.tc.object.bytecode.ManagerUtil;
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.TransparencyClassSpec;
11 import com.tc.simulator.app.ApplicationConfig;
12 import com.tc.simulator.listener.ListenerProvider;
13 import com.tc.util.Assert;
14 import com.tc.util.DebugUtil;
15 import com.tctest.runner.AbstractTransparentApp;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap JavaDoc;
23 import java.util.concurrent.CyclicBarrier JavaDoc;
24
25 public class ConcurrentHashMapLoadTestApp extends AbstractTransparentApp {
26   private static final int NUM_OF_PUT = 1000;
27
28   private final DataKey[] keyRoots = new DataKey[] { new DataKey(1), new DataKey(2), new DataKey(3),
29       new DataKey(4) };
30   private final DataValue[] valueRoots = new DataValue[] { new DataValue(10), new DataValue(20),
31       new DataValue(30), new DataValue(40) };
32
33   private final CyclicBarrier JavaDoc barrier;
34   private final ConcurrentHashMap JavaDoc mapRoot = new ConcurrentHashMap JavaDoc();
35   private final SharedObject sharedRoot = new SharedObject();
36
37   public ConcurrentHashMapLoadTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
38     super(appId, cfg, listenerProvider);
39     barrier = new CyclicBarrier JavaDoc(getParticipantCount());
40   }
41
42   public void run() {
43     try {
44       int index = barrier.await();
45
46       testUnsharedToShared1(index);
47       testUnsharedToShared2(index);
48       testUnsharedToShared3(index);
49       testUnsharedToShared4(index);
50
51       testContainsKey1(index);
52       testContainsKey2(index);
53       testGet(index);
54       testRemove(index);
55       testReplace(index);
56
57       testPutMany(index);
58       testPutAndRemoveMany(index);
59     } catch (Throwable JavaDoc t) {
60       notifyError(t);
61     }
62   }
63
64   private void testUnsharedToShared1(int index) throws Exception JavaDoc {
65     if (index == 0) {
66       ConcurrentHashMap JavaDoc newMap = new ConcurrentHashMap JavaDoc();
67       newMap.put(keyRoots[0], valueRoots[0]);
68       newMap.put(keyRoots[1], valueRoots[1]);
69       newMap.put(keyRoots[2], valueRoots[2]);
70       newMap.put(keyRoots[3], valueRoots[3]);
71
72       synchronized (sharedRoot) {
73         sharedRoot.setMap(newMap);
74       }
75     }
76
77     barrier.await();
78
79     Map JavaDoc newMap = new HashMap JavaDoc();
80     newMap.put(keyRoots[0], valueRoots[0]);
81     newMap.put(keyRoots[1], valueRoots[1]);
82     newMap.put(keyRoots[2], valueRoots[2]);
83     newMap.put(keyRoots[3], valueRoots[3]);
84
85     Map JavaDoc sharedMap = sharedRoot.getMap();
86     assertMappingsEqual(newMap, sharedMap);
87
88     barrier.await();
89   }
90
91   private void testUnsharedToShared2(int index) throws Exception JavaDoc {
92     if (index == 0) {
93       ConcurrentHashMap JavaDoc newMap = new ConcurrentHashMap JavaDoc();
94       newMap.put(keyRoots[0], valueRoots[0]);
95       newMap.put(keyRoots[1], valueRoots[1]);
96       newMap.put(keyRoots[2], valueRoots[2]);
97
98       mapRoot.put("newMap", newMap);
99       mapRoot.put(keyRoots[0], keyRoots[1]);
100     }
101
102     barrier.await();
103
104     Map JavaDoc newMap = new HashMap JavaDoc();
105     newMap.put(keyRoots[0], valueRoots[0]);
106     newMap.put(keyRoots[1], valueRoots[1]);
107     newMap.put(keyRoots[2], valueRoots[2]);
108
109     Map JavaDoc sharedMap = (Map JavaDoc) mapRoot.get("newMap");
110     assertMappingsEqual(newMap, sharedMap);
111
112     barrier.await();
113
114     if (index == 1) {
115       Map JavaDoc m = (Map JavaDoc) mapRoot.get("newMap");
116       m.put(keyRoots[3], valueRoots[3]);
117     }
118
119     barrier.await();
120
121     newMap.put(keyRoots[3], valueRoots[3]);
122
123     sharedMap = (Map JavaDoc) mapRoot.get("newMap");
124     assertMappingsEqual(newMap, sharedMap);
125
126     barrier.await();
127   }
128
129   private void testUnsharedToShared3(int index) throws Exception JavaDoc {
130     if (index == 0) {
131       ConcurrentHashMap JavaDoc newMap = new ConcurrentHashMap JavaDoc();
132       DataKey key1 = new DataKey(1);
133       DataKey key2 = new DataKey(2);
134       DataValue val1 = new DataValue(10);
135       DataValue val2 = new DataValue(20);
136       newMap.put(key1, val1);
137       newMap.put(key2, val2);
138
139       Assert.assertNull(((Manageable) key1).__tc_managed());
140
141       mapRoot.put("newMap", newMap);
142
143       Assert.assertNotNull(((Manageable) key1).__tc_managed());
144     }
145
146     barrier.await();
147   }
148
149   private void testUnsharedToShared4(int index) throws Exception JavaDoc {
150     clearMapRoot(index);
151     if (index == 0) {
152       ConcurrentHashMap JavaDoc newMap = new ConcurrentHashMap JavaDoc();
153       HashKey key1 = new HashKey(1);
154       HashKey key2 = new HashKey(2);
155       HashValue val1 = new HashValue(10);
156       HashValue val2 = new HashValue(20);
157       newMap.put(key1, val1);
158       newMap.put(key2, val2);
159
160       mapRoot.put(newMap, "newMap");
161     }
162
163     barrier.await();
164
165     Assert.assertEquals(1, mapRoot.size());
166
167     Set JavaDoc keys = mapRoot.keySet();
168     Iterator JavaDoc keyIterator = keys.iterator();
169     Map JavaDoc map = (Map JavaDoc) keyIterator.next();
170     map.containsKey(new HashKey(1));
171     map.containsKey(new HashKey(2));
172     map.containsValue(new HashValue(10));
173     map.containsValue(new HashValue(20));
174
175     Object JavaDoc o = mapRoot.get(map);
176     Assert.assertEquals("newMap", o);
177
178     barrier.await();
179
180     if (index == 1) {
181       map.put(new HashKey(3), new HashValue(30));
182     }
183
184     barrier.await();
185
186     Assert.assertEquals(new HashValue(30), map.get(new HashKey(3)));
187
188     barrier.await();
189   }
190
191   private void testContainsKey1(int index) throws Exception JavaDoc {
192     if (index == 0) {
193       DataKey key1 = new DataKey(1);
194       DataKey key2 = new DataKey(1);
195
196       DataValue val1 = new DataValue(10);
197       DataValue val2 = new DataValue(10);
198
199       Assert.assertNull(((Manageable) key1).__tc_managed());
200       Assert.assertNull(((Manageable) key2).__tc_managed());
201       Assert.assertNull(((Manageable) val1).__tc_managed());
202       Assert.assertNull(((Manageable) val2).__tc_managed());
203
204       mapRoot.put(key1, val1);
205       Assert.assertNotNull(((Manageable) key1).__tc_managed());
206       Assert.assertNotNull(((Manageable) val1).__tc_managed());
207
208       Assert.assertTrue(mapRoot.containsKey(key1));
209       Assert.assertFalse(mapRoot.containsKey(key2));
210
211       Assert.assertNull(((Manageable) key2).__tc_managed());
212
213       Assert.assertTrue(mapRoot.contains(val1));
214       Assert.assertFalse(mapRoot.contains(val2));
215
216       Assert.assertNull(((Manageable) val2).__tc_managed());
217     }
218
219     barrier.await();
220   }
221
222   private void testContainsKey2(int index) throws Exception JavaDoc {
223     if (index == 0) {
224       HashKey key1 = new HashKey(1);
225       HashKey key2 = new HashKey(1);
226
227       HashValue val1 = new HashValue(10);
228       HashValue val2 = new HashValue(10);
229
230       Assert.assertNull(((Manageable) key1).__tc_managed());
231       Assert.assertNull(((Manageable) key2).__tc_managed());
232       Assert.assertNull(((Manageable) val1).__tc_managed());
233       Assert.assertNull(((Manageable) val2).__tc_managed());
234
235       mapRoot.put(key1, val1);
236
237       Assert.assertNotNull(((Manageable) key1).__tc_managed());
238       Assert.assertNotNull(((Manageable) val1).__tc_managed());
239
240       Assert.assertTrue(mapRoot.containsKey(key1));
241       Assert.assertTrue(mapRoot.containsKey(key2));
242
243       Assert.assertNull(((Manageable) key2).__tc_managed());
244
245       Assert.assertTrue(mapRoot.contains(val1));
246       Assert.assertTrue(mapRoot.contains(val2));
247
248       Assert.assertNull(((Manageable) val2).__tc_managed());
249     }
250
251     barrier.await();
252   }
253
254   private void testGet(int index) throws Exception JavaDoc {
255     if (index == 0) {
256       DataKey key1 = new DataKey(1);
257       DataKey key2 = new DataKey(1);
258
259       DataValue val1 = new DataValue(10);
260       DataValue val2 = new DataValue(10);
261
262       Assert.assertNull(((Manageable) key1).__tc_managed());
263       Assert.assertNull(((Manageable) key2).__tc_managed());
264       Assert.assertNull(((Manageable) val1).__tc_managed());
265       Assert.assertNull(((Manageable) val2).__tc_managed());
266
267       mapRoot.put(key1, val1);
268
269       Assert.assertNotNull(((Manageable) key1).__tc_managed());
270       Assert.assertNotNull(((Manageable) val1).__tc_managed());
271
272       Assert.assertNotNull(mapRoot.get(key1));
273       Assert.assertNull(mapRoot.get(key2));
274
275       Assert.assertNull(((Manageable) key2).__tc_managed());
276     }
277
278     barrier.await();
279   }
280
281   private void testRemove(int index) throws Exception JavaDoc {
282     if (index == 0) {
283       DataKey key1 = new DataKey(1);
284       DataKey key2 = new DataKey(1);
285
286       DataValue val1 = new DataValue(10);
287       DataValue val2 = new DataValue(10);
288
289       Assert.assertNull(((Manageable) key1).__tc_managed());
290       Assert.assertNull(((Manageable) key2).__tc_managed());
291       Assert.assertNull(((Manageable) val1).__tc_managed());
292       Assert.assertNull(((Manageable) val2).__tc_managed());
293
294       mapRoot.put(key1, val1);
295
296       Assert.assertNotNull(((Manageable) key1).__tc_managed());
297       Assert.assertNotNull(((Manageable) val1).__tc_managed());
298
299       Assert.assertNull(mapRoot.remove(key2));
300
301       Assert.assertNull(((Manageable) key2).__tc_managed());
302     }
303
304     barrier.await();
305   }
306
307   private void testReplace(int index) throws Exception JavaDoc {
308     if (index == 0) {
309       DataKey key1 = new DataKey(1);
310       DataKey key2 = new DataKey(1);
311
312       DataValue val1 = new DataValue(10);
313       DataValue val2 = new DataValue(10);
314
315       Assert.assertNull(((Manageable) key1).__tc_managed());
316       Assert.assertNull(((Manageable) key2).__tc_managed());
317       Assert.assertNull(((Manageable) val1).__tc_managed());
318       Assert.assertNull(((Manageable) val2).__tc_managed());
319
320       mapRoot.put(key1, val1);
321
322       Assert.assertNotNull(((Manageable) key1).__tc_managed());
323       Assert.assertNotNull(((Manageable) val1).__tc_managed());
324
325       Assert.assertNull(mapRoot.replace(key2, val2));
326
327       Assert.assertNull(((Manageable) key2).__tc_managed());
328     }
329
330     barrier.await();
331   }
332
333   private void testPutMany(int index) throws Exception JavaDoc {
334     if (index == 0) {
335       for (int i = 0; i < NUM_OF_PUT; i++) {
336         mapRoot.put(new HashKey(i), new HashValue(i));
337       }
338     }
339
340     barrier.await();
341     
342     if (DebugUtil.DEBUG) {
343       System.err.println("Index: " + index + ", map size: " + mapRoot.size());
344       System.err.println("Index: " + index + ", map: " + mapRoot);
345     }
346
347     for (int i = 0; i < NUM_OF_PUT; i++) {
348       Assert.assertEquals(new HashValue(i), mapRoot.get(new HashKey(i)));
349     }
350     
351     if (DebugUtil.DEBUG) {
352       System.err.println("Index: " + index + ", map size: " + mapRoot.size());
353       System.err.println("Index: " + index + " exiting last barrier in testPutMany.");
354     }
355
356     barrier.await();
357   }
358
359   private void testPutAndRemoveMany(int index) throws Exception JavaDoc {
360     DebugUtil.DEBUG = true;
361     clearMapRoot(index);
362
363     if (index == 0) {
364       for (int i = 0; i < NUM_OF_PUT; i++) {
365         System.out.println("Put: " + i);
366         mapRoot.put(new HashKey(i), new HashValue(i));
367       }
368     } else if (index == 1) {
369       for (int i = 0; i < NUM_OF_PUT; i++) {
370         Object JavaDoc o = null;
371         while (o == null) {
372           o = mapRoot.remove(new HashKey(i));
373         }
374           
375         Assert.assertEquals(o, new HashValue(i));
376         System.out.println("Remove: " + i);
377       }
378     }
379
380     barrier.await();
381
382     if (DebugUtil.DEBUG) {
383       System.err.println("Node id: " + ManagerUtil.getClientID() + " -- " + index + ", size of concurrentHashMap: " + mapRoot.size());
384     }
385     Assert.assertTrue(mapRoot.isEmpty());
386
387     barrier.await();
388     
389     DebugUtil.DEBUG = false;
390   }
391
392   private void clearMapRoot(int index) throws Exception JavaDoc {
393     if (index == 0) {
394       System.err.println("In clearMapRoot");
395       
396       mapRoot.clear();
397     }
398     barrier.await();
399   }
400
401   void assertMappingsEqual(Map JavaDoc expect, Map JavaDoc actual) {
402     Assert.assertEquals(expect.size(), actual.size());
403
404     Set JavaDoc expectEntries = expect.entrySet();
405     Set JavaDoc actualEntries = actual.entrySet();
406
407     for (Iterator JavaDoc i = expectEntries.iterator(); i.hasNext();) {
408       Entry entry = (Entry) i.next();
409       Assert.assertEquals(((DataValue) entry.getValue()).getInt(), ((DataValue) actual.get(entry.getKey())).getInt());
410     }
411
412     for (Iterator JavaDoc i = actualEntries.iterator(); i.hasNext();) {
413       Entry entry = (Entry) i.next();
414       Assert.assertEquals(((DataValue) entry.getValue()).getInt(), ((DataValue) expect.get(entry.getKey())).getInt());
415     }
416   }
417
418   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
419     String JavaDoc testClass = ConcurrentHashMapLoadTestApp.class.getName();
420     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
421
422     config.addIncludePattern(testClass + "$*", false, false, true);
423
424     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
425     config.addWriteAutolock(methodExpression);
426
427     spec.addRoot("barrier", "barrier");
428     spec.addRoot("mapRoot", "mapRoot");
429     spec.addRoot("sharedRoot", "sharedRoot");
430     spec.addRoot("keyRoots", "keyRoots");
431     spec.addRoot("valueRoots", "valueRoots");
432   }
433
434   private static class DataKey {
435     private int i;
436
437     public DataKey(int i) {
438       super();
439       this.i = i;
440     }
441
442     public int getInt() {
443       return this.i;
444     }
445     
446     public String JavaDoc toString() {
447       return super.toString() + ", i: " + i;
448     }
449   }
450
451   private static class DataValue {
452     private int i;
453
454     public DataValue(int i) {
455       super();
456       this.i = i;
457     }
458
459     public int getInt() {
460       return this.i;
461     }
462     
463     public String JavaDoc toString() {
464       return super.toString() + ", i: " + i;
465     }
466   }
467
468   private static class HashKey {
469     private int i;
470
471     public HashKey(int i) {
472       super();
473       this.i = i;
474     }
475
476     public int getInt() {
477       return this.i;
478     }
479
480     public int hashCode() {
481       return i;
482     }
483
484     public boolean equals(Object JavaDoc obj) {
485       if (obj == null) return false;
486       if (!(obj instanceof HashKey)) return false;
487       return ((HashKey) obj).i == i;
488     }
489     
490     public String JavaDoc toString() {
491       return super.toString() + ", i: " + i;
492     }
493   }
494
495   private static class HashValue {
496     private int i;
497
498     public HashValue(int i) {
499       super();
500       this.i = i;
501     }
502
503     public int getInt() {
504       return this.i;
505     }
506
507     public int hashCode() {
508       return i;
509     }
510
511     public boolean equals(Object JavaDoc obj) {
512       if (obj == null) return false;
513       if (!(obj instanceof HashValue)) return false;
514       return ((HashValue) obj).i == i;
515     }
516
517     public String JavaDoc toString() {
518       return super.toString() + ", i: " + i;
519     }
520   }
521
522   private static class SharedObject {
523     private ConcurrentHashMap JavaDoc map;
524
525     public SharedObject() {
526       super();
527     }
528
529     public ConcurrentHashMap JavaDoc getMap() {
530       return map;
531     }
532
533     public void setMap(ConcurrentHashMap JavaDoc map) {
534       this.map = map;
535     }
536
537   }
538
539 }
540
Popular Tags