1 8 9 package org.jboss.test.cache.test.eviction; 10 11 import java.util.Random ; 12 13 import junit.framework.Test; 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 import org.apache.log4j.Logger; 18 import org.jboss.cache.Fqn; 19 import org.jboss.cache.PropertyConfigurator; 20 import org.jboss.cache.TreeCache; 21 import org.jboss.cache.eviction.RegionManager; 22 28 public final class ConcurrentEvictAndRemoveTestCase extends TestCase 29 { 30 private static final Logger logger = Logger.getLogger(ConcurrentEvictAndRemoveTestCase.class.getName()); 31 private TreeCache cache; 32 33 34 static void log(String msg) { 35 System.out.println(msg); 36 logger.info(msg); 37 } 38 39 void err(String msg) { 40 System.err.println(msg); 41 logger.error(msg); 42 } 43 44 public ConcurrentEvictAndRemoveTestCase(String name) 45 { 46 super(name); 47 } 48 49 public void setUp() throws Exception 50 { 51 cache = new TreeCache(); 52 cache.setCacheMode(TreeCache.LOCAL); 53 PropertyConfigurator config = new PropertyConfigurator(); 54 config.configure(cache, "META-INF/local-eviction-service.xml"); 55 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 56 cache.start(); 57 } 58 public void tearDown() throws Exception 59 { 60 cache.stop(); 61 } 62 63 public void test() throws Exception 64 { 65 final long MAX_LOOP_TIME = 4000; int numberOfInitialCreatedNodes = 0; 67 68 log("*** Start Test: ConcurrentEvictAndRemoveTest ***"); 69 70 { 72 final StopWatch stopWatch = new StopWatch(); 73 int i = 0; 74 while(true) { 75 i++; 76 final Fqn fqn = Fqn.fromString("/test/item_" + i); 77 cache.put(fqn, "att", null); 78 if (stopWatch.getTime()>MAX_LOOP_TIME) { 79 break; 80 } 81 } 82 numberOfInitialCreatedNodes = i; 83 log( 84 "nodes created: " 85 + numberOfInitialCreatedNodes 86 + " time: " 87 + stopWatch.getTime() 88 + " ms" 89 90 ); 91 } 92 93 { 95 int numberCacheHitSuccess = 0; 96 int numberCacheHitFailure = 0; 97 final Random random = new Random (); 98 final StopWatch stopWatch = new StopWatch(); 99 100 while(true) { 101 final Fqn fqn = Fqn.fromString("/test/item_" + random.nextInt(numberOfInitialCreatedNodes)); 102 if (cache.exists(fqn)) { 103 cache.remove(fqn); 104 numberCacheHitSuccess++; 105 } else { 106 numberCacheHitFailure++; 107 } 108 if (stopWatch.getTime()>MAX_LOOP_TIME) { 109 break; 110 } 111 } 112 log( 113 "nodes removed: " 114 + numberCacheHitSuccess 115 + ", total cache hits: " 116 + (numberCacheHitFailure + numberCacheHitSuccess) 117 + " time: " 118 + stopWatch.getTime() 119 + " ms" 120 ); 121 } 122 123 { 125 log("create put/remove events"); 126 final int SEQUENCE_SIZE = 10000; final long TIMEOUT_DEFAULT = 100000; 128 final int MAX_TIMEOUT_FACTOR = 10; 129 130 for(int i = 0; i < RegionManager.CAPACITY; i = i + SEQUENCE_SIZE) { 131 final int sequenceStart = i; 132 final int sequenceEnd = i + SEQUENCE_SIZE -1; 133 134 Thread thread = new Thread ( 135 new HelperThread(cache, sequenceStart, sequenceEnd) 136 ); 137 138 long timeout = (HelperThread.getTime()==HelperThread.EXECUTION_TIME_NOT_INITIALIZED)? 139 TIMEOUT_DEFAULT: 140 HelperThread.getTime()*MAX_TIMEOUT_FACTOR; 141 142 thread.start(); 143 try { 144 thread.join(timeout); 145 if (thread.isAlive()) { 146 err("Timeout of " + timeout + " ms exceeded for sequence of put/remove operations."); 148 fail("Timeout of " + timeout + " ms exceeded for sequence of put/remove operations."); 149 } else { 150 } 152 } 153 catch(InterruptedException ignore) { 154 err("Thread join interrupted."); 155 } 156 } 157 } 158 log("*** Successfully finished! ***"); 159 } 160 public static Test suite() throws Exception 161 { 162 return new TestSuite(ConcurrentEvictAndRemoveTestCase.class); 163 } 164 165 private static final class StopWatch { 167 public StopWatch() { 168 startTime = System.currentTimeMillis() ; 169 } 170 private final long startTime; 173 public String toString() { 174 return String.valueOf(getTime()); 175 } 176 public long getTime() { 177 return System.currentTimeMillis() - startTime; 178 } 179 } 180 private static final class HelperThread implements Runnable { 181 private static final Logger logger = Logger.getLogger(HelperThread.class.getName()); 182 private final TreeCache cache; 183 private final int start; 184 private final int end; 185 public static final long EXECUTION_TIME_NOT_INITIALIZED = -1; 187 private static long previousExceutionTime = EXECUTION_TIME_NOT_INITIALIZED; 188 private static final String FQN_PREFIX = "/test/overflow-item_"; 189 190 public HelperThread ( TreeCache cache, int start, int end) { 191 this.cache = cache; 192 this.start = start; 193 this.end = end; 194 } 195 public void run() { 196 StopWatch stopWatch = new StopWatch(); 197 log( 198 "process node range: " 199 + this.start 200 + "/" 201 + this.end 202 ); 203 try { 204 for (int i = start; i<=end; i++) { 205 final Fqn fqn = Fqn.fromString(FQN_PREFIX + i); 206 cache.put(fqn, "att", null); 207 cache.remove(fqn); 208 } 210 } catch (Exception e) { 211 System.out.println("exception: " + e); 212 if ( e instanceof RuntimeException ) { 213 throw (RuntimeException )e; 214 } else { 215 throw new RuntimeException (e); 216 } 217 } 218 previousExceutionTime = stopWatch.getTime(); 219 log( 220 "time: " 221 + previousExceutionTime 222 + " ms" 223 ); 224 } 225 public static long getTime() { 226 return previousExceutionTime; 227 } 228 } 229 } 230 | Popular Tags |