KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.jboss.cache.PropertyConfigurator;
15 import org.jboss.cache.TreeCache;
16 import org.jboss.cache.aop.TreeCacheAop;
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  * Local mode performance test for TreeCacheAop.
31  *
32  * @version $Revision: 1.4 $
33  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> May 20 2003
34  */

35 public class LocalPerfAopTest extends TestCase
36 {
37    TreeCacheAop cache_;
38    int cachingMode_ = TreeCache.LOCAL;
39    final static Properties JavaDoc p_;
40 // final static Logger log_ = Logger.getLogger(LocalPerfAopTest.class);
41
String JavaDoc oldFactory_ = null;
42    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
43    ArrayList JavaDoc nodeList_;
44    static final int depth_ = 3;
45    static final int children_ = 4;
46    DummyTransactionManager tm_;
47
48    static
49    {
50       p_ = new Properties JavaDoc();
51       p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
52    }
53
54    public LocalPerfAopTest(String JavaDoc name)
55    {
56       super(name);
57    }
58
59    public void setUp() throws Exception JavaDoc
60    {
61       super.setUp();
62
63       oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
64       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
65
66       DummyTransactionManager.getInstance();
67       initCaches(TreeCache.LOCAL);
68       nodeList_ = nodeGen(depth_, children_);
69       tm_ = new DummyTransactionManager();
70
71       log("LocalPerfAopTest: cacheMode=LOCAL, one cache");
72    }
73
74    public void tearDown() throws Exception JavaDoc
75    {
76       super.tearDown();
77
78       DummyTransactionManager.destroy();
79       destroyCaches();
80
81       if (oldFactory_ != null) {
82          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
83          oldFactory_ = null;
84       }
85
86    }
87
88    void initCaches(int caching_mode) throws Exception JavaDoc
89    {
90       cachingMode_ = caching_mode;
91       cache_ = new TreeCacheAop();
92       PropertyConfigurator config = new PropertyConfigurator();
93       config.configure(cache_, "META-INF/local-service.xml"); // read in generic local xml
94
cache_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
95       cache_.startService();
96    }
97
98    void destroyCaches() throws Exception JavaDoc
99    {
100       cache_.stopService();
101       cache_ = null;
102    }
103
104    public void testAll() throws Exception JavaDoc
105    {
106       log("=== No transaction ===");
107       // Formating
108
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
109       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
110       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
111       boolean hasTx = false;
112
113       // Step 1. Add entries to the cache
114
long time1 = System.currentTimeMillis();
115       int nOps = _add(hasTx);
116       long time2 = System.currentTimeMillis();
117       double d = (double) (time2 - time1) / nOps;
118       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
119             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
120             " msec.");
121       dumbStr = new StringBuffer JavaDoc();
122
123       // Step 2. Query the cache
124
time1 = System.currentTimeMillis();
125       nOps = _get(hasTx);
126       time2 = System.currentTimeMillis();
127       d = (double) (time2 - time1) / nOps;
128       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
129             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
130             " msec.");
131       dumbStr = new StringBuffer JavaDoc();
132
133       // Step 3. Remove entries from the cache
134
time1 = System.currentTimeMillis();
135       nOps = _remove(hasTx);
136       time2 = System.currentTimeMillis();
137       d = (double) (time2 - time1) / nOps;
138       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
139             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
140             " msec.");
141    }
142
143    protected void setLevelRW()
144    {
145       log("set lock level to RWUpgrade ...");
146       LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
147    }
148
149    protected void setLevelSerial()
150    {
151       log("set lock level to SimpleLock ...");
152       LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
153    }
154
155    public void testAllTx_RWLock() throws Exception JavaDoc
156    {
157       setLevelRW();
158       allTx();
159    }
160
161    public void testAllTx_SimpleLock() throws Exception JavaDoc
162    {
163       setLevelSerial();
164       allTx();
165    }
166
167    protected void allTx() throws Exception JavaDoc
168    {
169       log("=== With transaction ===");
170       // Formating
171
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
172       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
173       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
174       boolean hasTx = true;
175
176       // Step 1. Add entries to the cache
177
long time1 = System.currentTimeMillis();
178       int nOps = _add(hasTx);
179       long time2 = System.currentTimeMillis();
180       double d = (double) (time2 - time1) / nOps;
181       log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps
182             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
183             " msec.");
184       dumbStr = new StringBuffer JavaDoc();
185
186       // Step 2. Query the cache
187
time1 = System.currentTimeMillis();
188       nOps = _get(hasTx);
189       time2 = System.currentTimeMillis();
190       d = (double) (time2 - time1) / nOps;
191       log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps
192             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
193             " msec.");
194       dumbStr = new StringBuffer JavaDoc();
195
196       // Step 3. Remove entries from the cache
197
time1 = System.currentTimeMillis();
198       nOps = _remove(hasTx);
199       time2 = System.currentTimeMillis();
200       d = (double) (time2 - time1) / nOps;
201       log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps
202             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
203             " msec.");
204    }
205
206    private int _add(boolean hasTx) throws Exception JavaDoc
207    {
208       UserTransaction JavaDoc tx = null;
209       if (hasTx) {
210          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
211       }
212
213       for (int i = 0; i < nodeList_.size(); i++) {
214          String JavaDoc key = Integer.toString(i);
215          String JavaDoc value = Integer.toString(i);
216          if (hasTx) {
217             tx.begin();
218             cache_.put((String JavaDoc) nodeList_.get(i), key, value);
219             tx.commit();
220          } else {
221             cache_.put((String JavaDoc) nodeList_.get(i), key, value);
222          }
223       }
224
225       return nodeList_.size();
226    }
227
228    private int _get(boolean hasTx) throws Exception JavaDoc
229    {
230       UserTransaction JavaDoc tx = null;
231       if (hasTx) {
232          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
233       }
234
235       for (int i = 0; i < nodeList_.size(); i++) {
236          String JavaDoc key = Integer.toString(i);
237          if (hasTx) {
238             tx.begin();
239             cache_.get((String JavaDoc) nodeList_.get(i), key);
240             tx.commit();
241          } else {
242             cache_.get((String JavaDoc) nodeList_.get(i), key);
243          }
244       }
245
246       return nodeList_.size();
247    }
248
249    private int _remove(boolean hasTx) throws Exception JavaDoc
250    {
251       UserTransaction JavaDoc tx = null;
252       if (hasTx) {
253          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
254       }
255
256       for (int i = 0; i < nodeList_.size(); i++) {
257          String JavaDoc key = Integer.toString(i);
258          if (hasTx) {
259             tx.begin();
260             cache_.remove((String JavaDoc) nodeList_.get(i), key);
261             tx.commit();
262          } else {
263             cache_.remove((String JavaDoc) nodeList_.get(i), key);
264          }
265       }
266
267       return nodeList_.size();
268    }
269
270    /**
271     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
272     * of the hierarchy and children is the number of children under each node.
273     * This strucutre is used to add, get, and remove for each node.
274     */

275    private ArrayList JavaDoc nodeGen(int depth, int children)
276    {
277       ArrayList JavaDoc strList = new ArrayList JavaDoc();
278       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
279       ArrayList JavaDoc newList = new ArrayList JavaDoc();
280
281       oldList.add("/");
282       newList.add("/");
283       strList.add("/");
284
285       while (depth > 0) {
286          // Trying to produce node name at this depth.
287
newList = new ArrayList JavaDoc();
288          for (int i = 0; i < oldList.size(); i++) {
289             for (int j = 0; j < children; j++) {
290                String JavaDoc tmp = (String JavaDoc) oldList.get(i);
291                tmp += Integer.toString(j);
292                if (depth != 1) tmp += "/";
293                newList.add(tmp);
294             }
295          }
296          strList.addAll(newList);
297          oldList = newList;
298          depth--;
299       }
300
301       log("Nodes generated: " + strList.size());
302       return strList;
303    }
304
305    public static Test suite() throws Exception JavaDoc
306    {
307       return new TestSuite(LocalPerfAopTest.class);
308    }
309
310    private void log(String JavaDoc str)
311    {
312 // System.out.println(this.getClass().getName() +": " +str);
313
System.out.println(str);
314    }
315
316 }
317
Popular Tags