001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.cache.test;
019:
020: import org.sape.carbon.core.component.Lookup;
021: import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor;
022: import org.sape.carbon.core.exception.ExceptionUtility;
023: import org.sape.carbon.services.cache.Cache;
024: import org.sape.carbon.services.cache.total.TotalCacheConfiguration;
025:
026: import junit.extensions.ActiveTestSuite;
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: /**
032: * <p>Tests the performance of the component with multiple reader
033: * and writer threads. </p>
034: * @since carbon 1.0
035: * @author Nitin Gulati, July 2002
036: * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:08 $)
037: * <br>Copyright 2002 Sapient
038: */
039: public class MultipleReadersWriterPerformanceTest extends TestCase {
040:
041: // No of reader threads
042: public static int READER_THREADS = 2;
043:
044: // No of writer threads
045: public static int WRITER_THREADS = 0;
046:
047: // No of reads on any Functional Interface of the component.
048: public static long NUM_READS = 10000;
049:
050: // Time in millesecs after which writer threads wakes up.
051: public static long WRITER_THREAD_FREEQUENCY = 200;
052:
053: // Time in millesecs between starts of read threads.
054: public static int READ_THREADS_INTERVAL = 1;
055:
056: // Component to be looked up.
057: public static String COMPONENT_NAME = "/cache/test/testTotalCache";
058:
059: public static class Reader implements Runnable {
060:
061: public void run() {
062: // Initialize the cache
063: Cache localCache = (Cache) Lookup.getInstance()
064: .fetchComponent(COMPONENT_NAME);
065:
066: long start = System.currentTimeMillis();
067:
068: // Start run
069: for (long i = 0; i < NUM_READS; ++i) {
070: Integer key = new Integer(
071: (int) (Math.random() * CacheServiceTest.CACHE_SIZE));
072: Object value = localCache.get(key);
073: if (!getCacheValue(key).equals(value)) {
074: // do nothing...
075: }
076: }
077:
078: long end = System.currentTimeMillis();
079: // System.out.println("Thread # " +
080: // Thread.currentThread().getName() +
081: // " Completed " + NUM_READS + " in " + (end-start) +
082: // " millisecs" );
083: }
084:
085: }
086:
087: public static class Writer implements Runnable {
088:
089: public void run() {
090: ConfigurationInterceptor assist = (ConfigurationInterceptor) Lookup
091: .getInstance().fetchComponent(COMPONENT_NAME);
092:
093: TotalCacheConfiguration config = (TotalCacheConfiguration) assist
094: .getWorkingConfiguration();
095:
096: while (true) {
097: config.setComponentDescription("changeDescription");
098: assist.applyConfiguration();
099: try {
100: Thread.currentThread().sleep(
101: WRITER_THREAD_FREEQUENCY);
102: } catch (InterruptedException ie) {
103: }
104: }
105: }
106: }
107:
108: public static String getCacheValue(Integer i) {
109: return "String " + i.toString();
110: }
111:
112: public void executeTest(int readers, int writers) {
113: Thread readerThreads[] = new Thread[readers];
114: Thread writerThreads[] = new Thread[writers];
115:
116: Reader reader = new Reader();
117: Writer writer = new Writer();
118:
119: System.out.println("Starting " + readers
120: + " reader threads and " + writers + " writer threads");
121: long start = System.currentTimeMillis();
122: for (int i = 0; i < writers; i++) {
123: writerThreads[i] = new Thread(writer);
124: writerThreads[i].setName("Writer thread #" + i);
125: writerThreads[i].start();
126: }
127: for (int j = 0; j < readers; j++) {
128: readerThreads[j] = new Thread(reader);
129: readerThreads[j].setName("reader thread #" + j);
130: //Thread.currentThread().sleep(READ_THREADS_INTERVAL);
131: readerThreads[j].start();
132: }
133:
134: //Wait for all threads
135: for (int i = 0; i < writers; i++) {
136: try {
137: writerThreads[i].join();
138: } catch (InterruptedException ie) {
139: System.out.println(ExceptionUtility
140: .printStackTracesToString(ie));
141: }
142: }
143: for (int i = 0; i < readers; i++) {
144: try {
145: readerThreads[i].join();
146: } catch (InterruptedException ie) {
147: System.out.println(ExceptionUtility
148: .printStackTracesToString(ie));
149: }
150: }
151: System.out.println("Total with " + readers + "readers & "
152: + writers + "writers: elapsed time ["
153: + (System.currentTimeMillis() - start) + "]");
154: }
155:
156: public static void main(String args[]) throws Exception {
157:
158: }
159:
160: public void runTest() {
161: this .executeTest(1, 0);
162: this .executeTest(10, 0);
163: this .executeTest(20, 0);
164: this .executeTest(30, 0);
165: this .executeTest(40, 0);
166: this .executeTest(50, 0);
167: this .executeTest(100, 0);
168:
169: // this.executeTest(1,1);
170: // this.executeTest(10, 1);
171: }
172:
173: public MultipleReadersWriterPerformanceTest(String name) {
174: super (name);
175: }
176:
177: /*
178: * write your test methods here following these examples:
179: *
180: * public void testFunction1() {
181: * test something
182: * }
183: *
184: * public void testFunction2() {
185: * test something else
186: * }
187: */
188:
189: /**
190: * Method called by jUnit to get all the tests in this test case.
191: * @return Test the suite of tests in this test case
192: */
193: public static Test suite() {
194: TestSuite masterSuite = new TestSuite();
195:
196: // add single threaded tests
197: Test singleThreadedTests = getSingleThreadedTests();
198: if (singleThreadedTests != null) {
199: masterSuite.addTest(singleThreadedTests);
200: }
201:
202: // add multi threaded tests
203: Test multiThreadedTests = getMultiThreadedTests();
204: if (multiThreadedTests != null) {
205: masterSuite.addTest(multiThreadedTests);
206: }
207:
208: return masterSuite;
209: }
210:
211: /**
212: * This method is used within the suite method to get all of the single
213: * threaded tests.
214: *
215: * Add all your single threaded tests in this method with a line like:
216: * suite.addTest(new CachPerformanceTest("testFunction1"));
217: *
218: * @return Test the suite of single threaded tests in this test case
219: */
220: private static Test getSingleThreadedTests() {
221: TestSuite suite = new TestSuite();
222: /*
223: * add your tests here following these examples:
224: *
225: * suite.addTest(new CachPerformanceTest("testFunction1"));
226: * suite.addTest(new CachPerformanceTest("testFunction2"));
227: */
228: suite.addTest(new MultipleReadersWriterPerformanceTest(
229: "runTest"));
230: return suite;
231: }
232:
233: /**
234: * This method is used within the suite method to get all of the multi
235: * threaded tests.
236: *
237: * Add all your multi threaded tests in this method with a line like:
238: * addTest(suite, "testFunction1", 5);
239: *
240: * @return Test the suite of multi-threaded tests in this test case
241: */
242: private static Test getMultiThreadedTests() {
243: TestSuite suite = new ActiveTestSuite();
244: /*
245: * add your tests here following these examples:
246: *
247: * addTest(suite, "testFunction1", 5);
248: * addTest(suite, "testFunction2", 10);
249: */
250: return suite;
251: }
252:
253: /**
254: * This method will add the give test to the give suite the specified
255: * number of times. This is best used for multi-threaded tests where
256: * suite is an instance of ActiveTestSuite and you want to run the same
257: * test in multiple threads.
258: *
259: * @param suite the suite to add the test to.
260: * @param testName the name of the test to add.
261: * @param number the number of times to add the test to the suite
262: */
263: private static void addTest(TestSuite suite, String testName,
264: int number) {
265: for (int count = 0; count < number; count++) {
266: suite.addTest(new MultipleReadersWriterPerformanceTest(
267: testName));
268: }
269: }
270: }
|