1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 7 8 import com.tc.exception.TCRuntimeException; 9 import com.tc.object.config.ConfigVisitor; 10 import com.tc.object.config.DSOClientConfigHelper; 11 import com.tc.object.config.TransparencyClassSpec; 12 import com.tc.object.config.spec.SynchronizedIntSpec; 13 import com.tc.simulator.app.ApplicationConfig; 14 import com.tc.simulator.listener.ListenerProvider; 15 import com.tc.util.Assert; 16 import com.tctest.runner.AbstractTransparentApp; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 24 public class TransparencySpeedTestApp extends AbstractTransparentApp { 25 public final static int MUTATOR_COUNT = 3; 26 public final static int ADD_COUNT = 10; public final static int VERIFIER_COUNT = 3; 28 29 private static Map myRoot = new HashMap (); 30 private long count; 31 private int commits = 0; 32 private SynchronizedInt gcount = new SynchronizedInt(0); 33 34 public TransparencySpeedTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 35 super(appId, cfg, listenerProvider); 36 } 37 38 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 39 config.getOrCreateSpec("com.tctest.TransparencySpeedTestApp$TestObj"); 40 TransparencyClassSpec spec = config.getOrCreateSpec("com.tctest.TransparencySpeedTestApp"); 41 spec.addRoot("myRoot", "rootBabyRoot"); 42 spec.addRoot("gcount", "globalCount"); 43 44 String methodExpression = "long com.tctest.TransparencySpeedTestApp.test4(int, java.lang.Object)"; 45 config.addWriteAutolock(methodExpression); 46 methodExpression = "long com.tctest.TransparencySpeedTestApp.test5(int, java.lang.Object)"; 47 config.addWriteAutolock(methodExpression); 48 methodExpression = "void com.tctest.TransparencySpeedTestApp.notifyDone()"; 49 config.addWriteAutolock(methodExpression); 50 51 spec = config.getOrCreateSpec("com.tctest.TransparencySpeedTestVerifier"); 52 53 spec.addRoot("resultRoot", "rootBabyRoot"); 54 55 methodExpression = "boolean com.tctest.TransparencySpeedTestVerifier.verify()"; 56 57 config.addWriteAutolock(methodExpression); 58 new SynchronizedIntSpec().visit(visitor, config); 59 64 } 65 66 public void run() { 67 68 int myId = gcount.increment(); 69 if(myId > MUTATOR_COUNT) { 70 verify(); 71 } else { 72 mutate(myId); 73 } 74 } 75 76 private void verify() { 77 try { 78 new TransparencySpeedTestVerifier().verify(); 79 } catch (Exception e) { 80 throw new TCRuntimeException(e); 81 } 82 } 83 84 public void mutate(int myId) { 85 this.count = (myId - 1) * ADD_COUNT; 86 System.err.println("AppId is :" + getApplicationId() + " and count = " + count); 87 boolean remove = false; 88 long start = System.currentTimeMillis(); 89 long totalInTXTime = 0; 90 for (int i = 0; i < ADD_COUNT; i++) { 91 totalInTXTime += test4(i, new Object ()); 92 if (false) totalInTXTime += test5(i, new Object ()); 93 remove = !remove; 94 } 95 long seconds = (System.currentTimeMillis() - start) / 1000; 96 String commitsPerSecond = "DIVIDE BY ZERO!"; 97 if (seconds != 0) { 98 commitsPerSecond = "" + (commits / seconds); 99 } 100 System.out.println("****Commits:" + commits + " seconds:" + seconds + " commits/second: " + commitsPerSecond 101 + " Total in tx:" + totalInTXTime); 102 notifyDone(); 103 } 104 105 private void notifyDone() { 106 synchronized (myRoot) { 107 myRoot.notifyAll(); 109 } 110 } 111 112 public long test4(int i, Object foo) { 113 synchronized (myRoot) { 114 long start = System.currentTimeMillis(); 115 commits++; 116 int s = myRoot.size(); 117 long c = count++; 118 if (myRoot.containsKey(new Long (c))) { 119 Assert.eval(false); 120 } 121 myRoot.put(new Long (c), new TestObj(new TestObj(null))); 122 if (myRoot.size() != s + 1) System.out.println("Wrong size!:" + s + " new size:" + myRoot.size()); 123 Assert.eval(myRoot.size() == s + 1); 124 return System.currentTimeMillis() - start; 126 } 127 } 128 129 public long test5(int i, Object foo) { 130 synchronized (myRoot) { 131 long start = System.currentTimeMillis(); 132 commits++; 133 int s = myRoot.size(); 134 myRoot.remove(new Long (count - 1)); 135 if (myRoot.size() != s - 1) System.out.println("Wrong size!:" + s + " new size:" + myRoot.size()); 136 Assert.eval(myRoot.size() == s - 1); 137 return System.currentTimeMillis() - start; 139 } 140 } 141 142 public static class TestObj { 143 private TestObj obj; 144 private String string = "Steve"; 145 private int integer = 22; 146 private boolean bool = false; 147 private Map map = new HashMap (); 148 149 private TestObj() { 150 } 152 153 public TestObj(TestObj obj) { 154 this.obj = obj; 155 for (int i = 0; i < 30; i++) { 156 map.put(new Long (i), new TestObj()); 157 } 158 } 159 160 public Object getObject() { 161 return this.obj; 162 } 163 164 public boolean check() { 165 return string.equals("Steve") && integer == 22 && bool == false; 166 } 167 } 168 } | Popular Tags |