1 2 18 19 package org.sape.carbon.services.cache.test; 20 21 import org.sape.carbon.core.component.Lookup; 22 import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor; 23 import org.sape.carbon.core.exception.ExceptionUtility; 24 import org.sape.carbon.services.cache.Cache; 25 import org.sape.carbon.services.cache.total.TotalCacheConfiguration; 26 27 import junit.extensions.ActiveTestSuite; 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 40 public class MultipleReadersWriterPerformanceTest extends TestCase { 41 42 public static int READER_THREADS = 2; 44 45 public static int WRITER_THREADS = 0; 47 48 public static long NUM_READS = 10000; 50 51 public static long WRITER_THREAD_FREEQUENCY = 200; 53 54 public static int READ_THREADS_INTERVAL = 1; 56 57 public static String COMPONENT_NAME = "/cache/test/testTotalCache"; 59 60 public static class Reader implements Runnable { 61 62 public void run() { 63 Cache localCache = 65 (Cache) Lookup.getInstance(). 66 fetchComponent(COMPONENT_NAME); 67 68 long start = System.currentTimeMillis(); 69 70 for (long i=0; i<NUM_READS; ++i) { 72 Integer key = new Integer ( 73 (int) (Math.random() * CacheServiceTest.CACHE_SIZE)); 74 Object value = localCache.get(key); 75 if(!getCacheValue(key).equals(value)) { 76 } 78 } 79 80 long end = System.currentTimeMillis(); 81 } 86 87 } 88 89 public static class Writer implements Runnable { 90 91 public void run() { 92 ConfigurationInterceptor assist = 93 (ConfigurationInterceptor) Lookup.getInstance(). 94 fetchComponent(COMPONENT_NAME); 95 96 TotalCacheConfiguration config = 97 (TotalCacheConfiguration) assist.getWorkingConfiguration(); 98 99 while(true) { 100 config.setComponentDescription("changeDescription"); 101 assist.applyConfiguration(); 102 try { 103 Thread.currentThread( 104 ).sleep(WRITER_THREAD_FREEQUENCY); 105 } catch(InterruptedException ie) { } 106 } 107 } 108 } 109 110 public static String getCacheValue(Integer i) { 111 return "String " + i.toString(); 112 } 113 114 public void executeTest(int readers, int writers) { 115 Thread readerThreads[] = new Thread [readers]; 116 Thread writerThreads[] = new Thread [writers]; 117 118 Reader reader = new Reader(); 119 Writer writer = new Writer(); 120 121 System.out.println("Starting " + readers + " reader threads and " 122 + writers + " writer threads" ); 123 long start = System.currentTimeMillis(); 124 for(int i=0;i<writers;i++) { 125 writerThreads[i] = new Thread (writer); 126 writerThreads[i].setName("Writer thread #" + i); 127 writerThreads[i].start(); 128 } 129 for(int j=0;j<readers;j++) { 130 readerThreads[j] = new Thread (reader); 131 readerThreads[j].setName("reader thread #" + j); 132 readerThreads[j].start(); 134 } 135 136 for(int i=0;i<writers;i++) { 138 try { 139 writerThreads[i].join(); 140 } catch (InterruptedException ie) { 141 System.out.println(ExceptionUtility.printStackTracesToString(ie)); 142 } 143 } 144 for(int i=0;i<readers;i++) { 145 try { 146 readerThreads[i].join(); 147 } catch (InterruptedException ie) { 148 System.out.println(ExceptionUtility.printStackTracesToString(ie)); 149 } 150 } 151 System.out.println("Total with " + readers + "readers & " + writers + "writers: elapsed time [" + (System.currentTimeMillis() - start) + "]"); 152 } 153 154 155 public static void main(String args[]) throws Exception { 156 157 } 158 159 public void runTest() { 160 this.executeTest(1, 0); 161 this.executeTest(10, 0); 162 this.executeTest(20,0); 163 this.executeTest(30,0); 164 this.executeTest(40,0); 165 this.executeTest(50,0); 166 this.executeTest(100,0); 167 168 } 171 172 173 public MultipleReadersWriterPerformanceTest(String name) { 174 super(name); 175 } 176 177 188 189 193 public static Test suite() { 194 TestSuite masterSuite = new TestSuite(); 195 196 Test singleThreadedTests = getSingleThreadedTests(); 198 if(singleThreadedTests != null) { 199 masterSuite.addTest(singleThreadedTests); 200 } 201 202 Test multiThreadedTests = getMultiThreadedTests(); 204 if(multiThreadedTests != null) { 205 masterSuite.addTest(multiThreadedTests); 206 } 207 208 return masterSuite; 209 } 210 211 220 private static Test getSingleThreadedTests() { 221 TestSuite suite = new TestSuite(); 222 228 suite.addTest(new MultipleReadersWriterPerformanceTest("runTest")); 229 return suite; 230 } 231 232 241 private static Test getMultiThreadedTests() { 242 TestSuite suite = new ActiveTestSuite(); 243 249 return suite; 250 } 251 252 262 private static void addTest(TestSuite suite, String testName, int number) { 263 for(int count=0; count<number; count++) { 264 suite.addTest(new MultipleReadersWriterPerformanceTest(testName)); 265 } 266 } 267 } 268 269 270 | Popular Tags |