KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > perf > basic > ReplicatedAsyncPerfTestCase


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.perf.basic;
10
11
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
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.UserTransaction JavaDoc;
24 import java.text.DecimalFormat JavaDoc;
25 import java.text.FieldPosition JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * Replicated asynchronous mode performance test for transactional TreeCache
31  *
32  * @version $Revision: 1.1 $
33  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> May 20 2003
34  */

35 public class ReplicatedAsyncPerfTestCase extends TestCase
36 {
37    TreeCache cache1_, cache2_, cache3_;
38    int cachingMode_ = TreeCache.REPL_ASYNC;
39    final String JavaDoc groupName_ = "TreeCacheTestGroup";
40    final static Properties JavaDoc p_;
41 // final static Logger log_ = Logger.getLogger(ReplicatedAsyncPerfAopTest.class);
42
String JavaDoc oldFactory_ = null;
43    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
44
45    ArrayList JavaDoc nodeList_;
46    static final int depth_ = 3;
47    static final int children_ = 4;
48    DummyTransactionManager tm_;
49
50    static
51    {
52       p_ = new Properties JavaDoc();
53       p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
54    }
55
56    public ReplicatedAsyncPerfTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60
61    public void setUp() throws Exception JavaDoc
62    {
63       super.setUp();
64
65       oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
66       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
67
68       DummyTransactionManager.getInstance();
69       nodeList_ = nodeGen(depth_, children_);
70       tm_ = new DummyTransactionManager();
71
72       log("ReplicatedAsyncPerfAopTest: cacheMode=REPL_ASYNC");
73    }
74
75    public void tearDown() throws Exception JavaDoc
76    {
77       super.tearDown();
78
79       DummyTransactionManager.destroy();
80
81       if (oldFactory_ != null) {
82          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
83          oldFactory_ = null;
84       }
85
86    }
87
88    TreeCache createCache() throws Exception JavaDoc
89    {
90       TreeCache cache = new TreeCache();
91       PropertyConfigurator config = new PropertyConfigurator();
92       config.configure(cache, "META-INF/replAsync-service.xml");
93       cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
94       return cache;
95    }
96
97    void destroyCache(TreeCache cache) throws Exception JavaDoc
98    {
99       cache.stopService();
100       cache = null;
101    }
102
103    protected void setLevelRW()
104    {
105       log("set lock level to RWUpgrade ...");
106       LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
107    }
108
109    protected void setLevelSerial()
110    {
111       log("set lock level to SimpleLock ...");
112       LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
113    }
114
115    public void testOneCacheTx_RWLock() throws Exception JavaDoc
116    {
117       setLevelRW();
118       oneCacheTx();
119    }
120
121    public void testOneCacheTx_SimpleLock() throws Exception JavaDoc
122    {
123       setLevelSerial();
124       oneCacheTx();
125    }
126
127    protected void oneCacheTx() throws Exception JavaDoc
128    {
129       log("=== 1 cache with transaction (no concurrent access) ===");
130       cache1_ = createCache();
131       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
132       cache1_.startService();
133
134       // Formating
135
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
136       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
137       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
138       boolean hasTx = true;
139
140       // Step 1. Add entries to the cache
141
long time1 = System.currentTimeMillis();
142       int nOps = _add(cache1_, hasTx);
143       long time2 = System.currentTimeMillis();
144       double d = (double) (time2 - time1) / nOps;
145       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
146             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
147             " msec.");
148       dumbStr = new StringBuffer JavaDoc();
149
150       // Step 2. Query the cache
151
time1 = System.currentTimeMillis();
152       nOps = _get(cache1_, hasTx);
153       time2 = System.currentTimeMillis();
154       d = (double) (time2 - time1) / nOps;
155       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
156             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
157             " msec.");
158       dumbStr = new StringBuffer JavaDoc();
159
160       // Step 3. Remove entries from the cache
161
time1 = System.currentTimeMillis();
162       nOps = _remove(cache1_, hasTx);
163       time2 = System.currentTimeMillis();
164       d = (double) (time2 - time1) / nOps;
165       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
166             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
167             " msec.");
168
169       destroyCache(cache1_);
170    }
171
172    public void test2CachesTx_RWLock() throws Exception JavaDoc
173    {
174       setLevelRW();
175       twoCachesTx();
176    }
177
178    public void test2CachesTx_SimpleLock() throws Exception JavaDoc
179    {
180       setLevelSerial();
181       twoCachesTx();
182    }
183
184    protected void twoCachesTx() throws Exception JavaDoc
185    {
186       log("=== 2 caches with transaction (no concurrent access) ===");
187       cache1_ = createCache();
188       cache2_ = createCache();
189       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
190       cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
191       cache1_.startService();
192       cache2_.startService();
193
194       // Formating
195
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
196       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
197       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
198       boolean hasTx = true;
199
200       // Step 1. Add entries to the cache
201
long time1 = System.currentTimeMillis();
202       int nOps = _add(cache1_, hasTx);
203       long time2 = System.currentTimeMillis();
204       double d = (double) (time2 - time1) / nOps;
205       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
206             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
207             " msec.");
208       dumbStr = new StringBuffer JavaDoc();
209
210       // Step 2. Query the cache
211
time1 = System.currentTimeMillis();
212       nOps = _get(cache1_, hasTx);
213       time2 = System.currentTimeMillis();
214       d = (double) (time2 - time1) / nOps;
215       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
216             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
217             " msec.");
218       dumbStr = new StringBuffer JavaDoc();
219
220       // Step 3. Remove entries from the cache
221
time1 = System.currentTimeMillis();
222       nOps = _remove(cache2_, hasTx);
223       time2 = System.currentTimeMillis();
224       d = (double) (time2 - time1) / nOps;
225       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
226             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
227             " msec.");
228
229       destroyCache(cache1_);
230       destroyCache(cache2_);
231    }
232
233    public void test2CachesTxWithReplQueue_RWLock() throws Exception JavaDoc
234    {
235       setLevelRW();
236       twoCachesTxWithReplQueue();
237    }
238
239    public void test2CachesTxWithReplQueue_SimpleLock() throws Exception JavaDoc
240    {
241       setLevelSerial();
242       twoCachesTxWithReplQueue();
243    }
244
245    protected void twoCachesTxWithReplQueue() throws Exception JavaDoc
246    {
247       log("=== 2 caches with transaction (no concurrent access) using repl queue ===");
248       cache1_ = createCache();
249       cache2_ = createCache();
250
251       cache1_.setUseReplQueue(true);
252       cache1_.setReplQueueInterval(5000);
253       cache1_.setReplQueueMaxElements(100);
254
255       cache2_.setUseReplQueue(true);
256       cache2_.setReplQueueInterval(5000);
257       cache2_.setReplQueueMaxElements(100);
258
259       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
260       cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
261       cache1_.startService();
262       cache2_.startService();
263
264       // Formating
265
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
266       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
267       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
268       boolean hasTx = true;
269
270       // Step 1. Add entries to the cache
271
long time1 = System.currentTimeMillis();
272       int nOps = _add(cache1_, hasTx);
273       long time2 = System.currentTimeMillis();
274       double d = (double) (time2 - time1) / nOps;
275       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
276             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
277             " msec.");
278       dumbStr = new StringBuffer JavaDoc();
279
280       // Step 2. Query the cache
281
time1 = System.currentTimeMillis();
282       nOps = _get(cache1_, hasTx);
283       time2 = System.currentTimeMillis();
284       d = (double) (time2 - time1) / nOps;
285       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
286             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
287             " msec.");
288       dumbStr = new StringBuffer JavaDoc();
289
290       // Step 3. Remove entries from the cache
291
time1 = System.currentTimeMillis();
292       nOps = _remove(cache2_, hasTx);
293       time2 = System.currentTimeMillis();
294       d = (double) (time2 - time1) / nOps;
295       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
296             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
297             " msec.");
298
299       destroyCache(cache1_);
300       destroyCache(cache2_);
301    }
302
303    public void test3CachesTx_RWLock() throws Exception JavaDoc
304    {
305       setLevelRW();
306       threeCachesTx();
307    }
308
309    public void test3CachesTx_SimpleLock() throws Exception JavaDoc
310    {
311       setLevelSerial();
312       threeCachesTx();
313    }
314
315    protected void threeCachesTx() throws Exception JavaDoc
316    {
317       log("=== 3 caches with transaction (no concurrent access) ===");
318       cache1_ = createCache();
319       cache2_ = createCache();
320       cache3_ = createCache();
321       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
322       cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
323       cache3_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
324       cache1_.startService();
325       cache2_.startService();
326       cache3_.startService();
327
328       // Formating
329
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
330       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
331       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
332       boolean hasTx = true;
333
334       // Step 1. Add entries to the cache
335
long time1 = System.currentTimeMillis();
336       int nOps = _add(cache1_, hasTx);
337       long time2 = System.currentTimeMillis();
338       double d = (double) (time2 - time1) / nOps;
339       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
340             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
341             " msec.");
342       dumbStr = new StringBuffer JavaDoc();
343
344       // Step 2. Query the cache
345
time1 = System.currentTimeMillis();
346       nOps = _get(cache2_, hasTx);
347       time2 = System.currentTimeMillis();
348       d = (double) (time2 - time1) / nOps;
349       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
350             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
351             " msec.");
352       dumbStr = new StringBuffer JavaDoc();
353
354       // Step 3. Remove entries from the cache
355
time1 = System.currentTimeMillis();
356       nOps = _remove(cache3_, hasTx);
357       time2 = System.currentTimeMillis();
358       d = (double) (time2 - time1) / nOps;
359       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
360             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
361             " msec.");
362
363       destroyCache(cache1_);
364       destroyCache(cache2_);
365       destroyCache(cache3_);
366    }
367
368    private int _add(TreeCache cache, boolean hasTx) throws Exception JavaDoc
369    {
370       UserTransaction JavaDoc tx = null;
371       if (hasTx) {
372          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
373       }
374
375       for (int i = 0; i < nodeList_.size(); i++) {
376          String JavaDoc key = Integer.toString(i);
377          String JavaDoc value = Integer.toString(i);
378          if (hasTx) {
379             tx.begin();
380             cache.put((String JavaDoc) nodeList_.get(i), key, value);
381             tx.commit();
382          } else {
383             cache.put((String JavaDoc) nodeList_.get(i), key, value);
384          }
385       }
386
387       return nodeList_.size();
388    }
389
390    private int _get(TreeCache cache, boolean hasTx) throws Exception JavaDoc
391    {
392       UserTransaction JavaDoc tx = null;
393       if (hasTx) {
394          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
395       }
396
397       for (int i = 0; i < nodeList_.size(); i++) {
398          String JavaDoc key = Integer.toString(i);
399          if (hasTx) {
400             tx.begin();
401             cache.get((String JavaDoc) nodeList_.get(i), key);
402             tx.commit();
403          } else {
404             cache.get((String JavaDoc) nodeList_.get(i), key);
405          }
406       }
407
408       return nodeList_.size();
409    }
410
411    private int _remove(TreeCache cache, boolean hasTx) throws Exception JavaDoc
412    {
413       UserTransaction JavaDoc tx = null;
414       if (hasTx) {
415          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
416       }
417
418       for (int i = 0; i < nodeList_.size(); i++) {
419          String JavaDoc key = Integer.toString(i);
420          if (hasTx) {
421             tx.begin();
422             cache.remove((String JavaDoc) nodeList_.get(i), key);
423             tx.commit();
424          } else {
425             cache.remove((String JavaDoc) nodeList_.get(i), key);
426          }
427       }
428
429       return nodeList_.size();
430    }
431
432    /**
433     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
434     * of the hierarchy and children is the number of children under each node.
435     */

436    private ArrayList JavaDoc nodeGen(int depth, int children)
437    {
438       ArrayList JavaDoc strList = new ArrayList JavaDoc();
439       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
440       ArrayList JavaDoc newList = new ArrayList JavaDoc();
441
442       oldList.add("/");
443       newList.add("/");
444       strList.add("/");
445
446       while (depth > 0) {
447          // Trying to produce node name at this depth.
448
newList = new ArrayList JavaDoc();
449          for (int i = 0; i < oldList.size(); i++) {
450             for (int j = 0; j < children; j++) {
451                String JavaDoc tmp = (String JavaDoc) oldList.get(i);
452                tmp += Integer.toString(j);
453                if (depth != 1) tmp += "/";
454                newList.add(tmp);
455             }
456          }
457          strList.addAll(newList);
458          oldList = newList;
459          depth--;
460       }
461
462       log("Nodes generated: " + strList.size());
463       return strList;
464    }
465
466    public static Test suite() throws Exception JavaDoc
467    {
468       return new TestSuite(ReplicatedAsyncPerfTestCase.class);
469    }
470
471    private void log(String JavaDoc str)
472    {
473 // System.out.println(this.getClass().getName() +": " +str);
474
System.out.println(str);
475    }
476
477 }
478
Popular Tags