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 LocalStressTestCase 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 = 50; 51 static Exception thread_ex=null; 52 53 static 54 { 55 p_ = new Properties (); 56 p_.put(Context.INITIAL_CONTEXT_FACTORY, 57 "org.jboss.cache.transaction.DummyContextFactory"); 58 } 59 60 public LocalStressTestCase(String name) 61 { 62 super(name); 63 } 64 65 public void setUp() throws Exception 66 { 67 super.setUp(); 68 69 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 70 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 71 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); t2.join(60000); 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 try { 168 _run(); 169 } 170 catch(Exception e) { 171 thread_ex=e; 172 } 173 } 174 175 public void _run() throws HeuristicMixedException, SystemException, 176 NotSupportedException, HeuristicRollbackException, RollbackException, CacheException { 177 for (int loop = 0; loop < MAX_LOOP; loop++) { 178 sleep_(random_.nextInt(50)); 179 log("Executing op1. Loop-" + loop); 180 op1(); 181 sleep_(random_.nextInt(50)); 182 log("Executing op2..."); 183 op2(); 184 sleep_(random_.nextInt(50)); 185 log("Executing op3..."); 186 op3(); 187 } 188 } 189 190 private void op1() throws SystemException, NotSupportedException, CacheException, 192 HeuristicMixedException, HeuristicRollbackException, RollbackException { 193 int i = random_.nextInt(nodeList_.size() - 1); 194 String key = Integer.toString(i); 195 String value = Integer.toString(i); 196 197 tx_.begin(); 198 String node = (String ) nodeList_.get(i); 199 cache_.get(node, key); 200 sleep_(random_.nextInt(SLEEP_TIME)); cache_.put(node, key, value); 202 sleep_(random_.nextInt(SLEEP_TIME)); cache_.remove(node, key); 204 tx_.commit(); 205 } 206 207 private void op2() throws SystemException, NotSupportedException, CacheException, 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("/"); 258 newList.add("/"); 259 strList.add("/"); 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(LocalStressTestCase.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 |