KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > LocalConcurrentTest


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.cache.tests.aop;
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.aop.TreeCacheAop;
18 import org.jboss.cache.lock.IsolationLevel;
19 import org.jboss.cache.lock.LockStrategyFactory;
20 import org.jboss.cache.transaction.DummyTransactionManager;
21
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.transaction.UserTransaction JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Random JavaDoc;
28
29
30 /**
31  * Local mode stress test for TreeCacheAop. Test getObject, putObject and removeObject under load
32  * and concurrency.
33  *
34  * @version $Revision: 1.1.1.1 $
35  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> December 2004
36  */

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

150       if(thread_ex != null)
151          throw thread_ex;
152    }
153
154    class RunThread extends Thread JavaDoc
155    {
156       final int seed_;
157       Random JavaDoc random_;
158       Person person_;
159
160       public RunThread(int seed)
161       {
162          seed_ = seed;
163          random_ = new Random JavaDoc(seed);
164 // createPerson();
165
}
166
167       private void createPerson() {
168          person_ = new Person();
169          person_.setName("Ben");
170          person_.setAge(18);
171          ArrayList JavaDoc lang = new ArrayList JavaDoc();
172          lang.add("English");
173          lang.add("French");
174          lang.add("Mandarin");
175          person_.setLanguages(lang);
176          Address addr = new Address();
177          addr.setZip(95123);
178          addr.setStreet("Almeria");
179          addr.setCity("San Jose");
180          person_.setAddress(addr);
181       }
182
183       public void run() {
184          try {
185             _run();
186          }
187          catch(Exception JavaDoc e) {
188             thread_ex=e;
189          }
190       }
191
192       /**
193        * Note that this operation needs to run under transaction context as a whole. If not, then different
194        * threads will have the chance to use the same fqn node. While one thread has finished putObject and the
195        * other thread can the do the same, it will screw up the reference counting (??).
196        * @throws Exception
197        */

198       public void _run() throws Exception JavaDoc {
199          for (int loop = 0; loop < MAX_LOOP; loop++) {
200             tx_.begin();
201             createPerson();
202             sleep_(random_.nextInt(50));
203             op1();
204 // sleep_(random_.nextInt(50));
205
// op2();
206
sleep_(random_.nextInt(50));
207             op3();
208             tx_.commit();
209          }
210       }
211
212       // Operation 1
213
private void op1() throws CacheException {
214          int i = random_.nextInt(nodeList_.size() - 1);
215
216          String JavaDoc node = (String JavaDoc) nodeList_.get(i) + "/aop";
217          cache_.putObject(node, person_);
218          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
219
cache_.getObject(node);
220          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
221
cache_.removeObject(node);
222       }
223
224       // Operation 2
225
private void op2() throws CacheException {
226          int i = random_.nextInt(nodeList_.size() - 1);
227
228          String JavaDoc node = (String JavaDoc) nodeList_.get(i) + "/aop";
229          cache_.getObject(node);
230          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
231
cache_.getObject(node);
232          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
233
}
234
235       // Operation 3
236
private void op3() throws CacheException {
237          int i = random_.nextInt(nodeList_.size() - 1);
238
239          String JavaDoc node = (String JavaDoc) nodeList_.get(i) + "/aop";
240          cache_.putObject(node, person_);
241          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
242
cache_.removeObject(node);
243       }
244
245       private void sleep_(long msecs)
246       {
247          try {
248             Thread.sleep(msecs);
249          } catch (Exception JavaDoc ex) {
250          }
251       }
252    }
253
254    /**
255     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
256     * of the hierarchy and children is the number of children under each node.
257     * This strucutre is used to add, get, and remove for each node.
258     */

259    private ArrayList JavaDoc nodeGen(int depth, int children)
260    {
261       ArrayList JavaDoc strList = new ArrayList JavaDoc();
262       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
263       ArrayList JavaDoc newList = new ArrayList JavaDoc();
264
265       // Skip root node
266
oldList.add("/");
267       newList.add("/");
268       strList.add("/");
269
270       while (depth > 0) {
271          // Trying to produce node name at this depth.
272
newList = new ArrayList JavaDoc();
273          for (int i = 0; i < oldList.size(); i++) {
274             for (int j = 0; j < children; j++) {
275                String JavaDoc tmp = (String JavaDoc) oldList.get(i);
276                tmp += Integer.toString(j);
277                if (depth != 1) {
278                   tmp += "/";
279                }
280
281                newList.add(tmp);
282             }
283          }
284          strList.addAll(newList);
285          oldList = newList;
286          depth--;
287       }
288
289       // let's prune out root node
290
for(int i=0; i < strList.size(); i++) {
291          if( strList.get(i).equals("/") ) {
292             strList.remove(i);
293             break;
294          }
295       }
296       log("Nodes generated: " + strList.size());
297       return strList;
298    }
299
300    public static Test suite() throws Exception JavaDoc
301    {
302       return new TestSuite(LocalConcurrentTest.class);
303    }
304
305    private static void log(String JavaDoc str)
306    {
307       System.out.println("Thread: " + Thread.currentThread() + ": " + str);
308 // System.out.println(str);
309
}
310
311 }
312
Popular Tags