KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

261    private ArrayList JavaDoc nodeGen(int depth, int children)
262    {
263       ArrayList JavaDoc strList = new ArrayList JavaDoc();
264       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
265       ArrayList JavaDoc newList = new ArrayList JavaDoc();
266
267       oldList.add("/");
268       newList.add("/");
269       strList.add("/");
270
271       while (depth > 0) {
272          // Trying to produce node name at this depth.
273
newList = new ArrayList JavaDoc();
274          for (int i = 0; i < oldList.size(); i++) {
275             for (int j = 0; j < children; j++) {
276                String JavaDoc tmp = (String JavaDoc) oldList.get(i);
277                tmp += Integer.toString(j);
278                if (depth != 1) tmp += "/";
279                newList.add(tmp);
280             }
281          }
282          strList.addAll(newList);
283          oldList = newList;
284          depth--;
285       }
286
287       log("Nodes generated: " + strList.size());
288       return strList;
289    }
290
291
292    public static Test suite() throws Exception JavaDoc
293    {
294       return new TestSuite(LocalMapPerfTestCase.class);
295    }
296
297    private void log(String JavaDoc str)
298    {
299 // System.out.println(this.getClass().getName() +": " +str);
300
System.out.println(str);
301    }
302
303 }
304
Popular Tags