1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.BrokenBarrierException; 7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 8 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 9 10 import com.tc.object.config.ConfigVisitor; 11 import com.tc.object.config.DSOClientConfigHelper; 12 import com.tc.object.config.TransparencyClassSpec; 13 import com.tc.object.config.spec.CyclicBarrierSpec; 14 import com.tc.object.config.spec.SynchronizedIntSpec; 15 import com.tc.simulator.app.ApplicationConfig; 16 import com.tc.simulator.listener.ListenerProvider; 17 import com.tc.util.Assert; 18 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 19 20 import java.text.DateFormat ; 21 import java.text.SimpleDateFormat ; 22 import java.util.ArrayList ; 23 import java.util.Date ; 24 import java.util.HashMap ; 25 import java.util.Hashtable ; 26 import java.util.IdentityHashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedHashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.TreeMap ; 32 import java.util.Map.Entry; 33 34 public class MapOfMapsTestApp extends AbstractErrorCatchingTransparentApp { 35 36 private static final int LOOP_COUNT = 10; 37 private static final int DEPTH_COUNT = 5; 38 private static final int BREATH_COUNT = 3; 39 40 final Map root = new HashMap (); 41 final SynchronizedInt uid = new SynchronizedInt(0); 42 CyclicBarrier barrier; 43 44 public MapOfMapsTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 45 super(appId, cfg, listenerProvider); 46 } 47 48 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 49 String testClass = MapOfMapsTestApp.class.getName(); 50 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 51 spec.addRoot("root", "root"); 52 spec.addRoot("uid", "uid"); 53 spec.addRoot("barrier", "barrier"); 54 String methodExpression = "* " + testClass + ".read(..)"; 55 config.addReadAutolock(methodExpression); 56 methodExpression = "* " + testClass + ".add2Root(..)"; 57 config.addWriteAutolock(methodExpression); 58 methodExpression = "* " + testClass + ".populateMyRoot(..)"; 59 config.addWriteAutolock(methodExpression); 60 new SynchronizedIntSpec().visit(visitor, config); 61 new CyclicBarrierSpec().visit(visitor, config); 62 } 63 64 public void runTest() throws BrokenBarrierException, InterruptedException { 65 setCyclicBarrier(); 66 int myid = uid.increment(); 67 if (myid == 1) { 68 runCreateMaps(); 70 } else { 71 runReadMaps(); 73 } 74 } 75 76 private void runReadMaps() throws BrokenBarrierException, InterruptedException { 77 int count = 0; 78 int mapCountNo = calculateMapCount(DEPTH_COUNT, BREATH_COUNT); 79 while (count++ < LOOP_COUNT) { 80 barrier.barrier(); 81 log("Readers : Loop Count : " + count); 82 Map myRoot = read(String.valueOf(count), root); 83 int mapCount = countMaps(myRoot); 84 log("Readers : No Of Maps = " + mapCount); 85 Assert.assertEquals(mapCountNo, mapCount); 86 } 87 } 88 89 private int calculateMapCount(int depth, int breath) { 90 int pow = breath; 91 int count = 0; 92 for (int i = 0; i <= depth; i++) { 93 count += pow; 94 pow *= breath; 95 } 96 return count; 97 } 98 99 private int countMaps(Map m) { 100 101 int count = 0; 102 for (Iterator i = m.entrySet().iterator(); i.hasNext();) { 103 Map.Entry e = (Entry) i.next(); 104 if (e.getValue() instanceof Map ) { 105 count = count + 1 + countMaps((Map ) e.getValue()); 106 } 107 } 108 return count; 109 } 110 111 private void runCreateMaps() throws BrokenBarrierException, InterruptedException { 112 int count = 0; 113 while (count++ < LOOP_COUNT) { 114 log("Writer : Loop Count : " + count); 115 Map myRoot = new HashMap (); 116 add2Root(String.valueOf(count), myRoot); 117 populateMyRoot(myRoot, DEPTH_COUNT, BREATH_COUNT); 118 barrier.barrier(); 119 } 120 } 121 122 private void populateMyRoot(Map myRoot, int depth, int breath) { 123 List childs = new ArrayList (); 124 int b = breath; 125 synchronized (myRoot) { 126 while (b-- > 0) { 127 childs.add(addToMap(String.valueOf(b), myRoot)); 128 } 129 } 130 if (depth-- > 0) { 131 for (Iterator i = childs.iterator(); i.hasNext();) { 132 Map child = (Map ) i.next(); 133 populateMyRoot(child, depth, breath); 134 } 135 } 136 } 137 138 private void setCyclicBarrier() { 139 int participationCount = getParticipantCount(); 140 log("Participation Count = " + participationCount); 141 barrier = new CyclicBarrier(participationCount); 142 } 143 144 private void add2Root(String id, Map myRoot) { 145 synchronized (root) { 146 root.put(id, myRoot); 147 } 148 } 149 150 static DateFormat formatter = new SimpleDateFormat ("hh:mm:ss,S"); 151 152 private static void log(String message) { 153 System.err.println(Thread.currentThread().getName() + " :: " 154 + formatter.format(new Date (System.currentTimeMillis())) + " : " + message); 155 } 156 157 private Map addToMap(String id, Map m) { 158 Map child = getChild(m); 159 m.put(id, child); 160 return child; 161 } 162 163 167 private Map getChild(Map m) { 168 if (m instanceof HashMap ) { 169 return getPopulatedMap(new Hashtable ()); 170 } else if (m instanceof Hashtable ) { 171 return getPopulatedMap(new LinkedHashMap ()); 172 } else if (m instanceof LinkedHashMap ) { 173 return getPopulatedMap(new IdentityHashMap ()); 174 } else if (m instanceof IdentityHashMap ) { 175 return getPopulatedMap(new TreeMap ()); 176 } else if (m instanceof TreeMap ) { 177 return getPopulatedMap(new HashMap ()); 178 } else { 179 throw new AssertionError ("Should never get here"); 180 } 181 } 182 183 private Map getPopulatedMap(Map m) { 184 for (int i = 0; i < 10; i++) { 185 m.put("Hello - " + i, new String ("Hehehehehehehehehehehehehehehehehheheheheheheheheheheheheheheheheh-" + i)); 186 } 187 return m; 188 } 189 190 private Map read(String id, Map m) { 191 synchronized (m) { 192 return (Map ) m.get(id); 193 } 194 } 195 196 } 197 | Popular Tags |