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 EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
008:
009: import com.tc.exception.TCRuntimeException;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.TransparencyClassSpec;
013: import com.tc.object.config.spec.SynchronizedIntSpec;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tc.util.Assert;
017: import com.tctest.runner.AbstractTransparentApp;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: /**
023: * @author steve
024: */
025: public class TransparencySpeedTestApp extends AbstractTransparentApp {
026: public final static int MUTATOR_COUNT = 3;
027: public final static int ADD_COUNT = 10; // must be divisible by 2
028: public final static int VERIFIER_COUNT = 3;
029:
030: private static Map myRoot = new HashMap();
031: private long count;
032: private int commits = 0;
033: private SynchronizedInt gcount = new SynchronizedInt(0);
034:
035: public TransparencySpeedTestApp(String appId,
036: ApplicationConfig cfg, ListenerProvider listenerProvider) {
037: super (appId, cfg, listenerProvider);
038: }
039:
040: public static void visitL1DSOConfig(ConfigVisitor visitor,
041: DSOClientConfigHelper config) {
042: config
043: .getOrCreateSpec("com.tctest.TransparencySpeedTestApp$TestObj");
044: TransparencyClassSpec spec = config
045: .getOrCreateSpec("com.tctest.TransparencySpeedTestApp");
046: spec.addRoot("myRoot", "rootBabyRoot");
047: spec.addRoot("gcount", "globalCount");
048:
049: String methodExpression = "long com.tctest.TransparencySpeedTestApp.test4(int, java.lang.Object)";
050: config.addWriteAutolock(methodExpression);
051: methodExpression = "long com.tctest.TransparencySpeedTestApp.test5(int, java.lang.Object)";
052: config.addWriteAutolock(methodExpression);
053: methodExpression = "void com.tctest.TransparencySpeedTestApp.notifyDone()";
054: config.addWriteAutolock(methodExpression);
055:
056: spec = config
057: .getOrCreateSpec("com.tctest.TransparencySpeedTestVerifier");
058:
059: spec.addRoot("resultRoot", "rootBabyRoot");
060:
061: methodExpression = "boolean com.tctest.TransparencySpeedTestVerifier.verify()";
062:
063: config.addWriteAutolock(methodExpression);
064: new SynchronizedIntSpec().visit(visitor, config);
065: }
066:
067: public void run() {
068:
069: int myId = gcount.increment();
070: if (myId > MUTATOR_COUNT) {
071: verify();
072: } else {
073: mutate(myId);
074: }
075: }
076:
077: private void verify() {
078: try {
079: new TransparencySpeedTestVerifier().verify();
080: } catch (Exception e) {
081: throw new TCRuntimeException(e);
082: }
083: }
084:
085: public void mutate(int myId) {
086: this .count = (myId - 1) * ADD_COUNT;
087: System.err.println("AppId is :" + getApplicationId()
088: + " and count = " + count);
089: boolean remove = false;
090: long start = System.currentTimeMillis();
091: long totalInTXTime = 0;
092: for (int i = 0; i < ADD_COUNT; i++) {
093: totalInTXTime += test4(i, new Object());
094: if (false)
095: totalInTXTime += test5(i, new Object());
096: remove = !remove;
097: }
098: long seconds = (System.currentTimeMillis() - start) / 1000;
099: String commitsPerSecond = "DIVIDE BY ZERO!";
100: if (seconds != 0) {
101: commitsPerSecond = "" + (commits / seconds);
102: }
103: System.out.println("****Commits:" + commits + " seconds:"
104: + seconds + " commits/second: " + commitsPerSecond
105: + " Total in tx:" + totalInTXTime);
106: notifyDone();
107: }
108:
109: private void notifyDone() {
110: synchronized (myRoot) {
111: // The guy waiting is in TransparencySpeedTestVerifier ...
112: myRoot.notifyAll();
113: }
114: }
115:
116: public long test4(int i, Object foo) {
117: synchronized (myRoot) {
118: long start = System.currentTimeMillis();
119: commits++;
120: int s = myRoot.size();
121: long c = count++;
122: if (myRoot.containsKey(new Long(c))) {
123: Assert.eval(false);
124: }
125: myRoot.put(new Long(c), new TestObj(new TestObj(null)));
126: if (myRoot.size() != s + 1)
127: System.out.println("Wrong size!:" + s + " new size:"
128: + myRoot.size());
129: Assert.eval(myRoot.size() == s + 1);
130: // System.out.println("^^^TOTAL SIZE ADD:" + myRoot.size() + "^^^:" + this);
131: return System.currentTimeMillis() - start;
132: }
133: }
134:
135: public long test5(int i, Object foo) {
136: synchronized (myRoot) {
137: long start = System.currentTimeMillis();
138: commits++;
139: int s = myRoot.size();
140: myRoot.remove(new Long(count - 1));
141: if (myRoot.size() != s - 1)
142: System.out.println("Wrong size!:" + s + " new size:"
143: + myRoot.size());
144: Assert.eval(myRoot.size() == s - 1);
145: // System.out.println("^^^TOTAL SIZE REMOVE:" + myRoot.size() + "^^^:" + this);
146: return System.currentTimeMillis() - start;
147: }
148: }
149:
150: public static class TestObj {
151: private TestObj obj;
152: private String string = "Steve";
153: private int integer = 22;
154: private boolean bool = false;
155: private Map map = new HashMap();
156:
157: private TestObj() {
158: //
159: }
160:
161: public TestObj(TestObj obj) {
162: this .obj = obj;
163: for (int i = 0; i < 30; i++) {
164: map.put(new Long(i), new TestObj());
165: }
166: }
167:
168: public Object getObject() {
169: return this .obj;
170: }
171:
172: public boolean check() {
173: return string.equals("Steve") && integer == 22
174: && bool == false;
175: }
176: }
177: }
|