KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > perf > aop > ReplicatedSyncPerfAopTest


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.aop;
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.aop.TreeCacheAop;
18 import org.jboss.cache.lock.IsolationLevel;
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 synchronous mode performance test for transactional TreeCache.
31  *
32  * @version $Revision: 1.4 $
33  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> May 20 2003
34  */

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

398    private ArrayList JavaDoc nodeGen(int depth, int children)
399    {
400       ArrayList JavaDoc strList = new ArrayList JavaDoc();
401       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
402       ArrayList JavaDoc newList = new ArrayList JavaDoc();
403
404       oldList.add("/");
405       newList.add("/");
406       strList.add("/");
407
408       while (depth > 0) {
409          // Trying to produce node name at this depth.
410
newList = new ArrayList JavaDoc();
411          for (int i = 0; i < oldList.size(); i++) {
412             for (int j = 0; j < children; j++) {
413                String JavaDoc tmp = (String JavaDoc) oldList.get(i);
414                tmp += Integer.toString(j);
415                if (depth != 1) tmp += "/";
416                newList.add(tmp);
417             }
418          }
419          strList.addAll(newList);
420          oldList = newList;
421          depth--;
422       }
423
424       log("Nodes generated: " + strList.size());
425       return strList;
426    }
427
428    public static Test suite() throws Exception JavaDoc
429    {
430       return new TestSuite(ReplicatedSyncPerfAopTest.class);
431    }
432
433    private void log(String JavaDoc str)
434    {
435 // System.out.println(this.getClass().getName() +": " +str);
436
System.out.println(str);
437    }
438
439 }
440
Popular Tags