1 8 9 package org.jboss.test.cache.perf.aop; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.jboss.cache.PropertyConfigurator; 15 import org.jboss.cache.TreeCache; 16 import org.jboss.cache.aop.TreeCacheAop; 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 ; 22 import javax.naming.InitialContext ; 23 import javax.transaction.UserTransaction ; 24 import java.text.DecimalFormat ; 25 import java.text.FieldPosition ; 26 import java.util.ArrayList ; 27 import java.util.Properties ; 28 29 35 public class LocalPerfAopTest extends TestCase 36 { 37 TreeCacheAop cache_; 38 int cachingMode_ = TreeCache.LOCAL; 39 final static Properties p_; 40 String oldFactory_ = null; 42 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 43 ArrayList nodeList_; 44 static final int depth_ = 3; 45 static final int children_ = 4; 46 DummyTransactionManager tm_; 47 48 static 49 { 50 p_ = new Properties (); 51 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 52 } 53 54 public LocalPerfAopTest(String name) 55 { 56 super(name); 57 } 58 59 public void setUp() throws Exception 60 { 61 super.setUp(); 62 63 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 64 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 65 66 DummyTransactionManager.getInstance(); 67 initCaches(TreeCache.LOCAL); 68 nodeList_ = nodeGen(depth_, children_); 69 tm_ = new DummyTransactionManager(); 70 71 log("LocalPerfAopTest: cacheMode=LOCAL, one cache"); 72 } 73 74 public void tearDown() throws Exception 75 { 76 super.tearDown(); 77 78 DummyTransactionManager.destroy(); 79 destroyCaches(); 80 81 if (oldFactory_ != null) { 82 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 83 oldFactory_ = null; 84 } 85 86 } 87 88 void initCaches(int caching_mode) throws Exception 89 { 90 cachingMode_ = caching_mode; 91 cache_ = new TreeCacheAop(); 92 PropertyConfigurator config = new PropertyConfigurator(); 93 config.configure(cache_, "META-INF/local-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 95 cache_.startService(); 96 } 97 98 void destroyCaches() throws Exception 99 { 100 cache_.stopService(); 101 cache_ = null; 102 } 103 104 public void testAll() throws Exception 105 { 106 log("=== No transaction ==="); 107 DecimalFormat form = new DecimalFormat ("#.00"); 109 FieldPosition fieldPos = new FieldPosition (0); 110 StringBuffer dumbStr = new StringBuffer (); 111 boolean hasTx = false; 112 113 long time1 = System.currentTimeMillis(); 115 int nOps = _add(hasTx); 116 long time2 = System.currentTimeMillis(); 117 double d = (double) (time2 - time1) / nOps; 118 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 119 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 120 " msec."); 121 dumbStr = new StringBuffer (); 122 123 time1 = System.currentTimeMillis(); 125 nOps = _get(hasTx); 126 time2 = System.currentTimeMillis(); 127 d = (double) (time2 - time1) / nOps; 128 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 129 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 130 " msec."); 131 dumbStr = new StringBuffer (); 132 133 time1 = System.currentTimeMillis(); 135 nOps = _remove(hasTx); 136 time2 = System.currentTimeMillis(); 137 d = (double) (time2 - time1) / nOps; 138 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 139 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 140 " msec."); 141 } 142 143 protected void setLevelRW() 144 { 145 log("set lock level to RWUpgrade ..."); 146 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 147 } 148 149 protected void setLevelSerial() 150 { 151 log("set lock level to SimpleLock ..."); 152 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 153 } 154 155 public void testAllTx_RWLock() throws Exception 156 { 157 setLevelRW(); 158 allTx(); 159 } 160 161 public void testAllTx_SimpleLock() throws Exception 162 { 163 setLevelSerial(); 164 allTx(); 165 } 166 167 protected void allTx() throws Exception 168 { 169 log("=== With transaction ==="); 170 DecimalFormat form = new DecimalFormat ("#.00"); 172 FieldPosition fieldPos = new FieldPosition (0); 173 StringBuffer dumbStr = new StringBuffer (); 174 boolean hasTx = true; 175 176 long time1 = System.currentTimeMillis(); 178 int nOps = _add(hasTx); 179 long time2 = System.currentTimeMillis(); 180 double d = (double) (time2 - time1) / nOps; 181 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 182 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 183 " msec."); 184 dumbStr = new StringBuffer (); 185 186 time1 = System.currentTimeMillis(); 188 nOps = _get(hasTx); 189 time2 = System.currentTimeMillis(); 190 d = (double) (time2 - time1) / nOps; 191 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 192 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 193 " msec."); 194 dumbStr = new StringBuffer (); 195 196 time1 = System.currentTimeMillis(); 198 nOps = _remove(hasTx); 199 time2 = System.currentTimeMillis(); 200 d = (double) (time2 - time1) / nOps; 201 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 202 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 203 " msec."); 204 } 205 206 private int _add(boolean hasTx) throws Exception 207 { 208 UserTransaction tx = null; 209 if (hasTx) { 210 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 211 } 212 213 for (int i = 0; i < nodeList_.size(); i++) { 214 String key = Integer.toString(i); 215 String value = Integer.toString(i); 216 if (hasTx) { 217 tx.begin(); 218 cache_.put((String ) nodeList_.get(i), key, value); 219 tx.commit(); 220 } else { 221 cache_.put((String ) nodeList_.get(i), key, value); 222 } 223 } 224 225 return nodeList_.size(); 226 } 227 228 private int _get(boolean hasTx) throws Exception 229 { 230 UserTransaction tx = null; 231 if (hasTx) { 232 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 233 } 234 235 for (int i = 0; i < nodeList_.size(); i++) { 236 String key = Integer.toString(i); 237 if (hasTx) { 238 tx.begin(); 239 cache_.get((String ) nodeList_.get(i), key); 240 tx.commit(); 241 } else { 242 cache_.get((String ) nodeList_.get(i), key); 243 } 244 } 245 246 return nodeList_.size(); 247 } 248 249 private int _remove(boolean hasTx) throws Exception 250 { 251 UserTransaction tx = null; 252 if (hasTx) { 253 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 254 } 255 256 for (int i = 0; i < nodeList_.size(); i++) { 257 String key = Integer.toString(i); 258 if (hasTx) { 259 tx.begin(); 260 cache_.remove((String ) nodeList_.get(i), key); 261 tx.commit(); 262 } else { 263 cache_.remove((String ) nodeList_.get(i), key); 264 } 265 } 266 267 return nodeList_.size(); 268 } 269 270 275 private ArrayList nodeGen(int depth, int children) 276 { 277 ArrayList strList = new ArrayList (); 278 ArrayList oldList = new ArrayList (); 279 ArrayList newList = new ArrayList (); 280 281 oldList.add("/"); 282 newList.add("/"); 283 strList.add("/"); 284 285 while (depth > 0) { 286 newList = new ArrayList (); 288 for (int i = 0; i < oldList.size(); i++) { 289 for (int j = 0; j < children; j++) { 290 String tmp = (String ) oldList.get(i); 291 tmp += Integer.toString(j); 292 if (depth != 1) tmp += "/"; 293 newList.add(tmp); 294 } 295 } 296 strList.addAll(newList); 297 oldList = newList; 298 depth--; 299 } 300 301 log("Nodes generated: " + strList.size()); 302 return strList; 303 } 304 305 public static Test suite() throws Exception 306 { 307 return new TestSuite(LocalPerfAopTest.class); 308 } 309 310 private void log(String str) 311 { 312 System.out.println(str); 314 } 315 316 } 317 | Popular Tags |