KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ConcurrentHashMapSwapingTestApp


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.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.config.TransparencyClassSpec;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tc.util.Assert;
12 import com.tctest.runner.AbstractTransparentApp;
13
14 import java.util.concurrent.ConcurrentHashMap JavaDoc;
15 import java.util.concurrent.CyclicBarrier JavaDoc;
16
17 public class ConcurrentHashMapSwapingTestApp extends AbstractTransparentApp {
18   private static final int NUM_OF_PUT = 2000;
19   private static final int NUM_OF_LOOP = 5;
20   private static final int MAX_KEY_VALUE = 1000;
21
22   private final CyclicBarrier JavaDoc barrier;
23   private final ConcurrentHashMap JavaDoc mapRoot = new ConcurrentHashMap JavaDoc();
24
25   public ConcurrentHashMapSwapingTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
26     super(appId, cfg, listenerProvider);
27     barrier = new CyclicBarrier JavaDoc(getParticipantCount());
28   }
29
30   public void run() {
31     try {
32       int index = barrier.await();
33       testPutMany(index);
34     } catch (Throwable JavaDoc t) {
35       notifyError(t);
36     }
37   }
38
39   private void testPutMany(int index) throws Exception JavaDoc {
40     clearMap(index);
41
42     for (int j = 0; j < NUM_OF_LOOP; j++) {
43       if (index == 0) {
44         for (int i = 0; i < NUM_OF_PUT; i++) {
45           Assert.assertEquals((j == 0 && i < MAX_KEY_VALUE) ? i : MAX_KEY_VALUE, mapRoot.size());
46           int beforeSize = mapRoot.size();
47           int useVal = i % MAX_KEY_VALUE;
48           if(i % 100 == 0 ) System.err.println("Puting -- i: " + i + ", using key: " + useVal);
49           Object JavaDoc key = new HashKey(useVal);
50           while (System.identityHashCode(key) == key.hashCode()) {
51             key = new HashKey(useVal);
52           }
53           Assert.assertFalse(System.identityHashCode(key) == key.hashCode());
54           mapRoot.put(key, new HashValue(useVal));
55           int afterSize = mapRoot.size();
56           Assert.assertTrue(afterSize >= beforeSize);
57           if(i % 100 == 0 ) System.err.println("beforeSize: " + beforeSize + ", afterSize: " + afterSize);
58         }
59       }
60
61       barrier.await();
62     }
63
64     Assert.assertEquals(MAX_KEY_VALUE, mapRoot.size());
65
66     for (int i = 0; i < MAX_KEY_VALUE; i++) {
67       int useVal = i % MAX_KEY_VALUE;
68       if(i % 100 == 0 ) System.err.println("Getting key: " + useVal);
69       Object JavaDoc key = new HashKey(useVal);
70       while (System.identityHashCode(key) == key.hashCode()) {
71         key = new HashKey(useVal);
72       }
73       Assert.assertEquals(new HashValue(useVal), mapRoot.get(key));
74     }
75
76     barrier.await();
77   }
78
79   private void clearMap(int index) throws Exception JavaDoc {
80     if (index == 0) {
81       mapRoot.clear();
82     }
83
84     barrier.await();
85   }
86
87   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
88     String JavaDoc testClass = ConcurrentHashMapSwapingTestApp.class.getName();
89     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
90
91     config.addIncludePattern(testClass + "$*", false, false, true);
92
93     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
94     config.addWriteAutolock(methodExpression);
95
96     spec.addRoot("barrier", "barrier");
97     spec.addRoot("mapRoot", "mapRoot");
98   }
99
100   private static class HashKey {
101     private int i;
102
103     public HashKey(int i) {
104       super();
105       this.i = i;
106     }
107
108     public int getInt() {
109       return this.i;
110     }
111
112     public int hashCode() {
113       return i;
114     }
115
116     public boolean equals(Object JavaDoc obj) {
117       if (obj == null) return false;
118       if (!(obj instanceof HashKey)) return false;
119       return ((HashKey) obj).i == i;
120     }
121   }
122
123   private static class HashValue {
124     private int i;
125
126     public HashValue(int i) {
127       super();
128       this.i = i;
129     }
130
131     public int getInt() {
132       return this.i;
133     }
134
135     public int hashCode() {
136       return i;
137     }
138
139     public boolean equals(Object JavaDoc obj) {
140       if (obj == null) return false;
141       if (!(obj instanceof HashValue)) return false;
142       return ((HashValue) obj).i == i;
143     }
144
145     public String JavaDoc toString() {
146       return super.toString() + ", i: " + i;
147     }
148   }
149
150 }
151
Popular Tags