KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > ejb > remote > test > RemoteHomeCachePerformanceTest


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

17
18 package org.sape.carbon.services.ejb.remote.test;
19
20
21 import javax.ejb.EJBHome JavaDoc;
22
23 import org.sape.carbon.core.component.Lookup;
24 import org.sape.carbon.services.ejb.EnterpriseBeanConfiguration;
25 import org.sape.carbon.services.ejb.remote.RemoteHomeFactory;
26
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 public class RemoteHomeCachePerformanceTest extends TestCase {
35
36     /**
37      * Provides a handle to Apache-commons logger
38      */

39     private Log log = LogFactory.getLog(this.getClass());
40
41     /**
42      * Path of the remote home factory test component
43      */

44     public static final String JavaDoc TEST_REMOTE_HOME_FACTORY =
45         "/ejb/test/RemoteHomeFactoryTest";
46
47
48     /**
49      * Logical name of the <code>Tester</code> EJB
50      */

51     public static final String JavaDoc TEST_REMOTE_EJB =
52         "org.sape.carbon.services.ejb.remote.test.Tester";
53
54
55     /**
56      * Number of iterations used in testing the home-interface cache
57      */

58     public static final long TEST_LOOKUP_ITERATIONS = 2000;
59
60
61     public RemoteHomeCachePerformanceTest(String JavaDoc name) {
62         super(name);
63     }
64
65
66     public static void main(String JavaDoc args[]) throws Exception JavaDoc{
67     }
68
69
70     /**
71      * Tests the performance of the EJB service's home-interface cache
72      * functionality.
73      */

74     public void testRemoteHomeCachePerformance() {
75
76         long startTime = 0;
77         long elapsedTime = 0;
78
79         EJBHome JavaDoc ejbHome = null;
80
81         RemoteHomeFactory homeFactory = (RemoteHomeFactory)
82             Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
83
84         if (log.isInfoEnabled()) {
85             log.info("Testing EJB home cache performance");
86
87             // Measure non-cached performance
88
log.info("Performing "
89                 + TEST_LOOKUP_ITERATIONS
90                 + " home interface lookups for EJB: "
91                 + TEST_REMOTE_EJB
92                 + " without using cache");
93         }
94
95         // Set the start time
96
startTime = System.currentTimeMillis();
97
98         try {
99             // Loop
100
for (long i = 0; i < TEST_LOOKUP_ITERATIONS; i++) {
101
102                 ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
103             }
104
105         } catch (Exception JavaDoc e) {
106             fail("Test of EJB home cache performance failed due to: " + e);
107         }
108
109         // Print the elapsed time
110
elapsedTime = System.currentTimeMillis() - startTime;
111         if (log.isInfoEnabled()) {
112             log.info("Total elapsed time: "
113                 + elapsedTime);
114             log.info("Lookups/Second: "
115                 + ((double) TEST_LOOKUP_ITERATIONS/elapsedTime*1000)
116                 + " lps");
117         }
118
119         // Measure cached lookup performance
120
EnterpriseBeanConfiguration ejbDetails =
121             homeFactory.getEJBDetails(TEST_REMOTE_EJB);
122
123         if (ejbDetails != null) {
124             ejbDetails.setCacheable(true);
125         } else {
126             fail("Test of EJB home cache performance failed; "
127                 + "unable to retrieve EJB details for logical name: "
128                 + TEST_REMOTE_EJB);
129         }
130
131         if (log.isInfoEnabled()) {
132             log.info("Performing "
133                 + TEST_LOOKUP_ITERATIONS
134                 + " home interface lookups for EJB: "
135                 + TEST_REMOTE_EJB
136                 + " using cache");
137         }
138
139         // Set the start time
140
startTime = System.currentTimeMillis();
141
142         try {
143             for (long i = 0; i < TEST_LOOKUP_ITERATIONS; i++) {
144
145                 ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
146             }
147         } catch (Exception JavaDoc e) {
148             fail("Test of EJB home cache performance failed due to: " + e);
149         }
150
151         // Print the elapsed time
152
elapsedTime = System.currentTimeMillis() - startTime;
153         if (log.isInfoEnabled()) {
154             log.info("Total elapsed time: "
155                 + elapsedTime);
156             log.info("Lookups/Second: "
157                 + ((double) TEST_LOOKUP_ITERATIONS/elapsedTime*1000)
158                 + " lps");
159         }
160     }
161
162     /**
163      * Method called by jUnit to get all the tests in this test case.
164      * @return Test the suite of tests in this test case
165      */

166     public static Test suite() {
167
168         TestSuite test = new TestSuite();
169
170         test.addTest(new RemoteHomeCachePerformanceTest(
171             "testRemoteHomeCachePerformance"));
172
173         return test;
174     }
175 }
176
Popular Tags