001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.listener.ListenerProvider;
012: import com.tc.util.Assert;
013: import com.tctest.runner.AbstractTransparentApp;
014:
015: import java.util.concurrent.ConcurrentHashMap;
016: import java.util.concurrent.CyclicBarrier;
017:
018: public class ConcurrentHashMapSwapingTestApp extends
019: AbstractTransparentApp {
020: public static final String GC_TEST_KEY = "gc-test";
021:
022: private static final int DEFAULT_NUM_OF_PUT = 2000;
023: private static final int DEFAULT_NUM_OF_LOOP = 5;
024: private static final int GC_NUM_OF_PUT = 1000;
025: private static final int GC_NUM_OF_LOOP = 2;
026: private static final int GC_CREATE_NUM = 5;
027: private static final int MAX_KEY_VALUE = 1000;
028:
029: private final CyclicBarrier barrier;
030: private final ConcurrentHashMap mapRoot = new ConcurrentHashMap();
031: private boolean isGcTest = false;
032: private final int gcCreateNum;
033: private final int numOfPut;
034: private final int numOfLoop;
035:
036: public ConcurrentHashMapSwapingTestApp(String appId,
037: ApplicationConfig cfg, ListenerProvider listenerProvider) {
038: super (appId, cfg, listenerProvider);
039: barrier = new CyclicBarrier(getParticipantCount());
040:
041: Boolean booleanObject = Boolean.valueOf(cfg
042: .getAttribute(GC_TEST_KEY));
043: isGcTest = booleanObject.booleanValue();
044:
045: if (isGcTest) {
046: gcCreateNum = GC_CREATE_NUM;
047: numOfPut = GC_NUM_OF_PUT;
048: numOfLoop = GC_NUM_OF_LOOP;
049: } else {
050: gcCreateNum = 1;
051: numOfPut = DEFAULT_NUM_OF_PUT;
052: numOfLoop = DEFAULT_NUM_OF_LOOP;
053: }
054:
055: System.err.println("***** setting isGcTest=[" + isGcTest
056: + "] gcCreateNum=[" + gcCreateNum + "] numOfPut=["
057: + numOfPut + "] numOfLoop=[" + numOfLoop + "]");
058: }
059:
060: public void run() {
061: try {
062: int index = barrier.await();
063: for (int i = 0; i < gcCreateNum; i++) {
064: testPutMany(index);
065: }
066: } catch (Throwable t) {
067: notifyError(t);
068: }
069: }
070:
071: private void testPutMany(int index) throws Exception {
072: clearMap(index);
073:
074: for (int j = 0; j < numOfLoop; j++) {
075: if (index == 0) {
076: for (int i = 0; i < numOfPut; i++) {
077: Assert.assertEquals(
078: (j == 0 && i < MAX_KEY_VALUE) ? i
079: : MAX_KEY_VALUE, mapRoot.size());
080: int beforeSize = mapRoot.size();
081: int useVal = i % MAX_KEY_VALUE;
082: if (i % 100 == 0)
083: System.err.println("Puting -- i: " + i
084: + ", using key: " + useVal);
085: Object key = new HashKey(useVal);
086: while (System.identityHashCode(key) == key
087: .hashCode()) {
088: key = new HashKey(useVal);
089: }
090: Assert
091: .assertFalse(System.identityHashCode(key) == key
092: .hashCode());
093: mapRoot.put(key, new HashValue(useVal));
094: int afterSize = mapRoot.size();
095: Assert.assertTrue(afterSize >= beforeSize);
096: if (i % 100 == 0)
097: System.err.println("beforeSize: " + beforeSize
098: + ", afterSize: " + afterSize);
099: }
100: }
101:
102: barrier.await();
103: }
104:
105: Assert.assertEquals(MAX_KEY_VALUE, mapRoot.size());
106:
107: for (int i = 0; i < MAX_KEY_VALUE; i++) {
108: int useVal = i % MAX_KEY_VALUE;
109: if (i % 100 == 0)
110: System.err.println("Getting key: " + useVal);
111: Object key = new HashKey(useVal);
112: if (key.hashCode() == System.identityHashCode(key)) {
113: String assertionMsg = "Getting key: " + useVal
114: + ", hashCode: " + key.hashCode()
115: + ", identityHashCode: "
116: + System.identityHashCode(key);
117: throw new AssertionError(assertionMsg);
118: }
119: Assert
120: .assertEquals(new HashValue(useVal), mapRoot
121: .get(key));
122: }
123:
124: barrier.await();
125: }
126:
127: private void clearMap(int index) throws Exception {
128: if (index == 0) {
129: mapRoot.clear();
130: }
131:
132: barrier.await();
133: }
134:
135: public static void visitL1DSOConfig(ConfigVisitor visitor,
136: DSOClientConfigHelper config) {
137: String testClass = ConcurrentHashMapSwapingTestApp.class
138: .getName();
139: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
140:
141: config.addIncludePattern(testClass + "$*", false, false, true);
142:
143: String methodExpression = "* " + testClass + "*.*(..)";
144: config.addWriteAutolock(methodExpression);
145:
146: spec.addRoot("barrier", "barrier");
147: spec.addRoot("mapRoot", "mapRoot");
148: }
149:
150: private static class HashKey {
151: private int i;
152:
153: public HashKey(int i) {
154: super ();
155: this .i = i;
156: }
157:
158: public int getInt() {
159: return this .i;
160: }
161:
162: public int hashCode() {
163: return i;
164: }
165:
166: public boolean equals(Object obj) {
167: if (obj == null)
168: return false;
169: if (!(obj instanceof HashKey))
170: return false;
171: return ((HashKey) obj).i == i;
172: }
173: }
174:
175: private static class HashValue {
176: private int i;
177:
178: public HashValue(int i) {
179: super ();
180: this .i = i;
181: }
182:
183: public int getInt() {
184: return this .i;
185: }
186:
187: public int hashCode() {
188: return i;
189: }
190:
191: public boolean equals(Object obj) {
192: if (obj == null)
193: return false;
194: if (!(obj instanceof HashValue))
195: return false;
196: return ((HashValue) obj).i == i;
197: }
198:
199: public String toString() {
200: return super .toString() + ", i: " + i;
201: }
202: }
203:
204: }
|