KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > stress > LocalStressTestCase


1 /*
2  *
3  * JBoss, the OpenSource J2EE webOS
4  *
5  * Distributable under LGPL license.
6  * See terms of license at gnu.org.
7  */

8
9 package org.jboss.test.cache.stress;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.jboss.cache.CacheException;
15 import org.jboss.cache.PropertyConfigurator;
16 import org.jboss.cache.TreeCache;
17 import org.jboss.cache.lock.IsolationLevel;
18 import org.jboss.cache.lock.LockStrategyFactory;
19 import org.jboss.cache.transaction.DummyTransactionManager;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.transaction.*;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Random JavaDoc;
27
28 //import java.text.DecimalFormat;
29
//import java.text.FieldPosition;
30

31 /**
32  * Local mode stress test for TreeCache.
33  *
34  * @version $Revision: 1.11 $
35  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> May 20 2003
36  */

37 public class LocalStressTestCase extends TestCase
38 {
39    static TreeCache cache_;
40    int cachingMode_ = TreeCache.LOCAL;
41    final static Properties JavaDoc p_;
42    // final static Logger log_ = Logger.getLogger(LocalStressTestCase.class);
43
String JavaDoc oldFactory_ = null;
44    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
45    static ArrayList JavaDoc nodeList_;
46    static final int depth_ = 4;
47    static final int children_ = 4;
48    DummyTransactionManager tm_;
49    static final int MAX_LOOP = 100;
50    static final int SLEEP_TIME = 50;
51    static Exception JavaDoc thread_ex=null;
52
53    static
54    {
55       p_ = new Properties JavaDoc();
56       p_.put(Context.INITIAL_CONTEXT_FACTORY,
57             "org.jboss.cache.transaction.DummyContextFactory");
58    }
59
60    public LocalStressTestCase(String JavaDoc name)
61    {
62       super(name);
63    }
64
65    public void setUp() throws Exception JavaDoc
66    {
67       super.setUp();
68
69       oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
70       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
71
72       DummyTransactionManager.getInstance();
73       initCaches(TreeCache.LOCAL);
74       nodeList_ = nodeGen(depth_, children_);
75       tm_ = new DummyTransactionManager();
76
77       log("LocalStressTestCase: cacheMode=TRANSIENT, one cache");
78    }
79
80    public void tearDown() throws Exception JavaDoc
81    {
82       super.tearDown();
83       thread_ex=null;
84       DummyTransactionManager.destroy();
85       destroyCaches();
86
87       if (oldFactory_ != null) {
88          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
89          oldFactory_ = null;
90       }
91
92    }
93
94    void initCaches(int caching_mode) throws Exception JavaDoc
95    {
96       cachingMode_ = caching_mode;
97       cache_ = new TreeCache();
98       PropertyConfigurator config = new PropertyConfigurator();
99       config.configure(cache_, "META-INF/local-eviction-service.xml"); // read in generic local xml
100
cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
101       cache_.createService();
102       cache_.startService();
103    }
104
105    void destroyCaches() throws Exception JavaDoc
106    {
107       cache_.stopService();
108       cache_ = null;
109    }
110
111    protected void setLevelRW()
112    {
113       log("set lock level to RWUpgrade ...");
114       LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
115    }
116
117    protected void setLevelSerial()
118    {
119       log("set lock level to SimpleLock ...");
120       LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
121    }
122
123    public void testAllTx_RWLock() throws Exception JavaDoc
124    {
125       setLevelRW();
126       allTx();
127    }
128
129    public void XtestAllTx_SimpleLock() throws Exception JavaDoc
130    {
131       setLevelSerial();
132       allTx();
133    }
134
135    private void allTx() throws Exception JavaDoc
136    {
137       UserTransaction tx = null;
138       tx = (UserTransaction) new InitialContext JavaDoc(p_).lookup("UserTransaction");
139
140       RunThread t1 = new RunThread(tx, 1);
141       RunThread t2 = new RunThread(tx, 2);
142
143       t1.start();
144       t2.start();
145
146       t1.join(60000); // wait for 20 secs
147
t2.join(60000); // wait for 20 secs
148

149       if(thread_ex != null)
150          throw thread_ex;
151    }
152
153    static class RunThread extends Thread JavaDoc
154    {
155       final int seed_;
156       Random JavaDoc random_;
157       UserTransaction tx_ = null;
158
159       public RunThread(UserTransaction tx, int seed)
160       {
161          seed_ = seed;
162          random_ = new Random JavaDoc(seed);
163          tx_ = tx;
164       }
165
166       public void run() {
167          try {
168             _run();
169          }
170          catch(Exception JavaDoc e) {
171             thread_ex=e;
172          }
173       }
174
175       public void _run() throws HeuristicMixedException, SystemException,
176                                 NotSupportedException, HeuristicRollbackException, RollbackException, CacheException {
177          for (int loop = 0; loop < MAX_LOOP; loop++) {
178             sleep_(random_.nextInt(50));
179             log("Executing op1. Loop-" + loop);
180             op1();
181             sleep_(random_.nextInt(50));
182             log("Executing op2...");
183             op2();
184             sleep_(random_.nextInt(50));
185             log("Executing op3...");
186             op3();
187          }
188       }
189
190       // Operation 1
191
private void op1() throws SystemException, NotSupportedException, CacheException,
192                                 HeuristicMixedException, HeuristicRollbackException, RollbackException {
193          int i = random_.nextInt(nodeList_.size() - 1);
194          String JavaDoc key = Integer.toString(i);
195          String JavaDoc value = Integer.toString(i);
196
197          tx_.begin();
198          String JavaDoc node = (String JavaDoc) nodeList_.get(i);
199          cache_.get(node, key);
200          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
201
cache_.put(node, key, value);
202          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
203
cache_.remove(node, key);
204          tx_.commit();
205       }
206
207       // Operation 2
208
private void op2() throws SystemException, NotSupportedException, CacheException,
209                                 HeuristicMixedException, HeuristicRollbackException, RollbackException {
210          int i = random_.nextInt(nodeList_.size() - 1);
211          String JavaDoc key = Integer.toString(i);
212
213          tx_.begin();
214          String JavaDoc node = (String JavaDoc) nodeList_.get(i);
215          cache_.get(node, key);
216          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
217
cache_.get(node, key);
218          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
219
tx_.commit();
220       }
221
222       // Operation 3
223
private void op3() throws SystemException, NotSupportedException, CacheException,
224                                 HeuristicMixedException, HeuristicRollbackException, RollbackException {
225          int i = random_.nextInt(nodeList_.size() - 1);
226          String JavaDoc key = Integer.toString(i);
227          String JavaDoc value = Integer.toString(i);
228
229          tx_.begin();
230          String JavaDoc node = (String JavaDoc) nodeList_.get(i);
231          cache_.put(node, key, value);
232          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
233
cache_.remove(node, key);
234          tx_.commit();
235       }
236
237       private void sleep_(long msecs)
238       {
239          try {
240             Thread.sleep(msecs);
241          } catch (Exception JavaDoc ex) {
242          }
243       }
244    }
245
246    /**
247     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
248     * of the hierarchy and children is the number of children under each node.
249     * This strucutre is used to add, get, and remove for each node.
250     */

251    private ArrayList JavaDoc nodeGen(int depth, int children)
252    {
253       ArrayList JavaDoc strList = new ArrayList JavaDoc();
254       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
255       ArrayList JavaDoc newList = new ArrayList JavaDoc();
256
257       oldList.add("/");
258       newList.add("/");
259       strList.add("/");
260
261       while (depth > 0) {
262          // Trying to produce node name at this depth.
263
newList = new ArrayList JavaDoc();
264          for (int i = 0; i < oldList.size(); i++) {
265             for (int j = 0; j < children; j++) {
266                String JavaDoc tmp = (String JavaDoc) oldList.get(i);
267                tmp += Integer.toString(j);
268                if (depth != 1)
269                   tmp += "/";
270                newList.add(tmp);
271             }
272          }
273          strList.addAll(newList);
274          oldList = newList;
275          depth--;
276       }
277
278       log("Nodes generated: " + strList.size());
279       return strList;
280    }
281
282    public static Test suite() throws Exception JavaDoc
283    {
284       return new TestSuite(LocalStressTestCase.class);
285    }
286
287    private static void log(String JavaDoc str)
288    {
289       System.out.println("Thread: " + Thread.currentThread() + ": " + str);
290 // System.out.println(str);
291
}
292
293 }
294
Popular Tags