001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.cache;
066:
067: import com.jcorporate.expresso.services.test.ExpressoTestCase;
068: import com.jcorporate.expresso.services.test.TestSystemInitializer;
069: import junit.framework.Test;
070: import junit.framework.TestSuite;
071: import org.apache.log4j.Logger;
072:
073: import java.util.Date;
074: import java.util.Enumeration;
075: import java.util.Vector;
076:
077: /**
078: * Cache Stress test. Scales the number of threads accessing the cache
079: * at a particular time.
080: *
081: * @author Michael Nash
082: * @author Michael Rimov
083: * @version $Revision: 1.3 $ on $Date: 2004/11/17 20:48:22 $
084: */
085: public class CacheStressTest extends ExpressoTestCase {
086:
087: private static final Logger log = Logger
088: .getLogger(CacheStressTest.class);
089:
090: private String cacheName = "testcache";
091:
092: /**
093: * Each index in the array represents one suite of test with the
094: * specified number of threads.
095: */
096: final int[] numThreads = { 5, 10, 25, 50, 100, 200 };
097:
098: // int[] numThreads = {10,25}; //Use this line if you're profiling and just want
099: // an idea of what's going on.
100:
101: /**
102: * Constructs a test case with the given name.
103: *
104: * @param name The test name
105: */
106: public CacheStressTest(String name) throws Exception {
107: super (name);
108: } /* CacheTest(String) */
109:
110: public static void main(String[] args) throws Exception {
111:
112: //Set the system properties we need
113: junit.textui.TestRunner.run(suite());
114: }
115:
116: /**
117: * Sets up the fixture, for example, open a network connection.
118: * This method is called before a test is executed.
119: */
120: public void setUp() throws Exception {
121: try {
122: CacheManager.createCache(TestSystemInitializer
123: .getTestContext(), cacheName, false);
124:
125: /* Create an ordered cache as well */
126: CacheManager.createCache(TestSystemInitializer
127: .getTestContext(), cacheName + "O", true);
128: } catch (java.util.ConcurrentModificationException cme) {
129: log.error("Error Setting Up", cme);
130: throw cme;
131: }
132: } /* setUp() */
133:
134: /**
135: * Tears down the fixture, for example, close a network connection.
136: * This method is called after a test is executed.
137: */
138: public void tearDown() throws Exception {
139: try {
140: CacheManager.clear(TestSystemInitializer.getTestContext(),
141: cacheName);
142: CacheManager.clear(TestSystemInitializer.getTestContext(),
143: cacheName + "O");
144: } catch (java.util.ConcurrentModificationException cme) {
145: log.error("Error Tearing Down", cme);
146: throw cme;
147: }
148: } /* tearDown() */
149:
150: /**
151: * Define the suite of tests that verify each function of the cache
152: *
153: * @return junit.framework.TestSuite
154: */
155: public static Test suite() throws Exception {
156: TestSuite suite = new TestSuite(CacheStressTest.class);
157:
158: return suite;
159: } /* suite() */
160:
161: /**
162: * Counts the number of test cases executed by run(TestResult result).
163: *
164: * @return number of test cases: in this case 1
165: */
166: public int countTestCases() {
167: return 1;
168: } /* countTestCases() */
169:
170: /**
171: * Test cache manager in a multi-threaded situation
172: */
173: public void testMultiThreaded() {
174: Vector myThreads = new Vector();
175: Thread oneTest = null;
176: long startTime = new Date().getTime();
177:
178: for (int j = 0; j < numThreads.length; j++) {
179: try {
180: if (myThreads.size() < numThreads[j]) {
181: for (int i = myThreads.size(); i < numThreads[j]; i++) {
182: myThreads
183: .addElement(new CacheTestThread("" + i));
184: }
185: } else {
186: myThreads.setSize(numThreads[j]);
187: }
188: /* Start 'em up */
189: for (Enumeration ee = myThreads.elements(); ee
190: .hasMoreElements();) {
191: oneTest = (Thread) ee.nextElement();
192: oneTest.start();
193: }
194: /* Wait for 'em to finish */
195: for (Enumeration ee = myThreads.elements(); ee
196: .hasMoreElements();) {
197: oneTest = (Thread) ee.nextElement();
198:
199: try {
200: oneTest.join();
201: } catch (InterruptedException ie) {
202: oneTest.interrupt();
203:
204: while (ee.hasMoreElements()) {
205: oneTest = (Thread) ee.nextElement();
206: oneTest.interrupt();
207: }
208:
209: throw new CacheException(ie);
210: }
211: }
212:
213: log.info("All threaded tests completed");
214: } catch (CacheException ce) {
215: log.error(ce);
216: fail("CacheException occurred in multi-threaded test - see log");
217: } catch (java.util.ConcurrentModificationException cme) {
218: log.error("Error testing simple caching", cme);
219: throw cme;
220: }
221:
222: long endTime = new Date().getTime();
223: System.out
224: .println("\nCacheTest: Total Time for Multi-threaded test: "
225: + Long.toString(endTime - startTime)
226: + " ms. Num Threads = "
227: + Integer.toString(numThreads[j]));
228: }
229: } /* testMultiThreaded() */
230:
231: }
|