KickJava   Java API By Example, From Geeks To Geeks.

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


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.3 $
36  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> May 20 2003
37  */

38 public class ReplicatedSyncMapPerfAopTest 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    static final int mapValueSize_ = 100;
52    static final String JavaDoc seed1_ = "This is a test. ";
53    static final String JavaDoc seed2_ = "THAT is a TEST. ";
54    StringBuffer JavaDoc originalStrBuf_;
55    StringBuffer JavaDoc newStrBuf_;
56
57    static
58    {
59       p_ = new Properties JavaDoc();
60       p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
61    }
62
63    public ReplicatedSyncMapPerfAopTest(String JavaDoc name)
64    {
65       super(name);
66    }
67
68    public void setUp() throws Exception JavaDoc
69    {
70       super.setUp();
71
72       oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
73       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
74
75       DummyTransactionManager.getInstance();
76       initCaches(TreeCache.LOCAL);
77       tm_ = new DummyTransactionManager();
78
79       originalStrBuf_ = new StringBuffer JavaDoc();
80       newStrBuf_ = new StringBuffer JavaDoc();
81       generateString();
82       log("ReplicatedSyncPerfAopTest: cacheMode=ReplSync");
83       nodeList_ = nodeGen(depth_, children_);
84       populateNode();
85    }
86
87    private void generateString()
88    {
89       int length = seed1_.length();
90       boolean isTrue = false;
91       while (originalStrBuf_.length() < mapValueSize_) {
92          originalStrBuf_.append(seed1_);
93          newStrBuf_.append(seed2_);
94       }
95    }
96
97    private void populateNode() throws Exception JavaDoc
98    {
99       // Formating
100
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
101       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
102       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
103       proxyMaps_ = new Map JavaDoc[nodeList_.size()];
104
105       long time1 = System.currentTimeMillis();
106       int nOps = 0;
107       for (int i = 0; i < nodeList_.size(); i++) {
108 // String key = Integer.toString(i);
109
// put the cache in the aop first
110
Map JavaDoc map = populateMap();
111          cache_.putObject((String JavaDoc) nodeList_.get(i), map);
112          proxyMaps_[i] = (Map JavaDoc) cache_.getObject((String JavaDoc) nodeList_.get(i));
113       }
114
115       nOps = nodeList_.size();
116       long time2 = System.currentTimeMillis();
117       double d = (double) (time2 - time1) / nOps;
118       log("Time elapsed for one putObject and getObject entry is " + (time2 - time1) + " with " + nOps
119             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
120             " msec.");
121    }
122
123    private Map JavaDoc populateMap()
124    {
125       Map JavaDoc map = new HashMap JavaDoc();
126       for (int i = 0; i < nodeList_.size(); i++) {
127          String JavaDoc key = Integer.toString(i) + "aop";
128          String JavaDoc value = originalStrBuf_.toString();
129          map.put(key, value);
130       }
131       return map;
132    }
133
134    public void tearDown() throws Exception JavaDoc
135    {
136       super.tearDown();
137
138       DummyTransactionManager.destroy();
139       destroyCaches();
140
141       if (oldFactory_ != null) {
142          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
143          oldFactory_ = null;
144       }
145
146       proxyMaps_ = null;
147    }
148
149    void initCaches(int caching_mode) throws Exception JavaDoc
150    {
151       cachingMode_ = caching_mode;
152       cache_ = new TreeCacheAop();
153       PropertyConfigurator config = new PropertyConfigurator();
154       config.configure(cache_, "META-INF/replSync-service.xml"); // read in generic replSync xml
155
cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
156       cache_.startService();
157 // org.jgroups.log.Trace.init();
158
}
159
160    void destroyCaches() throws Exception JavaDoc
161    {
162       cache_.stopService();
163       cache_ = null;
164    }
165
166    public void testAll() throws Exception JavaDoc
167    {
168
169       log("=== Start ===");
170 // try { Thread.sleep(5000); } catch (Exception ex) {};
171

172       // Formating
173
DecimalFormat JavaDoc form = new DecimalFormat JavaDoc("#.00");
174       FieldPosition JavaDoc fieldPos = new FieldPosition JavaDoc(0);
175       StringBuffer JavaDoc dumbStr = new StringBuffer JavaDoc();
176       boolean hasTx = false;
177
178       // Step 1. Add entries to the cache
179
long time1 = System.currentTimeMillis();
180       int nOps = 0;
181       nOps = _put(hasTx);
182       long time2 = System.currentTimeMillis();
183       double d = (double) (time2 - time1) / nOps;
184       log("Time elapsed for put entry is " + (time2 - time1) + " with " + nOps
185             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
186             " msec.");
187       dumbStr = new StringBuffer JavaDoc();
188
189       // Step 2. Query the cache
190
time1 = System.currentTimeMillis();
191       nOps = _get(hasTx);
192       time2 = System.currentTimeMillis();
193       d = (double) (time2 - time1) / nOps;
194       log("Time elapsed for get entry is " + (time2 - time1) + " with " + nOps
195             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
196             " msec.");
197       dumbStr = new StringBuffer JavaDoc();
198
199       // Step 3. Remove entries from the cache
200
time1 = System.currentTimeMillis();
201       nOps = _remove(hasTx);
202       time2 = System.currentTimeMillis();
203       d = (double) (time2 - time1) / nOps;
204       log("Time elapsed for remove entry is " + (time2 - time1) + " with " + nOps
205             + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) +
206             " msec.");
207       log("=== End ===");
208    }
209
210    protected void setLevelRW()
211    {
212       log("set lock level to RWUpgrade ...");
213       LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
214    }
215
216    protected void setLevelSerial()
217    {
218       log("set lock level to SimpleLock ...");
219       LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
220    }
221
222    private int _put(boolean hasTx) throws Exception JavaDoc
223    {
224       UserTransaction JavaDoc tx = null;
225       if (hasTx) {
226          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
227       }
228
229       String JavaDoc value = newStrBuf_.toString();
230       for (int i = 0; i < nodeList_.size(); i++) {
231          String JavaDoc key = Integer.toString(i) + "aop";
232          if (hasTx) {
233             tx.begin();
234             proxyMaps_[i].put(key, value);
235             tx.commit();
236          } else {
237             proxyMaps_[i].put(key, value);
238          }
239       }
240
241       return nodeList_.size();
242    }
243
244    private int _get(boolean hasTx) throws Exception JavaDoc
245    {
246       UserTransaction JavaDoc tx = null;
247       if (hasTx) {
248          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
249       }
250
251       for (int i = 1; i < nodeList_.size(); i++) {
252          String JavaDoc key = Integer.toString(i) + "aop";
253          if (hasTx) {
254             tx.begin();
255             String JavaDoc str = (String JavaDoc) proxyMaps_[i].get(key);
256 // log("_get(): key: " + key + " value: " +str);
257
tx.commit();
258          } else {
259             String JavaDoc str = (String JavaDoc) proxyMaps_[i].get(key);
260 // log("_get(): key: " + key + ", value: " +str);
261
}
262       }
263
264       return nodeList_.size();
265    }
266
267    private int _remove(boolean hasTx) throws Exception JavaDoc
268    {
269       UserTransaction JavaDoc tx = null;
270       if (hasTx) {
271          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
272       }
273
274       for (int i = 1; i < nodeList_.size(); i++) {
275          String JavaDoc key = Integer.toString(i) + "aop";
276          if (hasTx) {
277             tx.begin();
278             proxyMaps_[i].remove(key);
279             tx.commit();
280          } else {
281             proxyMaps_[i].remove(key);
282          }
283       }
284
285       return nodeList_.size();
286    }
287
288    /**
289     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
290     * of the hierarchy and children is the number of children under each node.
291     * This strucutre is used to add, get, and remove for each node.
292     */

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