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.ejb.remote.test;
019:
020: import javax.ejb.EJBHome;
021:
022: import org.sape.carbon.core.component.Lookup;
023: import org.sape.carbon.services.ejb.EnterpriseBeanConfiguration;
024: import org.sape.carbon.services.ejb.remote.RemoteHomeFactory;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: public class RemoteHomeCachePerformanceTest extends TestCase {
034:
035: /**
036: * Provides a handle to Apache-commons logger
037: */
038: private Log log = LogFactory.getLog(this .getClass());
039:
040: /**
041: * Path of the remote home factory test component
042: */
043: public static final String TEST_REMOTE_HOME_FACTORY = "/ejb/test/RemoteHomeFactoryTest";
044:
045: /**
046: * Logical name of the <code>Tester</code> EJB
047: */
048: public static final String TEST_REMOTE_EJB = "org.sape.carbon.services.ejb.remote.test.Tester";
049:
050: /**
051: * Number of iterations used in testing the home-interface cache
052: */
053: public static final long TEST_LOOKUP_ITERATIONS = 2000;
054:
055: public RemoteHomeCachePerformanceTest(String name) {
056: super (name);
057: }
058:
059: public static void main(String args[]) throws Exception {
060: }
061:
062: /**
063: * Tests the performance of the EJB service's home-interface cache
064: * functionality.
065: */
066: public void testRemoteHomeCachePerformance() {
067:
068: long startTime = 0;
069: long elapsedTime = 0;
070:
071: EJBHome ejbHome = null;
072:
073: RemoteHomeFactory homeFactory = (RemoteHomeFactory) Lookup
074: .getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
075:
076: if (log.isInfoEnabled()) {
077: log.info("Testing EJB home cache performance");
078:
079: // Measure non-cached performance
080: log.info("Performing " + TEST_LOOKUP_ITERATIONS
081: + " home interface lookups for EJB: "
082: + TEST_REMOTE_EJB + " without using cache");
083: }
084:
085: // Set the start time
086: startTime = System.currentTimeMillis();
087:
088: try {
089: // Loop
090: for (long i = 0; i < TEST_LOOKUP_ITERATIONS; i++) {
091:
092: ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
093: }
094:
095: } catch (Exception e) {
096: fail("Test of EJB home cache performance failed due to: "
097: + e);
098: }
099:
100: // Print the elapsed time
101: elapsedTime = System.currentTimeMillis() - startTime;
102: if (log.isInfoEnabled()) {
103: log.info("Total elapsed time: " + elapsedTime);
104: log
105: .info("Lookups/Second: "
106: + ((double) TEST_LOOKUP_ITERATIONS
107: / elapsedTime * 1000) + " lps");
108: }
109:
110: // Measure cached lookup performance
111: EnterpriseBeanConfiguration ejbDetails = homeFactory
112: .getEJBDetails(TEST_REMOTE_EJB);
113:
114: if (ejbDetails != null) {
115: ejbDetails.setCacheable(true);
116: } else {
117: fail("Test of EJB home cache performance failed; "
118: + "unable to retrieve EJB details for logical name: "
119: + TEST_REMOTE_EJB);
120: }
121:
122: if (log.isInfoEnabled()) {
123: log.info("Performing " + TEST_LOOKUP_ITERATIONS
124: + " home interface lookups for EJB: "
125: + TEST_REMOTE_EJB + " using cache");
126: }
127:
128: // Set the start time
129: startTime = System.currentTimeMillis();
130:
131: try {
132: for (long i = 0; i < TEST_LOOKUP_ITERATIONS; i++) {
133:
134: ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
135: }
136: } catch (Exception e) {
137: fail("Test of EJB home cache performance failed due to: "
138: + e);
139: }
140:
141: // Print the elapsed time
142: elapsedTime = System.currentTimeMillis() - startTime;
143: if (log.isInfoEnabled()) {
144: log.info("Total elapsed time: " + elapsedTime);
145: log
146: .info("Lookups/Second: "
147: + ((double) TEST_LOOKUP_ITERATIONS
148: / elapsedTime * 1000) + " lps");
149: }
150: }
151:
152: /**
153: * Method called by jUnit to get all the tests in this test case.
154: * @return Test the suite of tests in this test case
155: */
156: public static Test suite() {
157:
158: TestSuite test = new TestSuite();
159:
160: test.addTest(new RemoteHomeCachePerformanceTest(
161: "testRemoteHomeCachePerformance"));
162:
163: return test;
164: }
165: }
|