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.transaction.DummyTransactionManager; 19 20 import javax.naming.Context ; 21 import javax.naming.InitialContext ; 22 import javax.transaction.UserTransaction ; 23 import java.text.DecimalFormat ; 24 import java.text.FieldPosition ; 25 import java.util.ArrayList ; 26 import java.util.Properties ; 27 28 34 public class ReplicatedSyncPerfTestCase extends TestCase 35 { 36 TreeCache cache1_, cache2_, cache3_; 37 int cachingMode_ = TreeCache.REPL_SYNC; 38 final String groupName_ = "TreeCacheTestGroup"; 39 final static Properties p_; 40 String oldFactory_ = null; 42 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 43 44 ArrayList nodeList_; 45 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 ReplicatedSyncPerfTestCase(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("ReplicatedSyncPerfAopTest: cacheMode=REPL_SYNC"); 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/replSync-service.xml"); cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 94 cache.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 95 return cache; 96 } 97 98 void destroyCache(TreeCache cache) throws Exception 99 { 100 cache.stopService(); 101 cache = null; 102 } 103 104 105 106 public void testPuts() throws Exception 107 { 108 UserTransaction tx=null; 109 log("=== 1 cache with transaction (no concurrent access) many puts ==="); 110 cache1_ = createCache(); 111 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 112 cache1_.setCacheMode(TreeCache.REPL_SYNC); 113 cache1_.startService(); 114 115 tx=(UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 116 117 int nOps = 1000, realOps=0; 118 long time1 = System.currentTimeMillis(); 119 120 try { 121 for(int i=0; i < nOps; i++) { 122 tx.begin(); 123 cache1_.put("/bela/ban", "name", "Bela Ban"); 124 tx.commit(); 125 realOps++; 126 } 127 } 128 catch(Throwable t) { 129 if(tx != null) 130 tx.rollback(); 131 } 132 long time2 = System.currentTimeMillis(); 133 assertEquals(nOps, realOps); 134 135 double d = (double) (time2 - time1) / realOps; 136 log("Time elapsed for _add is " + (time2 - time1) + " with " + realOps 137 + " operations. Average per ops is: " + d + " msec."); 138 139 destroyCache(cache1_); 140 } 141 142 public void testOneCacheTx() throws Exception 143 { 144 log("=== 1 cache with transaction (no concurrent access) ==="); 145 cache1_ = createCache(); 146 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 147 cache1_.startService(); 148 149 DecimalFormat form = new DecimalFormat ("#.00"); 151 FieldPosition fieldPos = new FieldPosition (0); 152 StringBuffer dumbStr = new StringBuffer (); 153 boolean hasTx = true; 154 boolean oneTxOnly = false; 155 156 System.out.println("-- before add: number of locks held is " + cache1_.getNumberOfLocksHeld()); 158 long time1 = System.currentTimeMillis(); 159 int nOps = _add(cache1_, hasTx, oneTxOnly); 160 long time2 = System.currentTimeMillis(); 161 System.out.println("-- after add: number of locks held is " + cache1_.getNumberOfLocksHeld()); 162 double d = (double) (time2 - time1) / nOps; 163 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 164 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 165 " msec."); 166 dumbStr = new StringBuffer (); 167 168 System.out.println("-- before get: number of locks held is " + cache1_.getNumberOfLocksHeld()); 170 time1 = System.currentTimeMillis(); 171 nOps = _get(cache1_, hasTx, oneTxOnly); 172 time2 = System.currentTimeMillis(); 173 System.out.println("-- after get: number of locks held is " + cache1_.getNumberOfLocksHeld()); 174 d = (double) (time2 - time1) / nOps; 175 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 176 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 177 " msec."); 178 dumbStr = new StringBuffer (); 179 180 System.out.println("-- before remove: number of locks held is " + cache1_.getNumberOfLocksHeld()); 182 time1 = System.currentTimeMillis(); 183 nOps = _remove(cache1_, hasTx, oneTxOnly); 184 time2 = System.currentTimeMillis(); 185 System.out.println("-- after remove: number of locks held is " + cache1_.getNumberOfLocksHeld()); 186 d = (double) (time2 - time1) / nOps; 187 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 188 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 189 " msec."); 190 191 destroyCache(cache1_); 192 } 193 194 public void test2CachesTx() throws Exception 195 { 196 log("=== 2 caches with transaction (no concurrent access) ==="); 197 cache1_ = createCache(); 198 cache2_ = createCache(); 199 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 200 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 201 cache1_.startService(); 202 cache2_.startService(); 203 204 DecimalFormat form = new DecimalFormat ("#.00"); 206 FieldPosition fieldPos = new FieldPosition (0); 207 StringBuffer dumbStr = new StringBuffer (); 208 boolean hasTx = true; 209 boolean oneTxOnly = false; 210 211 long time1 = System.currentTimeMillis(); 213 int nOps = _add(cache1_, hasTx, oneTxOnly); 214 long time2 = System.currentTimeMillis(); 215 double d = (double) (time2 - time1) / nOps; 216 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 217 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 218 " msec."); 219 dumbStr = new StringBuffer (); 220 221 time1 = System.currentTimeMillis(); 223 nOps = _get(cache1_, hasTx, oneTxOnly); 224 time2 = System.currentTimeMillis(); 225 d = (double) (time2 - time1) / nOps; 226 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 227 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 228 " msec."); 229 dumbStr = new StringBuffer (); 230 231 time1 = System.currentTimeMillis(); 233 nOps = _remove(cache2_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 235 d = (double) (time2 - time1) / nOps; 236 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 237 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 238 " msec."); 239 240 destroyCache(cache1_); 241 destroyCache(cache2_); 242 } 243 244 public void test2CachesOneTxOnly() throws Exception 245 { 246 log("=== 2 caches with single transaction only (no concurrent access) ==="); 247 cache1_ = createCache(); 248 cache2_ = createCache(); 249 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 250 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 251 cache1_.startService(); 252 cache2_.startService(); 253 254 DecimalFormat form = new DecimalFormat ("#.00"); 256 FieldPosition fieldPos = new FieldPosition (0); 257 StringBuffer dumbStr = new StringBuffer (); 258 boolean hasTx = true; 259 boolean oneTxOnly = true; 260 261 long time1 = System.currentTimeMillis(); 263 int nOps = _add(cache1_, hasTx, oneTxOnly); 264 long time2 = System.currentTimeMillis(); 265 double d = (double) (time2 - time1) / nOps; 266 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 267 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 268 " msec."); 269 dumbStr = new StringBuffer (); 270 271 time1 = System.currentTimeMillis(); 273 nOps = _get(cache1_, hasTx, oneTxOnly); 274 time2 = System.currentTimeMillis(); 275 d = (double) (time2 - time1) / nOps; 276 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 277 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 278 " msec."); 279 dumbStr = new StringBuffer (); 280 281 time1 = System.currentTimeMillis(); 283 nOps = _remove(cache2_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 285 d = (double) (time2 - time1) / nOps; 286 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 287 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 288 " msec."); 289 290 destroyCache(cache1_); 291 destroyCache(cache2_); 292 } 293 294 public void test3CachesTx() throws Exception 295 { 296 log("=== 3 caches with transaction (no concurrent access) ==="); 297 cache1_ = createCache(); 298 cache2_ = createCache(); 299 cache3_ = createCache(); 300 cache1_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 301 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 302 cache2_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 303 cache3_.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 304 cache1_.startService(); 305 cache2_.startService(); 306 cache3_.startService(); 307 308 DecimalFormat form = new DecimalFormat ("#.00"); 310 FieldPosition fieldPos = new FieldPosition (0); 311 StringBuffer dumbStr = new StringBuffer (); 312 boolean hasTx = true; 313 boolean oneTxOnly = false; 314 315 long time1 = System.currentTimeMillis(); 317 int nOps = _add(cache1_, hasTx, oneTxOnly); 318 long time2 = System.currentTimeMillis(); 319 double d = (double) (time2 - time1) / nOps; 320 log("Time elapsed for _add is " + (time2 - time1) + " with " + nOps 321 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 322 " msec."); 323 dumbStr = new StringBuffer (); 324 325 time1 = System.currentTimeMillis(); 327 nOps = _get(cache2_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 329 d = (double) (time2 - time1) / nOps; 330 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 331 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 332 " msec."); 333 dumbStr = new StringBuffer (); 334 335 time1 = System.currentTimeMillis(); 337 nOps = _remove(cache3_, hasTx, oneTxOnly); time2 = System.currentTimeMillis(); 339 d = (double) (time2 - time1) / nOps; 340 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 341 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 342 " msec."); 343 344 destroyCache(cache1_); 345 destroyCache(cache2_); 346 destroyCache(cache3_); 347 } 348 349 private int _add(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception 350 { 351 UserTransaction tx = null; 352 if (hasTx) { 353 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 354 } 355 356 if (hasTx && oneTxOnly) { 357 tx.begin(); 358 } 359 360 for (int i = 0; i < nodeList_.size(); i++) { 361 String key = Integer.toString(i); 362 String value = Integer.toString(i); 363 if (hasTx && !oneTxOnly) { 364 tx.begin(); 365 cache.put((String ) nodeList_.get(i), key, value); 366 tx.commit(); 367 } else { 368 cache.put((String ) nodeList_.get(i), key, value); 369 } 370 } 371 372 if (hasTx && oneTxOnly) { 373 tx.commit(); 374 } 375 376 return nodeList_.size(); 377 } 378 379 private int _get(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception 380 { 381 UserTransaction tx = null; 382 if (hasTx) { 383 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 384 } 385 386 if (hasTx && oneTxOnly) { 387 tx.begin(); 388 } 389 390 for (int i = 0; i < nodeList_.size(); i++) { 391 String key = Integer.toString(i); 392 if (hasTx && !oneTxOnly) { 393 tx.begin(); 394 cache.get((String ) nodeList_.get(i), key); 395 tx.commit(); 396 } else { 397 cache.get((String ) nodeList_.get(i), key); 398 } 399 } 400 401 if (hasTx && oneTxOnly) { 402 tx.commit(); 403 } 404 405 return nodeList_.size(); 406 } 407 408 private int _remove(TreeCache cache, boolean hasTx, boolean oneTxOnly) throws Exception 409 { 410 UserTransaction tx = null; 411 if (hasTx) { 412 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 413 } 414 415 if (hasTx && oneTxOnly) { 416 tx.begin(); 417 } 418 419 for (int i = 0; i < nodeList_.size(); i++) { 420 String key = Integer.toString(i); 421 if (hasTx && !oneTxOnly) { 422 tx.begin(); 423 cache.remove((String ) nodeList_.get(i), key); 424 tx.commit(); 425 } else { 426 cache.remove((String ) nodeList_.get(i), key); 427 } 428 } 429 430 if (hasTx && oneTxOnly) { 431 tx.commit(); 432 } 433 434 return nodeList_.size(); 435 } 436 437 441 private ArrayList nodeGen(int depth, int children) 442 { 443 ArrayList strList = new ArrayList (); 444 ArrayList oldList = new ArrayList (); 445 ArrayList newList = new ArrayList (); 446 447 oldList.add("/"); 448 newList.add("/"); 449 strList.add("/"); 450 451 while (depth > 0) { 452 newList = new ArrayList (); 454 for (int i = 0; i < oldList.size(); i++) { 455 for (int j = 0; j < children; j++) { 456 String tmp = (String ) oldList.get(i); 457 tmp += Integer.toString(j); 458 if (depth != 1) tmp += "/"; 459 newList.add(tmp); 460 } 461 } 462 strList.addAll(newList); 463 oldList = newList; 464 depth--; 465 } 466 467 log("Nodes generated: " + strList.size()); 468 return strList; 469 } 470 471 public static Test suite() throws Exception 472 { 473 return new TestSuite(ReplicatedSyncPerfTestCase.class); 474 } 475 476 private void log(String str) 477 { 478 System.out.println(str); 480 } 481 482 } 483 | Popular Tags |