1 64 65 package com.jcorporate.expresso.core.cache; 66 67 import com.jcorporate.expresso.services.test.ExpressoTestCase; 68 import com.jcorporate.expresso.services.test.TestSystemInitializer; 69 import junit.framework.Test; 70 import junit.framework.TestSuite; 71 import org.apache.log4j.Logger; 72 73 import java.util.Date ; 74 import java.util.Enumeration ; 75 import java.util.Vector ; 76 77 78 86 public class CacheStressTest 87 extends ExpressoTestCase { 88 89 private static final Logger log = Logger.getLogger(CacheStressTest.class); 90 91 private String cacheName = "testcache"; 92 93 97 final int[] numThreads = {5, 10, 25, 50, 100, 200}; 98 101 106 public CacheStressTest(String name) 107 throws Exception { 108 super(name); 109 } 110 111 112 public static void main(String [] args) 113 throws Exception { 114 115 junit.textui.TestRunner.run(suite()); 117 } 118 119 123 public void setUp() 124 throws Exception { 125 try { 126 CacheManager.createCache(TestSystemInitializer.getTestContext(), cacheName, false); 127 128 129 CacheManager.createCache(TestSystemInitializer.getTestContext(), cacheName + "O", true); 130 } catch (java.util.ConcurrentModificationException cme) { 131 log.error("Error Setting Up", cme); 132 throw cme; 133 } 134 } 135 136 137 141 public void tearDown() 142 throws Exception { 143 try { 144 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName); 145 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName + "O"); 146 } catch (java.util.ConcurrentModificationException cme) { 147 log.error("Error Tearing Down", cme); 148 throw cme; 149 } 150 } 151 152 153 158 public static Test suite() 159 throws Exception { 160 TestSuite suite = new TestSuite(CacheStressTest.class); 161 162 return suite; 163 } 164 165 166 171 public int countTestCases() { 172 return 1; 173 } 174 175 178 public void testMultiThreaded() { 179 Vector myThreads = new Vector (); 180 Thread oneTest = null; 181 long startTime = new Date ().getTime(); 182 183 for (int j = 0; j < numThreads.length; j++) { 184 try { 185 if (myThreads.size() < numThreads[j]) { 186 for (int i = myThreads.size(); i < numThreads[j]; i++) { 187 myThreads.addElement(new CacheTestThread("" + i)); 188 } 189 } else { 190 myThreads.setSize(numThreads[j]); 191 } 192 193 for (Enumeration ee = myThreads.elements(); 194 ee.hasMoreElements();) { 195 oneTest = (Thread ) ee.nextElement(); 196 oneTest.start(); 197 } 198 199 for (Enumeration ee = myThreads.elements(); 200 ee.hasMoreElements();) { 201 oneTest = (Thread ) ee.nextElement(); 202 203 try { 204 oneTest.join(); 205 } catch (InterruptedException ie) { 206 oneTest.interrupt(); 207 208 while (ee.hasMoreElements()) { 209 oneTest = (Thread ) ee.nextElement(); 210 oneTest.interrupt(); 211 } 212 213 throw new CacheException(ie); 214 } 215 } 216 217 log.info("All threaded tests completed"); 218 } catch (CacheException ce) { 219 log.error(ce); 220 fail("CacheException occurred in multi-threaded test - see log"); 221 } catch (java.util.ConcurrentModificationException cme) { 222 log.error("Error testing simple caching", cme); 223 throw cme; 224 } 225 226 long endTime = new Date ().getTime(); 227 System.out.println("\nCacheTest: Total Time for Multi-threaded test: " + 228 Long.toString(endTime - startTime) + 229 " ms. Num Threads = " + Integer.toString(numThreads[j])); 230 } 231 } 232 233 } | Popular Tags |