KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > cache > test > MultipleReadersWriterPerformanceTest


1
2 /*
3  * The contents of this file are subject to the Sapient Public License
4  * Version 1.0 (the "License"); you may not use this file except in compliance
5  * with the License. You may obtain a copy of the License at
6  * http://carbon.sf.net/License.html.
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
10  * the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is The Carbon Component Framework.
13  *
14  * The Initial Developer of the Original Code is Sapient Corporation
15  *
16  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
17  */

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 /**
33  * <p>Tests the performance of the component with multiple reader
34  * and writer threads. </p>
35  * @since carbon 1.0
36  * @author Nitin Gulati, July 2002
37  * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:08 $)
38  * <br>Copyright 2002 Sapient
39  */

40 public class MultipleReadersWriterPerformanceTest extends TestCase {
41
42     // No of reader threads
43
public static int READER_THREADS = 2;
44
45     // No of writer threads
46
public static int WRITER_THREADS = 0;
47
48     // No of reads on any Functional Interface of the component.
49
public static long NUM_READS = 10000;
50
51     // Time in millesecs after which writer threads wakes up.
52
public static long WRITER_THREAD_FREEQUENCY = 200;
53
54     // Time in millesecs between starts of read threads.
55
public static int READ_THREADS_INTERVAL = 1;
56
57     // Component to be looked up.
58
public static String JavaDoc COMPONENT_NAME = "/cache/test/testTotalCache";
59
60     public static class Reader implements Runnable JavaDoc {
61
62         public void run() {
63             // Initialize the cache
64
Cache localCache =
65                 (Cache) Lookup.getInstance().
66                 fetchComponent(COMPONENT_NAME);
67
68             long start = System.currentTimeMillis();
69
70             // Start run
71
for (long i=0; i<NUM_READS; ++i) {
72                 Integer JavaDoc key = new Integer JavaDoc(
73                 (int) (Math.random() * CacheServiceTest.CACHE_SIZE));
74                 Object JavaDoc value = localCache.get(key);
75                 if(!getCacheValue(key).equals(value)) {
76                     // do nothing...
77
}
78             }
79
80             long end = System.currentTimeMillis();
81 // System.out.println("Thread # " +
82
// Thread.currentThread().getName() +
83
// " Completed " + NUM_READS + " in " + (end-start) +
84
// " millisecs" );
85
}
86
87     }
88
89     public static class Writer implements Runnable JavaDoc {
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 JavaDoc ie) { }
106             }
107         }
108     }
109
110     public static String JavaDoc getCacheValue(Integer JavaDoc i) {
111         return "String " + i.toString();
112     }
113
114     public void executeTest(int readers, int writers) {
115         Thread JavaDoc readerThreads[] = new Thread JavaDoc[readers];
116         Thread JavaDoc writerThreads[] = new Thread JavaDoc[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 JavaDoc(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 JavaDoc(reader);
131             readerThreads[j].setName("reader thread #" + j);
132             //Thread.currentThread().sleep(READ_THREADS_INTERVAL);
133
readerThreads[j].start();
134         }
135
136         //Wait for all threads
137
for(int i=0;i<writers;i++) {
138             try {
139                 writerThreads[i].join();
140             } catch (InterruptedException JavaDoc 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 JavaDoc 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 JavaDoc args[]) throws Exception JavaDoc {
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 // this.executeTest(1,1);
169
// this.executeTest(10, 1);
170
}
171
172
173     public MultipleReadersWriterPerformanceTest(String JavaDoc 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("runTest"));
229         return suite;
230     }
231
232     /**
233      * This method is used within the suite method to get all of the multi
234      * threaded tests.
235      *
236      * Add all your multi threaded tests in this method with a line like:
237      * addTest(suite, "testFunction1", 5);
238      *
239      * @return Test the suite of multi-threaded tests in this test case
240      */

241     private static Test getMultiThreadedTests() {
242         TestSuite suite = new ActiveTestSuite();
243         /*
244          * add your tests here following these examples:
245          *
246          * addTest(suite, "testFunction1", 5);
247          * addTest(suite, "testFunction2", 10);
248          */

249         return suite;
250     }
251
252     /**
253      * This method will add the give test to the give suite the specified
254      * number of times. This is best used for multi-threaded tests where
255      * suite is an instance of ActiveTestSuite and you want to run the same
256      * test in multiple threads.
257      *
258      * @param suite the suite to add the test to.
259      * @param testName the name of the test to add.
260      * @param number the number of times to add the test to the suite
261      */

262     private static void addTest(TestSuite suite, String JavaDoc 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