KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.text.DecimalFormat JavaDoc;
26 import java.text.FieldPosition JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 /**
33  * Local mode performance test for TreeCache.
34  *
35  * @version $Revision: 1.4 $
36  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> May 20 2003
37  */

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

260    private ArrayList JavaDoc nodeGen(int depth, int children)
261    {
262       ArrayList JavaDoc strList = new ArrayList JavaDoc();
263       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
264       ArrayList JavaDoc newList = new ArrayList JavaDoc();
265
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) tmp += "/";
278                newList.add(tmp);
279             }
280          }
281          strList.addAll(newList);
282          oldList = newList;
283          depth--;
284       }
285
286       log("Nodes generated: " + strList.size());
287       return strList;
288    }
289
290    public static Test suite() throws Exception JavaDoc
291    {
292       return new TestSuite(LocalMapPerfAopTest.class);
293    }
294
295    private void log(String JavaDoc str)
296    {
297 // System.out.println(this.getClass().getName() +": " +str);
298
System.out.println(str);
299    }
300
301 }
302
Popular Tags