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.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 ReplicatedSyncPerfAopTest extends TestCase 36 { 37 TreeCacheAop cache1_, cache2_, cache3_; 38 int cachingMode_ = TreeCache.REPL_SYNC; 39 final String groupName_ = "TreeCacheTestGroup"; 40 final static Properties p_; 41 String oldFactory_ = null; 43 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 44 45 ArrayList nodeList_; 46 static final int depth_ = 3; 48 static final int children_ = 3; 49 DummyTransactionManager tm_; 50 51 static 52 { 53 p_ = new Properties (); 54 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 55 } 56 57 public ReplicatedSyncPerfAopTest(String name) 58 { 59 super(name); 60 } 61 62 public void setUp() throws Exception 63 { 64 super.setUp(); 65 66 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 67 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 68 69 DummyTransactionManager.getInstance(); 70 nodeList_ = nodeGen(depth_, children_); 71 tm_ = new DummyTransactionManager(); 72 73 log("ReplicatedSyncPerfAopTest: cacheMode=REPL_SYNC"); 74 } 75 76 public void tearDown() throws Exception 77 { 78 super.tearDown(); 79 80 DummyTransactionManager.destroy(); 81 82 if (oldFactory_ != null) { 83 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 84 oldFactory_ = null; 85 } 86 87 } 88 89 TreeCacheAop createCache() throws Exception 90 { 91 TreeCacheAop cache = new TreeCacheAop(); 92 PropertyConfigurator config = new PropertyConfigurator(); 93 config.configure(cache, "META-INF/replSync-service.xml"); cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 95 cache.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 96 return cache; 97 } 98 99 void destroyCache(TreeCache cache) throws Exception 100 { 101 cache.stopService(); 102 cache = null; 103 } 104 105 public void testOneCacheTx() throws Exception 106 { 107 log("=== 1 cache with transaction (no concurrent access) ==="); 108 cache1_ = createCache(); 109 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 110 cache1_.startService(); 111 112 DecimalFormat form = new DecimalFormat ("#.00"); 114 FieldPosition fieldPos = new FieldPosition (0); 115 StringBuffer dumbStr = new StringBuffer (); 116 boolean hasTx = true; 117 boolean oneTxOnly = false; 118 119 long time1 = System.currentTimeMillis(); 121 int nOps = _add(cache1_, hasTx, oneTxOnly); 122 long time2 = System.currentTimeMillis(); 123 double d = (double) (time2 - time1) / nOps; 124 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 125 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 126 " msec."); 127 dumbStr = new StringBuffer (); 128 129 time1 = System.currentTimeMillis(); 131 nOps = _get(cache1_, hasTx, oneTxOnly); 132 time2 = System.currentTimeMillis(); 133 d = (double) (time2 - time1) / nOps; 134 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 135 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 136 " msec."); 137 dumbStr = new StringBuffer (); 138 139 time1 = System.currentTimeMillis(); 141 nOps = _remove(cache1_, hasTx, oneTxOnly); 142 time2 = System.currentTimeMillis(); 143 d = (double) (time2 - time1) / nOps; 144 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 145 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 146 " msec."); 147 148 destroyCache(cache1_); 149 } 150 151 public void test2CachesTx() throws Exception 152 { 153 log("=== 2 caches with transaction (no concurrent access) ==="); 154 cache1_ = createCache(); 155 cache2_ = createCache(); 156 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 157 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 158 cache1_.startService(); 159 cache2_.startService(); 160 161 DecimalFormat form = new DecimalFormat ("#.00"); 163 FieldPosition fieldPos = new FieldPosition (0); 164 StringBuffer dumbStr = new StringBuffer (); 165 boolean hasTx = true; 166 boolean oneTxOnly = false; 167 168 long time1 = System.currentTimeMillis(); 170 int nOps = _add(cache1_, hasTx, oneTxOnly); 171 long time2 = System.currentTimeMillis(); 172 double d = (double) (time2 - time1) / nOps; 173 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 174 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 175 " msec."); 176 dumbStr = new StringBuffer (); 177 178 time1 = System.currentTimeMillis(); 180 nOps = _get(cache1_, hasTx, oneTxOnly); 181 time2 = System.currentTimeMillis(); 182 d = (double) (time2 - time1) / nOps; 183 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 184 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 185 " msec."); 186 dumbStr = new StringBuffer (); 187 188 time1 = System.currentTimeMillis(); 190 nOps = _remove(cache2_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 192 d = (double) (time2 - time1) / nOps; 193 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 194 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 195 " msec."); 196 197 destroyCache(cache1_); 198 destroyCache(cache2_); 199 } 200 201 public void test2CachesOneTxOnly() throws Exception 202 { 203 log("=== 2 caches with single transaction only (no concurrent access) ==="); 204 cache1_ = createCache(); 205 cache2_ = createCache(); 206 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 207 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 208 cache1_.startService(); 209 cache2_.startService(); 210 211 DecimalFormat form = new DecimalFormat ("#.00"); 213 FieldPosition fieldPos = new FieldPosition (0); 214 StringBuffer dumbStr = new StringBuffer (); 215 boolean hasTx = true; 216 boolean oneTxOnly = true; 217 218 long time1 = System.currentTimeMillis(); 220 int nOps = _add(cache1_, hasTx, oneTxOnly); 221 long time2 = System.currentTimeMillis(); 222 double d = (double) (time2 - time1) / nOps; 223 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 224 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 225 " msec."); 226 dumbStr = new StringBuffer (); 227 228 time1 = System.currentTimeMillis(); 230 nOps = _get(cache1_, hasTx, oneTxOnly); 231 time2 = System.currentTimeMillis(); 232 d = (double) (time2 - time1) / nOps; 233 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 234 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 235 " msec."); 236 dumbStr = new StringBuffer (); 237 238 time1 = System.currentTimeMillis(); 240 nOps = _remove(cache2_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 242 d = (double) (time2 - time1) / nOps; 243 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 244 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 245 " msec."); 246 247 destroyCache(cache1_); 248 destroyCache(cache2_); 249 } 250 251 public void test3CachesTx() throws Exception 252 { 253 log("=== 3 caches with transaction (no concurrent access) ==="); 254 cache1_ = createCache(); 255 cache2_ = createCache(); 256 cache3_ = createCache(); 257 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 258 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 259 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 260 cache3_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 261 cache1_.startService(); 262 cache2_.startService(); 263 cache3_.startService(); 264 265 DecimalFormat form = new DecimalFormat ("#.00"); 267 FieldPosition fieldPos = new FieldPosition (0); 268 StringBuffer dumbStr = new StringBuffer (); 269 boolean hasTx = true; 270 boolean oneTxOnly = false; 271 272 long time1 = System.currentTimeMillis(); 274 int nOps = _add(cache1_, hasTx, oneTxOnly); 275 long time2 = System.currentTimeMillis(); 276 double d = (double) (time2 - time1) / nOps; 277 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 278 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 279 " msec."); 280 dumbStr = new StringBuffer (); 281 282 time1 = System.currentTimeMillis(); 284 nOps = _get(cache2_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 286 d = (double) (time2 - time1) / nOps; 287 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 288 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 289 " msec."); 290 dumbStr = new StringBuffer (); 291 292 time1 = System.currentTimeMillis(); 294 nOps = _remove(cache3_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 296 d = (double) (time2 - time1) / nOps; 297 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 298 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 299 " msec."); 300 301 destroyCache(cache1_); 302 destroyCache(cache2_); 303 destroyCache(cache3_); 304 } 305 306 private int _add(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception 307 { 308 UserTransaction tx = null; 309 if (hasTx) { 310 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 311 } 312 313 if (hasTx && oneTxOnly) { 314 tx.begin(); 315 } 316 317 for (int i = 0; i < nodeList_.size(); i++) { 318 String key = Integer.toString(i); 319 String value = Integer.toString(i); 320 if (hasTx && !oneTxOnly) { 321 tx.begin(); 322 cache.put((String ) nodeList_.get(i), key, value); 323 tx.commit(); 324 } else { 325 cache.put((String ) nodeList_.get(i), key, value); 326 } 327 } 328 329 if (hasTx && oneTxOnly) { 330 tx.commit(); 331 } 332 333 return nodeList_.size(); 334 } 335 336 private int _get(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception 337 { 338 UserTransaction tx = null; 339 if (hasTx) { 340 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 341 } 342 343 if (hasTx && oneTxOnly) { 344 tx.begin(); 345 } 346 347 for (int i = 0; i < nodeList_.size(); i++) { 348 String key = Integer.toString(i); 349 if (hasTx && !oneTxOnly) { 350 tx.begin(); 351 cache.get((String ) nodeList_.get(i), key); 352 tx.commit(); 353 } else { 354 cache.get((String ) nodeList_.get(i), key); 355 } 356 } 357 358 if (hasTx && oneTxOnly) { 359 tx.commit(); 360 } 361 362 return nodeList_.size(); 363 } 364 365 private int _remove(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception 366 { 367 UserTransaction tx = null; 368 if (hasTx) { 369 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 370 } 371 372 if (hasTx && oneTxOnly) { 373 tx.begin(); 374 } 375 376 for (int i = 0; i < nodeList_.size(); i++) { 377 String key = Integer.toString(i); 378 if (hasTx && !oneTxOnly) { 379 tx.begin(); 380 cache.remove((String ) nodeList_.get(i), key); 381 tx.commit(); 382 } else { 383 cache.remove((String ) nodeList_.get(i), key); 384 } 385 } 386 387 if (hasTx && oneTxOnly) { 388 tx.commit(); 389 } 390 391 return nodeList_.size(); 392 } 393 394 398 private ArrayList nodeGen(int depth, int children) 399 { 400 ArrayList strList = new ArrayList (); 401 ArrayList oldList = new ArrayList (); 402 ArrayList newList = new ArrayList (); 403 404 oldList.add("/"); 405 newList.add("/"); 406 strList.add("/"); 407 408 while (depth > 0) { 409 newList = new ArrayList (); 411 for (int i = 0; i < oldList.size(); i++) { 412 for (int j = 0; j < children; j++) { 413 String tmp = (String ) oldList.get(i); 414 tmp += Integer.toString(j); 415 if (depth != 1) tmp += "/"; 416 newList.add(tmp); 417 } 418 } 419 strList.addAll(newList); 420 oldList = newList; 421 depth--; 422 } 423 424 log("Nodes generated: " + strList.size()); 425 return strList; 426 } 427 428 public static Test suite() throws Exception 429 { 430 return new TestSuite(ReplicatedSyncPerfAopTest.class); 431 } 432 433 private void log(String str) 434 { 435 System.out.println(str); 437 } 438 439 } 440 | Popular Tags |