KickJava   Java API By Example, From Geeks To Geeks.

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


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 ReplicatedAsyncMapPerfTestCase 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    static final int mapValueSize_ = 100;
54    static final String JavaDoc seed1_ = "This is a test. ";
55    static final String JavaDoc seed2_ = "THAT is a TEST. ";
56    StringBuffer JavaDoc originalStrBuf_;
57    StringBuffer JavaDoc newStrBuf_;
58
59    static
60    {
61       p_ = new Properties JavaDoc();
62       p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
63    }
64
65    public ReplicatedAsyncMapPerfTestCase(String JavaDoc name)
66    {
67       super(name);
68    }
69
70    public void setUp() throws Exception JavaDoc
71    {
72       super.setUp();
73
74       oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
75       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
76
77       DummyTransactionManager.getInstance();
78       initCaches(TreeCache.LOCAL);
79       tm_ = new DummyTransactionManager();
80
81       originalStrBuf_ = new StringBuffer JavaDoc();
82       newStrBuf_ = new StringBuffer JavaDoc();
83       generateString();
84       log("ReplicatedasyncMapPerfAopTest: cacheMode=replAsync");
85       populateMap();
86       nodeList_ = nodeGen(depth_, children_);
87    }
88
89    private void generateString()
90    {
91       int length = seed1_.length();
92       boolean isTrue = false;
93       while (originalStrBuf_.length() < mapValueSize_) {
94          originalStrBuf_.append(seed1_);
95          newStrBuf_.append(seed2_);
96       }
97    }
98
99    private void populateMap()
100    {
101       int depth = depth_;
102       int children = children_;
103       int total = 0;
104       while (depth > 0) {
105          for (int i = 0; i < total; i++) {
106             for (int j = 0; j < children; j++) {
107                total++;
108             }
109          }
110          depth--;
111       }
112
113       for (int i = 0; i < total; i++) {
114          String JavaDoc key = Integer.toString(i);
115          String JavaDoc value = originalStrBuf_.toString();
116          map_.put(key, value);
117       }
118    }
119
120    public void tearDown() throws Exception JavaDoc
121    {
122       super.tearDown();
123
124       DummyTransactionManager.destroy();
125       destroyCaches();
126
127       if (oldFactory_ != null) {
128          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
129          oldFactory_ = null;
130       }
131
132       map_.clear();
133    }
134
135    void initCaches(int caching_mode) throws Exception JavaDoc
136    {
137       cachingMode_ = caching_mode;
138       cache_ = new TreeCache();
139       PropertyConfigurator config = new PropertyConfigurator();
140       config.configure(cache_, "META-INF/replAsync-service.xml"); // read in generic replAsync xml
141
cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
142       cache_.startService();
143 // org.jgroups.log.Trace.init();
144
}
145
146    void destroyCaches() throws Exception JavaDoc
147    {
148       cache_.stopService();
149       cache_ = null;
150    }
151
152    public void testAll() throws Exception JavaDoc
153    {
154       try {
155          Thread.sleep(5000);
156       } catch (Exception JavaDoc ex) {
157       }
158       log("=== Transaction ===");
159       // Formating
160
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
161       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
162       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
163       boolean hasTx = false;
164
165       // Step 1. Add entries to the cache
166
long time1 = System.currentTimeMillis();
167       int nOps = _put(hasTx);
168       long time2 = System.currentTimeMillis();
169       double d = (double) (time2 - time1) / nOps;
170       log("Time elapsed for put entry is " + (time2 - time1) + " with " + nOps
171             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
172             " msec.");
173       dumbStr = new StringBuffer JavaDoc();
174
175       // Step 2. Query the cache
176
time1 = System.currentTimeMillis();
177       nOps = _get(hasTx);
178       time2 = System.currentTimeMillis();
179       d = (double) (time2 - time1) / nOps;
180       log("Time elapsed for get entry is " + (time2 - time1) + " with " + nOps
181             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
182             " msec.");
183       dumbStr = new StringBuffer JavaDoc();
184
185       // Step 3. Remove entries from the cache
186
time1 = System.currentTimeMillis();
187       nOps = _remove(hasTx);
188       time2 = System.currentTimeMillis();
189       d = (double) (time2 - time1) / nOps;
190       log("Time elapsed for remove entry is " + (time2 - time1) + " with " + nOps
191             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
192             " msec.");
193    }
194
195    protected void setLevelRW()
196    {
197       log("set lock level to RWUpgrade ...");
198       LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
199    }
200
201    protected void setLevelSerial()
202    {
203       log("set lock level to SimpleLock ...");
204       LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
205    }
206
207    private int _put(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       String JavaDoc value = newStrBuf_.toString();
215       for (int i = 0; i < nodeList_.size(); i++) {
216          String JavaDoc key = Integer.toString(i);
217          map_.put(key, value);
218          if (hasTx) {
219             tx.begin();
220             cache_.put((String JavaDoc) nodeList_.get(i), key, map_);
221             tx.commit();
222          } else {
223             cache_.put((String JavaDoc) nodeList_.get(i), key, map_);
224          }
225       }
226
227       return nodeList_.size();
228    }
229
230    private int _get(boolean hasTx) throws Exception JavaDoc
231    {
232       UserTransaction JavaDoc tx = null;
233       if (hasTx) {
234          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
235       }
236
237       for (int i = 0; i < nodeList_.size(); i++) {
238          String JavaDoc key = Integer.toString(i);
239          if (hasTx) {
240             tx.begin();
241             Map JavaDoc map = (Map JavaDoc) cache_.get((String JavaDoc) nodeList_.get(i), key);
242             String JavaDoc str = (String JavaDoc) map.get(key);
243 // log("_get(): key: " + key + " value: " +str);
244
tx.commit();
245          } else {
246             Map JavaDoc map = (Map JavaDoc) cache_.get((String JavaDoc) nodeList_.get(i), key);
247             String JavaDoc str = (String JavaDoc) map.get(key);
248 // log("_get(): key: " + key + " value: " +str);
249
}
250       }
251
252       return nodeList_.size();
253    }
254
255    private int _remove(boolean hasTx) throws Exception JavaDoc
256    {
257       UserTransaction JavaDoc tx = null;
258       if (hasTx) {
259          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
260       }
261
262       for (int i = 0; i < nodeList_.size(); i++) {
263          String JavaDoc key = Integer.toString(i);
264          if (hasTx) {
265             tx.begin();
266             Map JavaDoc map = (Map JavaDoc) cache_.get((String JavaDoc) nodeList_.get(i), key);
267             map.remove(key);
268             tx.commit();
269          } else {
270             Map JavaDoc map = (Map JavaDoc) cache_.get((String JavaDoc) nodeList_.get(i), key);
271             map.remove(key);
272          }
273       }
274
275       return nodeList_.size();
276    }
277
278
279    /**
280     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
281     * of the hierarchy and children is the number of children under each node.
282     * This strucutre is used to add, get, and remove for each node.
283     */

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