KickJava   Java API By Example, From Geeks To Geeks.

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


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.transaction.DummyTransactionManager;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.transaction.UserTransaction JavaDoc;
23 import java.text.DecimalFormat JavaDoc;
24 import java.text.FieldPosition JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 /**
29  * Replicated synchronous mode performance test for transactional TreeCache.
30  *
31  * @version $Revision: 1.1 $
32  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> May 20 2003
33  */

34 public class ReplicatedSyncPerfTestCase extends TestCase
35 {
36    TreeCache cache1_, cache2_, cache3_;
37    int cachingMode_ = TreeCache.REPL_SYNC;
38    final String JavaDoc groupName_ = "TreeCacheTestGroup";
39    final static Properties JavaDoc p_;
40 // final static Logger log_ = Logger.getLogger(ReplicatedSyncPerfAopTest.class);
41
String JavaDoc oldFactory_ = null;
42    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
43
44    ArrayList JavaDoc nodeList_;
45    // (4, 4) combination will generate 340 nodes.
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 ReplicatedSyncPerfTestCase(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("ReplicatedSyncPerfAopTest: cacheMode=REPL_SYNC");
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/replSync-service.xml"); // use generic syncRepl xml
93
cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
94       cache.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
95       return cache;
96    }
97
98    void destroyCache(TreeCache cache) throws Exception JavaDoc
99    {
100       cache.stopService();
101       cache = null;
102    }
103
104
105     /** Executes a lot of put() operations against the cache */
106     public void testPuts() throws Exception JavaDoc
107    {
108         UserTransaction JavaDoc tx=null;
109         log("=== 1 cache with transaction (no concurrent access) many puts ===");
110         cache1_ = createCache();
111         cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
112         cache1_.setCacheMode(TreeCache.REPL_SYNC);
113         cache1_.startService();
114
115         tx=(UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
116
117         int nOps = 1000, realOps=0;
118         long time1 = System.currentTimeMillis();
119
120         try {
121             for(int i=0; i < nOps; i++) {
122                 tx.begin();
123                 cache1_.put("/bela/ban", "name", "Bela Ban");
124                 tx.commit();
125                 realOps++;
126             }
127         }
128         catch(Throwable JavaDoc t) {
129             if(tx != null)
130                 tx.rollback();
131         }
132         long time2 = System.currentTimeMillis();
133         assertEquals(nOps, realOps);
134
135         double d = (double) (time2 - time1) / realOps;
136         log("Time elapsed for _add is " + (time2 - time1) + " with " + realOps
137                 + " operations. Average per ops is: " + d + " msec.");
138
139        destroyCache(cache1_);
140     }
141
142    public void testOneCacheTx() throws Exception JavaDoc
143    {
144       log("=== 1 cache with transaction (no concurrent access) ===");
145       cache1_ = createCache();
146       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
147       cache1_.startService();
148
149       // Formating
150
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
151       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
152       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
153       boolean hasTx = true;
154       boolean oneTxOnly = false;
155
156       // Step 1. Add entries to the cache
157
System.out.println("-- before add: number of locks held is " + cache1_.getNumberOfLocksHeld());
158       long time1 = System.currentTimeMillis();
159       int nOps = _add(cache1_, hasTx, oneTxOnly);
160       long time2 = System.currentTimeMillis();
161       System.out.println("-- after add: number of locks held is " + cache1_.getNumberOfLocksHeld());
162       double d = (double) (time2 - time1) / nOps;
163       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
164             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
165             " msec.");
166       dumbStr = new StringBuffer JavaDoc();
167
168       // Step 2. Query the cache
169
System.out.println("-- before get: number of locks held is " + cache1_.getNumberOfLocksHeld());
170       time1 = System.currentTimeMillis();
171       nOps = _get(cache1_, hasTx, oneTxOnly);
172       time2 = System.currentTimeMillis();
173       System.out.println("-- after get: number of locks held is " + cache1_.getNumberOfLocksHeld());
174       d = (double) (time2 - time1) / nOps;
175       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
176             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
177             " msec.");
178       dumbStr = new StringBuffer JavaDoc();
179
180       // Step 3. Remove entries from the cache
181
System.out.println("-- before remove: number of locks held is " + cache1_.getNumberOfLocksHeld());
182       time1 = System.currentTimeMillis();
183       nOps = _remove(cache1_, hasTx, oneTxOnly);
184       time2 = System.currentTimeMillis();
185       System.out.println("-- after remove: number of locks held is " + cache1_.getNumberOfLocksHeld());
186       d = (double) (time2 - time1) / nOps;
187       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
188             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
189             " msec.");
190
191       destroyCache(cache1_);
192    }
193
194    public void test2CachesTx() throws Exception JavaDoc
195    {
196       log("=== 2 caches with transaction (no concurrent access) ===");
197       cache1_ = createCache();
198       cache2_ = createCache();
199       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
200       cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
201       cache1_.startService();
202       cache2_.startService();
203
204       // Formating
205
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
206       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
207       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
208       boolean hasTx = true;
209       boolean oneTxOnly = false;
210
211       // Step 1. Add entries to the cache
212
long time1 = System.currentTimeMillis();
213       int nOps = _add(cache1_, hasTx, oneTxOnly);
214       long time2 = System.currentTimeMillis();
215       double d = (double) (time2 - time1) / nOps;
216       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
217             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
218             " msec.");
219       dumbStr = new StringBuffer JavaDoc();
220
221       // Step 2. Query the cache
222
time1 = System.currentTimeMillis();
223       nOps = _get(cache1_, hasTx, oneTxOnly);
224       time2 = System.currentTimeMillis();
225       d = (double) (time2 - time1) / nOps;
226       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
227             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
228             " msec.");
229       dumbStr = new StringBuffer JavaDoc();
230
231       // Step 3. Remove entries from the cache
232
time1 = System.currentTimeMillis();
233       nOps = _remove(cache2_, hasTx, oneTxOnly); // Note we remove nodes from cache2.
234
time2 = System.currentTimeMillis();
235       d = (double) (time2 - time1) / nOps;
236       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
237             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
238             " msec.");
239
240       destroyCache(cache1_);
241       destroyCache(cache2_);
242    }
243
244    public void test2CachesOneTxOnly() throws Exception JavaDoc
245    {
246       log("=== 2 caches with single transaction only (no concurrent access) ===");
247       cache1_ = createCache();
248       cache2_ = createCache();
249       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
250       cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
251       cache1_.startService();
252       cache2_.startService();
253
254       // Formating
255
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
256       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
257       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
258       boolean hasTx = true;
259       boolean oneTxOnly = true;
260
261       // Step 1. Add entries to the cache
262
long time1 = System.currentTimeMillis();
263       int nOps = _add(cache1_, hasTx, oneTxOnly);
264       long time2 = System.currentTimeMillis();
265       double d = (double) (time2 - time1) / nOps;
266       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
267             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
268             " msec.");
269       dumbStr = new StringBuffer JavaDoc();
270
271       // Step 2. Query the cache
272
time1 = System.currentTimeMillis();
273       nOps = _get(cache1_, hasTx, oneTxOnly);
274       time2 = System.currentTimeMillis();
275       d = (double) (time2 - time1) / nOps;
276       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
277             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
278             " msec.");
279       dumbStr = new StringBuffer JavaDoc();
280
281       // Step 3. Remove entries from the cache
282
time1 = System.currentTimeMillis();
283       nOps = _remove(cache2_, hasTx, oneTxOnly); // Note we remove nodes from cache2.
284
time2 = System.currentTimeMillis();
285       d = (double) (time2 - time1) / nOps;
286       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
287             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
288             " msec.");
289
290       destroyCache(cache1_);
291       destroyCache(cache2_);
292    }
293
294    public void test3CachesTx() throws Exception JavaDoc
295    {
296       log("=== 3 caches with transaction (no concurrent access) ===");
297       cache1_ = createCache();
298       cache2_ = createCache();
299       cache3_ = createCache();
300       cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
301       cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
302       cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
303       cache3_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
304       cache1_.startService();
305       cache2_.startService();
306       cache3_.startService();
307
308       // Formating
309
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
310       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
311       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
312       boolean hasTx = true;
313       boolean oneTxOnly = false;
314
315       // Step 1. Add entries to the cache
316
long time1 = System.currentTimeMillis();
317       int nOps = _add(cache1_, hasTx, oneTxOnly);
318       long time2 = System.currentTimeMillis();
319       double d = (double) (time2 - time1) / nOps;
320       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
321             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
322             " msec.");
323       dumbStr = new StringBuffer JavaDoc();
324
325       // Step 2. Query the cache
326
time1 = System.currentTimeMillis();
327       nOps = _get(cache2_, hasTx, oneTxOnly); // Note query is from cache2
328
time2 = System.currentTimeMillis();
329       d = (double) (time2 - time1) / nOps;
330       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
331             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
332             " msec.");
333       dumbStr = new StringBuffer JavaDoc();
334
335       // Step 3. Remove entries from the cache
336
time1 = System.currentTimeMillis();
337       nOps = _remove(cache3_, hasTx, oneTxOnly); // Note remove is from cache3
338
time2 = System.currentTimeMillis();
339       d = (double) (time2 - time1) / nOps;
340       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
341             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
342             " msec.");
343
344       destroyCache(cache1_);
345       destroyCache(cache2_);
346       destroyCache(cache3_);
347    }
348
349    private int _add(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception JavaDoc
350    {
351       UserTransaction JavaDoc tx = null;
352       if (hasTx) {
353          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
354       }
355
356       if (hasTx && oneTxOnly) {
357          tx.begin();
358       }
359
360       for (int i = 0; i < nodeList_.size(); i++) {
361          String JavaDoc key = Integer.toString(i);
362          String JavaDoc value = Integer.toString(i);
363          if (hasTx && !oneTxOnly) {
364             tx.begin();
365             cache.put((String JavaDoc) nodeList_.get(i), key, value);
366             tx.commit();
367          } else {
368             cache.put((String JavaDoc) nodeList_.get(i), key, value);
369          }
370       }
371
372       if (hasTx && oneTxOnly) {
373          tx.commit();
374       }
375
376       return nodeList_.size();
377    }
378
379    private int _get(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception JavaDoc
380    {
381       UserTransaction JavaDoc tx = null;
382       if (hasTx) {
383          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
384       }
385
386       if (hasTx && oneTxOnly) {
387          tx.begin();
388       }
389
390       for (int i = 0; i < nodeList_.size(); i++) {
391          String JavaDoc key = Integer.toString(i);
392          if (hasTx && !oneTxOnly) {
393             tx.begin();
394             cache.get((String JavaDoc) nodeList_.get(i), key);
395             tx.commit();
396          } else {
397             cache.get((String JavaDoc) nodeList_.get(i), key);
398          }
399       }
400
401       if (hasTx && oneTxOnly) {
402          tx.commit();
403       }
404
405       return nodeList_.size();
406    }
407
408    private int _remove(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception JavaDoc
409    {
410       UserTransaction JavaDoc tx = null;
411       if (hasTx) {
412          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
413       }
414
415       if (hasTx && oneTxOnly) {
416          tx.begin();
417       }
418
419       for (int i = 0; i < nodeList_.size(); i++) {
420          String JavaDoc key = Integer.toString(i);
421          if (hasTx && !oneTxOnly) {
422             tx.begin();
423             cache.remove((String JavaDoc) nodeList_.get(i), key);
424             tx.commit();
425          } else {
426             cache.remove((String JavaDoc) nodeList_.get(i), key);
427          }
428       }
429
430       if (hasTx && oneTxOnly) {
431          tx.commit();
432       }
433
434       return nodeList_.size();
435    }
436
437    /**
438     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
439     * of the hierarchy and children is the number of children under each node.
440     */

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