1 8 9 package org.jboss.test.cache.stress; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.jboss.cache.CacheException; 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.*; 24 import java.util.ArrayList ; 25 import java.util.Properties ; 26 import java.util.Random ; 27 28 31 37 public class EvictionLocalStressTestCase extends TestCase 38 { 39 static TreeCache cache_; 40 int cachingMode_ = TreeCache.LOCAL; 41 final static Properties p_; 42 String oldFactory_ = null; 44 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 45 static ArrayList nodeList_; 46 static final int depth_ = 4; 47 static final int children_ = 4; 48 DummyTransactionManager tm_; 49 static final int MAX_LOOP = 100; 50 static final int SLEEP_TIME = 20; 51 static final String ROOT = "/stress/"; 52 53 static Exception thread_ex; 54 55 static 56 { 57 p_ = new Properties (); 58 p_.put(Context.INITIAL_CONTEXT_FACTORY, 59 "org.jboss.cache.transaction.DummyContextFactory"); 60 } 61 62 public EvictionLocalStressTestCase(String name) 63 { 64 super(name); 65 } 66 67 public void setUp() throws Exception 68 { 69 super.setUp(); 70 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 71 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 72 DummyTransactionManager.getInstance(); 73 initCaches(TreeCache.LOCAL); 74 nodeList_ = nodeGen(depth_, children_); 75 tm_ = new DummyTransactionManager(); 76 77 log("LocalStressTestCase: cacheMode=TRANSIENT, one cache"); 78 } 79 80 public void tearDown() throws Exception 81 { 82 super.tearDown(); 83 thread_ex=null; 84 DummyTransactionManager.destroy(); 85 destroyCaches(); 86 87 if (oldFactory_ != null) { 88 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 89 oldFactory_ = null; 90 } 91 92 } 93 94 void initCaches(int caching_mode) throws Exception 95 { 96 cachingMode_ = caching_mode; 97 cache_ = new TreeCache(); 98 PropertyConfigurator config = new PropertyConfigurator(); 99 config.configure(cache_, "META-INF/local-eviction-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 101 cache_.createService(); 102 cache_.startService(); 103 } 104 105 void destroyCaches() throws Exception 106 { 107 cache_.stopService(); 108 cache_ = null; 109 } 110 111 protected void setLevelRW() 112 { 113 log("set lock level to RWUpgrade ..."); 114 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 115 } 116 117 protected void setLevelSerial() 118 { 119 log("set lock level to SimpleLock ..."); 120 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 121 } 122 123 public void testAllTx_RWLock() throws Exception 124 { 125 setLevelRW(); 126 allTx(); 127 } 128 129 public void XtestAllTx_SimpleLock() throws Exception 130 { 131 setLevelSerial(); 132 allTx(); 133 } 134 135 private void allTx() throws Exception 136 { 137 UserTransaction tx = null; 138 tx = (UserTransaction) new InitialContext (p_).lookup("UserTransaction"); 139 140 RunThread t1 = new RunThread(tx, 1); 141 RunThread t2 = new RunThread(tx, 2); 142 143 t1.start(); 144 t2.start(); 145 146 t1.join(60000); 147 t2.join(60000); 148 149 if(thread_ex != null) 150 throw thread_ex; 151 } 152 153 static class RunThread extends Thread 154 { 155 final int seed_; 156 Random random_; 157 UserTransaction tx_ = null; 158 159 public RunThread(UserTransaction tx, int seed) 160 { 161 seed_ = seed; 162 random_ = new Random (seed); 163 tx_ = tx; 164 } 165 166 public void run() 167 { 168 try { 169 _run(); 170 } 171 catch(Exception ex) { 172 thread_ex=ex; 173 } 174 } 175 176 public void _run() throws CacheException, HeuristicMixedException, 177 SystemException, NotSupportedException, HeuristicRollbackException, RollbackException { 178 for(int outer = 0; outer < 4; outer++) { 179 for (int loop = 0; loop < MAX_LOOP; loop++) { 180 sleep_(random_.nextInt(SLEEP_TIME)); 181 log("Executing op1. Loop-" + loop); 182 op1(); 183 sleep_(random_.nextInt(SLEEP_TIME)); 184 log("Executing op2..."); 185 op2(); 186 sleep_(random_.nextInt(SLEEP_TIME)); 187 log("Executing op3..."); 188 op3(); 189 } 190 } 191 } 192 193 private void op1() throws CacheException { 195 int i = random_.nextInt(nodeList_.size() - 1); 196 String key = Integer.toString(i); 197 String value = Integer.toString(i); 198 199 String node = (String ) nodeList_.get(i); 201 cache_.get(node, key); 202 sleep_(random_.nextInt(SLEEP_TIME)); cache_.put(node, key, value); 204 } 206 207 private void op2() throws CacheException, SystemException, NotSupportedException, 209 HeuristicMixedException, HeuristicRollbackException, RollbackException { 210 int i = random_.nextInt(nodeList_.size() - 1); 211 String key = Integer.toString(i); 212 213 tx_.begin(); 214 String node = (String ) nodeList_.get(i); 215 cache_.get(node, key); 216 sleep_(random_.nextInt(SLEEP_TIME)); cache_.get(node, key); 218 sleep_(random_.nextInt(SLEEP_TIME)); tx_.commit(); 220 } 221 222 private void op3() throws SystemException, NotSupportedException, CacheException, 224 HeuristicMixedException, HeuristicRollbackException, RollbackException { 225 int i = random_.nextInt(nodeList_.size() - 1); 226 String key = Integer.toString(i); 227 String value = Integer.toString(i); 228 229 tx_.begin(); 230 String node = (String ) nodeList_.get(i); 231 cache_.put(node, key, value); 232 sleep_(random_.nextInt(SLEEP_TIME)); cache_.remove(node, key); 234 tx_.commit(); 235 } 236 237 private void sleep_(long msecs) 238 { 239 try { 240 Thread.sleep(msecs); 241 } catch (Exception ex) { 242 } 243 } 244 } 245 246 251 private ArrayList nodeGen(int depth, int children) 252 { 253 ArrayList strList = new ArrayList (); 254 ArrayList oldList = new ArrayList (); 255 ArrayList newList = new ArrayList (); 256 257 oldList.add(ROOT); 258 newList.add(ROOT); 259 strList.add(ROOT); 260 261 while (depth > 0) { 262 newList = new ArrayList (); 264 for (int i = 0; i < oldList.size(); i++) { 265 for (int j = 0; j < children; j++) { 266 String tmp = (String ) oldList.get(i); 267 tmp += Integer.toString(j); 268 if (depth != 1) 269 tmp += "/"; 270 newList.add(tmp); 271 } 272 } 273 strList.addAll(newList); 274 oldList = newList; 275 depth--; 276 } 277 278 log("Nodes generated: " + strList.size()); 279 return strList; 280 } 281 282 public static Test suite() throws Exception 283 { 284 return new TestSuite(EvictionLocalStressTestCase.class); 285 } 286 287 private static void log(String str) 288 { 289 System.out.println("Thread: " + Thread.currentThread() + ": " + str); 290 } 292 293 } 294 | Popular Tags |