1 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 ; 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 ReplicatedAsyncPerfTestCase extends TestCase 36 { 37 TreeCache cache1_, cache2_, cache3_; 38 int cachingMode_ = TreeCache.REPL_ASYNC; 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; 47 static final int children_ = 4; 48 DummyTransactionManager tm_; 49 50 static 51 { 52 p_ = new Properties (); 53 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 54 } 55 56 public ReplicatedAsyncPerfTestCase(String name) 57 { 58 super(name); 59 } 60 61 public void setUp() throws Exception 62 { 63 super.setUp(); 64 65 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 66 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 67 68 DummyTransactionManager.getInstance(); 69 nodeList_ = nodeGen(depth_, children_); 70 tm_ = new DummyTransactionManager(); 71 72 log("ReplicatedAsyncPerfAopTest: cacheMode=REPL_ASYNC"); 73 } 74 75 public void tearDown() throws Exception 76 { 77 super.tearDown(); 78 79 DummyTransactionManager.destroy(); 80 81 if (oldFactory_ != null) { 82 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 83 oldFactory_ = null; 84 } 85 86 } 87 88 TreeCache createCache() throws Exception 89 { 90 TreeCache cache = new TreeCache(); 91 PropertyConfigurator config = new PropertyConfigurator(); 92 config.configure(cache, "META-INF/replAsync-service.xml"); 93 cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 94 return cache; 95 } 96 97 void destroyCache(TreeCache cache) throws Exception 98 { 99 cache.stopService(); 100 cache = null; 101 } 102 103 protected void setLevelRW() 104 { 105 log("set lock level to RWUpgrade ..."); 106 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 107 } 108 109 protected void setLevelSerial() 110 { 111 log("set lock level to SimpleLock ..."); 112 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 113 } 114 115 public void testOneCacheTx_RWLock() throws Exception 116 { 117 setLevelRW(); 118 oneCacheTx(); 119 } 120 121 public void testOneCacheTx_SimpleLock() throws Exception 122 { 123 setLevelSerial(); 124 oneCacheTx(); 125 } 126 127 protected void oneCacheTx() throws Exception 128 { 129 log("=== 1 cache with transaction (no concurrent access) ==="); 130 cache1_ = createCache(); 131 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 132 cache1_.startService(); 133 134 DecimalFormat form = new DecimalFormat ("#.00"); 136 FieldPosition fieldPos = new FieldPosition (0); 137 StringBuffer dumbStr = new StringBuffer (); 138 boolean hasTx = true; 139 140 long time1 = System.currentTimeMillis(); 142 int nOps = _add(cache1_, hasTx); 143 long time2 = System.currentTimeMillis(); 144 double d = (double) (time2 - time1) / nOps; 145 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 146 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 147 " msec."); 148 dumbStr = new StringBuffer (); 149 150 time1 = System.currentTimeMillis(); 152 nOps = _get(cache1_, hasTx); 153 time2 = System.currentTimeMillis(); 154 d = (double) (time2 - time1) / nOps; 155 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 156 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 157 " msec."); 158 dumbStr = new StringBuffer (); 159 160 time1 = System.currentTimeMillis(); 162 nOps = _remove(cache1_, hasTx); 163 time2 = System.currentTimeMillis(); 164 d = (double) (time2 - time1) / nOps; 165 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 166 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 167 " msec."); 168 169 destroyCache(cache1_); 170 } 171 172 public void test2CachesTx_RWLock() throws Exception 173 { 174 setLevelRW(); 175 twoCachesTx(); 176 } 177 178 public void test2CachesTx_SimpleLock() throws Exception 179 { 180 setLevelSerial(); 181 twoCachesTx(); 182 } 183 184 protected void twoCachesTx() throws Exception 185 { 186 log("=== 2 caches with transaction (no concurrent access) ==="); 187 cache1_ = createCache(); 188 cache2_ = createCache(); 189 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 190 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 191 cache1_.startService(); 192 cache2_.startService(); 193 194 DecimalFormat form = new DecimalFormat ("#.00"); 196 FieldPosition fieldPos = new FieldPosition (0); 197 StringBuffer dumbStr = new StringBuffer (); 198 boolean hasTx = true; 199 200 long time1 = System.currentTimeMillis(); 202 int nOps = _add(cache1_, hasTx); 203 long time2 = System.currentTimeMillis(); 204 double d = (double) (time2 - time1) / nOps; 205 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 206 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 207 " msec."); 208 dumbStr = new StringBuffer (); 209 210 time1 = System.currentTimeMillis(); 212 nOps = _get(cache1_, hasTx); 213 time2 = System.currentTimeMillis(); 214 d = (double) (time2 - time1) / nOps; 215 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 216 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 217 " msec."); 218 dumbStr = new StringBuffer (); 219 220 time1 = System.currentTimeMillis(); 222 nOps = _remove(cache2_, hasTx); 223 time2 = System.currentTimeMillis(); 224 d = (double) (time2 - time1) / nOps; 225 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 226 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 227 " msec."); 228 229 destroyCache(cache1_); 230 destroyCache(cache2_); 231 } 232 233 public void test2CachesTxWithReplQueue_RWLock() throws Exception 234 { 235 setLevelRW(); 236 twoCachesTxWithReplQueue(); 237 } 238 239 public void test2CachesTxWithReplQueue_SimpleLock() throws Exception 240 { 241 setLevelSerial(); 242 twoCachesTxWithReplQueue(); 243 } 244 245 protected void twoCachesTxWithReplQueue() throws Exception 246 { 247 log("=== 2 caches with transaction (no concurrent access) using repl queue ==="); 248 cache1_ = createCache(); 249 cache2_ = createCache(); 250 251 cache1_.setUseReplQueue(true); 252 cache1_.setReplQueueInterval(5000); 253 cache1_.setReplQueueMaxElements(100); 254 255 cache2_.setUseReplQueue(true); 256 cache2_.setReplQueueInterval(5000); 257 cache2_.setReplQueueMaxElements(100); 258 259 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 260 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 261 cache1_.startService(); 262 cache2_.startService(); 263 264 DecimalFormat form = new DecimalFormat ("#.00"); 266 FieldPosition fieldPos = new FieldPosition (0); 267 StringBuffer dumbStr = new StringBuffer (); 268 boolean hasTx = true; 269 270 long time1 = System.currentTimeMillis(); 272 int nOps = _add(cache1_, hasTx); 273 long time2 = System.currentTimeMillis(); 274 double d = (double) (time2 - time1) / nOps; 275 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 276 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 277 " msec."); 278 dumbStr = new StringBuffer (); 279 280 time1 = System.currentTimeMillis(); 282 nOps = _get(cache1_, hasTx); 283 time2 = System.currentTimeMillis(); 284 d = (double) (time2 - time1) / nOps; 285 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 286 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 287 " msec."); 288 dumbStr = new StringBuffer (); 289 290 time1 = System.currentTimeMillis(); 292 nOps = _remove(cache2_, hasTx); 293 time2 = System.currentTimeMillis(); 294 d = (double) (time2 - time1) / nOps; 295 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 296 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 297 " msec."); 298 299 destroyCache(cache1_); 300 destroyCache(cache2_); 301 } 302 303 public void test3CachesTx_RWLock() throws Exception 304 { 305 setLevelRW(); 306 threeCachesTx(); 307 } 308 309 public void test3CachesTx_SimpleLock() throws Exception 310 { 311 setLevelSerial(); 312 threeCachesTx(); 313 } 314 315 protected void threeCachesTx() throws Exception 316 { 317 log("=== 3 caches with transaction (no concurrent access) ==="); 318 cache1_ = createCache(); 319 cache2_ = createCache(); 320 cache3_ = createCache(); 321 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 322 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 323 cache3_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 324 cache1_.startService(); 325 cache2_.startService(); 326 cache3_.startService(); 327 328 DecimalFormat form = new DecimalFormat ("#.00"); 330 FieldPosition fieldPos = new FieldPosition (0); 331 StringBuffer dumbStr = new StringBuffer (); 332 boolean hasTx = true; 333 334 long time1 = System.currentTimeMillis(); 336 int nOps = _add(cache1_, hasTx); 337 long time2 = System.currentTimeMillis(); 338 double d = (double) (time2 - time1) / nOps; 339 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 340 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 341 " msec."); 342 dumbStr = new StringBuffer (); 343 344 time1 = System.currentTimeMillis(); 346 nOps = _get(cache2_, hasTx); 347 time2 = System.currentTimeMillis(); 348 d = (double) (time2 - time1) / nOps; 349 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 350 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 351 " msec."); 352 dumbStr = new StringBuffer (); 353 354 time1 = System.currentTimeMillis(); 356 nOps = _remove(cache3_, hasTx); 357 time2 = System.currentTimeMillis(); 358 d = (double) (time2 - time1) / nOps; 359 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 360 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 361 " msec."); 362 363 destroyCache(cache1_); 364 destroyCache(cache2_); 365 destroyCache(cache3_); 366 } 367 368 private int _add(TreeCache cache, boolean hasTx) throws Exception 369 { 370 UserTransaction tx = null; 371 if (hasTx) { 372 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 373 } 374 375 for (int i = 0; i < nodeList_.size(); i++) { 376 String key = Integer.toString(i); 377 String value = Integer.toString(i); 378 if (hasTx) { 379 tx.begin(); 380 cache.put((String ) nodeList_.get(i), key, value); 381 tx.commit(); 382 } else { 383 cache.put((String ) nodeList_.get(i), key, value); 384 } 385 } 386 387 return nodeList_.size(); 388 } 389 390 private int _get(TreeCache cache, boolean hasTx) throws Exception 391 { 392 UserTransaction tx = null; 393 if (hasTx) { 394 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 395 } 396 397 for (int i = 0; i < nodeList_.size(); i++) { 398 String key = Integer.toString(i); 399 if (hasTx) { 400 tx.begin(); 401 cache.get((String ) nodeList_.get(i), key); 402 tx.commit(); 403 } else { 404 cache.get((String ) nodeList_.get(i), key); 405 } 406 } 407 408 return nodeList_.size(); 409 } 410 411 private int _remove(TreeCache cache, boolean hasTx) throws Exception 412 { 413 UserTransaction tx = null; 414 if (hasTx) { 415 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 416 } 417 418 for (int i = 0; i < nodeList_.size(); i++) { 419 String key = Integer.toString(i); 420 if (hasTx) { 421 tx.begin(); 422 cache.remove((String ) nodeList_.get(i), key); 423 tx.commit(); 424 } else { 425 cache.remove((String ) nodeList_.get(i), key); 426 } 427 } 428 429 return nodeList_.size(); 430 } 431 432 436 private ArrayList nodeGen(int depth, int children) 437 { 438 ArrayList strList = new ArrayList (); 439 ArrayList oldList = new ArrayList (); 440 ArrayList newList = new ArrayList (); 441 442 oldList.add("/"); 443 newList.add("/"); 444 strList.add("/"); 445 446 while (depth > 0) { 447 newList = new ArrayList (); 449 for (int i = 0; i < oldList.size(); i++) { 450 for (int j = 0; j < children; j++) { 451 String tmp = (String ) oldList.get(i); 452 tmp += Integer.toString(j); 453 if (depth != 1) tmp += "/"; 454 newList.add(tmp); 455 } 456 } 457 strList.addAll(newList); 458 oldList = newList; 459 depth--; 460 } 461 462 log("Nodes generated: " + strList.size()); 463 return strList; 464 } 465 466 public static Test suite() throws Exception 467 { 468 return new TestSuite(ReplicatedAsyncPerfTestCase.class); 469 } 470 471 private void log(String str) 472 { 473 System.out.println(str); 475 } 476 477 } 478 | Popular Tags |