1 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 ; 23 import javax.naming.InitialContext ; 24 import javax.transaction.UserTransaction ; 25 import java.text.DecimalFormat ; 26 import java.text.FieldPosition ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 38 public class LocalMapPerfAopTest extends TestCase 39 { 40 TreeCacheAop cache_; 41 int cachingMode_ = TreeCache.LOCAL; 42 final static Properties p_; 43 String oldFactory_ = null; 45 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 46 DummyTransactionManager tm_; 47 Map [] proxyMaps_ = null; 48 ArrayList nodeList_; 49 static final int depth_ = 3; 50 static final int children_ = 4; 51 52 static 53 { 54 p_ = new Properties (); 55 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 56 } 57 58 public LocalMapPerfAopTest(String name) 59 { 60 super(name); 61 } 62 63 public void setUp() throws Exception 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 80 { 81 proxyMaps_ = new Map [nodeList_.size()]; 82 for (int i = 0; i < nodeList_.size(); i++) { 83 Map map = populateMap(); 86 cache_.putObject((String ) nodeList_.get(i), map); 87 proxyMaps_[i] = (Map ) cache_.getObject((String ) nodeList_.get(i)); 88 } 89 } 90 91 private Map populateMap() 92 { 93 Map map = new HashMap (); 94 for (int i = 0; i < nodeList_.size(); i++) { 95 String key = Integer.toString(i) + "aop"; 96 String 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 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 118 { 119 cachingMode_ = caching_mode; 120 cache_ = new TreeCacheAop(); 121 PropertyConfigurator config = new PropertyConfigurator(); 122 config.configure(cache_, "META-INF/local-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 124 cache_.startService(); 125 } 126 127 void destroyCaches() throws Exception 128 { 129 cache_.stopService(); 130 cache_ = null; 131 } 132 133 public void testAll() throws Exception 134 { 135 136 log("=== Transaction ==="); 137 DecimalFormat form = new DecimalFormat ("#.00"); 139 FieldPosition fieldPos = new FieldPosition (0); 140 StringBuffer dumbStr = new StringBuffer (); 141 boolean hasTx = false; 142 143 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 (); 153 154 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 (); 163 164 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 187 { 188 UserTransaction tx = null; 189 if (hasTx) { 190 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 191 } 192 193 String value = "This is a modified value for the unit testing"; 194 for (int i = 0; i < nodeList_.size(); i++) { 195 String key = Integer.toString(i) + "aop"; 196 Map map = (Map ) cache_.getObject((String ) nodeList_.get(i)); 197 if (hasTx) { 198 tx.begin(); 199 map.put(key, value); 201 tx.commit(); 202 } else { 203 map.put(key, value); 205 } 206 } 207 208 return nodeList_.size(); 209 } 210 211 private int _get(boolean hasTx) throws Exception 212 { 213 UserTransaction tx = null; 214 if (hasTx) { 215 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 216 } 217 218 for (int i = 1; i < nodeList_.size(); i++) { 219 String key = Integer.toString(i) + "aop"; 220 if (hasTx) { 221 tx.begin(); 222 String str = (String ) proxyMaps_[i].get(key); 223 tx.commit(); 225 } else { 226 String str = (String ) proxyMaps_[i].get(key); 227 } 229 } 230 231 return nodeList_.size(); 232 } 233 234 private int _remove(boolean hasTx) throws Exception 235 { 236 UserTransaction tx = null; 237 if (hasTx) { 238 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 239 } 240 241 for (int i = 1; i < nodeList_.size(); i++) { 242 String 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 260 private ArrayList nodeGen(int depth, int children) 261 { 262 ArrayList strList = new ArrayList (); 263 ArrayList oldList = new ArrayList (); 264 ArrayList newList = new ArrayList (); 265 266 oldList.add("/"); 267 newList.add("/"); 268 strList.add("/"); 269 270 while (depth > 0) { 271 newList = new ArrayList (); 273 for (int i = 0; i < oldList.size(); i++) { 274 for (int j = 0; j < children; j++) { 275 String tmp = (String ) 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 291 { 292 return new TestSuite(LocalMapPerfAopTest.class); 293 } 294 295 private void log(String str) 296 { 297 System.out.println(str); 299 } 300 301 } 302 | Popular Tags |