001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOClientConfigHelper;
008: import com.tc.object.config.TransparencyClassSpec;
009: import com.tc.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tc.util.Assert;
012: import com.tc.util.concurrent.ThreadUtil;
013: import com.tctest.runner.AbstractTransparentApp;
014:
015: import java.util.HashMap;
016: import java.util.concurrent.CyclicBarrier;
017: import com.tc.util.runtime.Vm;
018:
019: public class HashMapBatchTxnTestApp extends AbstractTransparentApp {
020: int BATCHSIZE = (Vm.isIBM()) ? 400 : 1000;
021: int BATCHES = 80;
022:
023: private final CyclicBarrier barrier;
024: private final HashMap<Integer, HashMap> hashmap_root = new HashMap<Integer, HashMap>();
025:
026: public HashMapBatchTxnTestApp(String appId, ApplicationConfig cfg,
027: ListenerProvider listenerProvider) {
028: super (appId, cfg, listenerProvider);
029: barrier = new CyclicBarrier(getParticipantCount());
030: }
031:
032: public void run() {
033: try {
034: testHashMapBatchTxn();
035: } catch (Throwable t) {
036: notifyError(t);
037: }
038: }
039:
040: public static void visitL1DSOConfig(ConfigVisitor visitor,
041: DSOClientConfigHelper config) {
042:
043: String testClass = HashMapBatchTxnTestApp.class.getName();
044: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
045: config.addIncludePattern(testClass + "$*", false, false, true);
046:
047: String methodExpression = "* " + testClass + "*.*(..)";
048: config.addWriteAutolock(methodExpression);
049:
050: spec.addRoot("barrier", "barrier");
051: spec.addRoot("hashmap_root", "hashmap_root");
052: }
053:
054: private void testHashMapBatchTxn() throws Exception {
055: int index = barrier.await();
056:
057: for (int batch = 0; batch < BATCHES; batch += 2) {
058: if (index == 0) {
059: synchronized (hashmap_root) {
060: System.out.println("XXX Batching(client=0) "
061: + batch);
062: int id = BATCHSIZE * batch;
063: for (int i = 0; i < BATCHSIZE; ++i) {
064: HashMap<Integer, Integer> submap = generateNewHashMap(
065: id, 10 + (id % 10));
066: hashmap_root.put(new Integer(id), submap);
067: ++id;
068: }
069: }
070: }
071: if (index == 1) {
072: synchronized (hashmap_root) {
073: System.out.println("XXX Batching(client=1) "
074: + (batch + 1));
075: int id = BATCHSIZE * batch + BATCHSIZE;
076: for (int i = 0; i < BATCHSIZE; ++i) {
077: HashMap<Integer, Integer> submap = generateNewHashMap(
078: id, 10 + (id % 10));
079: hashmap_root.put(new Integer(id), submap);
080: ++id;
081: }
082: }
083: }
084: ThreadUtil.reallySleep(20);
085: }
086:
087: barrier.await();
088:
089: /* verification */
090: System.out.println("XXX starting verification");
091: for (int batch = 0; batch < BATCHES; ++batch) {
092: System.out.println("XXX verifying batch " + batch);
093: synchronized (hashmap_root) {
094: for (int i = 0; i < BATCHSIZE; ++i) {
095: HashMap<Integer, Integer> submap = hashmap_root
096: .get(new Integer(batch * BATCHSIZE + i));
097: Assert.assertTrue("Sub-HashMap("
098: + (batch * BATCHSIZE + i) + ") size is "
099: + submap.size() + " but expect "
100: + (10 + (i % 10)),
101: submap.size() == (10 + (i % 10)));
102: }
103: }
104: ThreadUtil.reallySleep(20);
105: }
106: System.out.println("XXX verification done");
107:
108: }
109:
110: HashMap generateNewHashMap(int startIndex, int size) {
111: HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
112:
113: for (int i = startIndex; i < (startIndex + size); ++i) {
114: map.put(new Integer(i), new Integer(i));
115: }
116:
117: return (map);
118: }
119:
120: }
|