KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > client > PerformanceTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/tests/com/sun/enterprise/management/client/PerformanceTest.java,v 1.6 2006/03/09 20:30:52 llc Exp $
26  * $Revision: 1.6 $
27  * $Date: 2006/03/09 20:30:52 $
28  */

29 package com.sun.enterprise.management.client;
30
31 import java.io.IOException JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 import java.lang.reflect.Method JavaDoc;
38 import java.lang.reflect.InvocationTargetException JavaDoc;
39
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.MBeanServerConnection JavaDoc;
42
43 import com.sun.appserv.management.base.AMX;
44 import com.sun.appserv.management.base.XTypes;
45 import com.sun.appserv.management.base.QueryMgr;
46 import com.sun.appserv.management.base.Util;
47 import com.sun.appserv.management.base.Sample;
48 import com.sun.appserv.management.DomainRoot;
49
50 import com.sun.appserv.management.config.SSLConfig;
51 import com.sun.appserv.management.config.DomainConfig;
52 import com.sun.appserv.management.config.AMXConfig;
53 import com.sun.appserv.management.config.StandaloneServerConfig;
54
55 import com.sun.appserv.management.util.misc.ClassUtil;
56 import com.sun.appserv.management.util.misc.MapUtil;
57 import com.sun.appserv.management.util.jmx.JMXUtil;
58 import com.sun.appserv.management.util.stringifier.ArrayStringifier;
59
60
61 import com.sun.enterprise.management.AMXTestBase;
62 import com.sun.enterprise.management.Capabilities;
63
64 /**
65     Note that the tests are synchronized so that the performance numbers
66     are not affected by concurrent tests.
67  */

68 public final class PerformanceTest extends AMXTestBase
69 {
70     private static boolean BASELINE_DONE = false;
71     
72     private static final int K = 1024;
73     private static final int MB = K * K;
74     
75         public static Capabilities
76     getCapabilities()
77     {
78         return getOfflineCapableCapabilities( true );
79     }
80     
81         public void
82     baselineTest( final MBeanServerConnection JavaDoc conn )
83         throws IOException JavaDoc
84     {
85         if ( ! BASELINE_DONE ) synchronized( PerformanceTest.class )
86         {
87             BASELINE_DONE = true;
88             
89             printVerbose( "--- Baseline statistics for connection --- " );
90             
91             final ObjectName JavaDoc delegateObjectName = JMXUtil.getMBeanServerDelegateObjectName();
92             final int ITER = 1000;
93             final long start = now();
94             for( int i = 0; i < ITER; ++i )
95             {
96                 conn.isRegistered( delegateObjectName );
97             }
98             printVerbose( "Time to call MBeanServerConnection.isRegistered() " + ITER + " times: " + (now() - start) + " ms" );
99             
100             final Sample sample = (Sample)getDomainRoot().getContainee( XTypes.SAMPLE );
101
102             // test upload bandwidth
103
final byte[] uploadBytes = new byte[ 1 * MB ];
104             final long uploadStart = now();
105             sample.uploadBytes( uploadBytes );
106             final long uploadElapsed = now() - uploadStart;
107             final int uploadKBPerSec = (int)((uploadBytes.length / 1024.0) / (uploadElapsed / 1000.0));
108             printVerbose( "Upload bandwidth (" + uploadBytes.length + " bytes): " + uploadKBPerSec + "kb/sec" );
109             
110             // test download bandwidth
111
final long downloadStart = now();
112             final byte[] downloadedBytes = sample.downloadBytes( 1 * K * K );
113             final long downloadElapsed = now() - downloadStart;
114             final int downloadKBPerSec = (int)((downloadedBytes.length / 1024.0) / (downloadElapsed / 1000.0));
115             printVerbose( "Download bandwidth (" + uploadBytes.length + " bytes): " + downloadKBPerSec + "kb/sec" );
116             
117             testTransferSizePerformance( conn );
118         }
119     }
120     
121     
122         private void
123     testTransferSizePerformance( final MBeanServerConnection JavaDoc conn )
124         throws IOException JavaDoc
125     {
126         final Sample sample = (Sample)getDomainRoot().getContainee( XTypes.SAMPLE );
127         
128         final int TOTAL = 1 * MB;
129         printVerbose( "Upload bandwidth, test size = " + (TOTAL / (float)(MB)) + "MB");
130         
131         for( int chunkSize = K; chunkSize <= TOTAL; chunkSize *= 2 )
132         {
133             final byte[] chunk = new byte[ chunkSize ];
134             
135             final long uploadStart = now();
136             int total = 0;
137             while ( total < TOTAL )
138             {
139                 sample.uploadBytes( chunk );
140                 total += chunk.length;
141             }
142             
143             final long uploadElapsed = now() - uploadStart;
144             final int uploadKBPerSec = (int)((TOTAL / (float)K) / (uploadElapsed / 1000.0));
145             printVerbose( "Upload bandwidth (" + chunkSize/K + "K chunks): " + uploadKBPerSec + "kb/sec" );
146         }
147     }
148
149         public
150     PerformanceTest( )
151     {
152         try
153         {
154             final MBeanServerConnection JavaDoc conn =
155                 Util.getExtra( getDomainRoot() ).getConnectionSource().getMBeanServerConnection( false );
156             
157             baselineTest( conn );
158         }
159         catch( IOException JavaDoc e )
160         {
161         }
162     }
163     
164         private Method JavaDoc
165     findMethod(
166         final Object JavaDoc target,
167         final String JavaDoc methodName,
168         final Object JavaDoc[] args )
169         throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
170     {
171         final Method JavaDoc[] methods = target.getClass().getDeclaredMethods();
172         
173         final int numArgs = args == null ? 0 : args.length;
174         Method JavaDoc testMethod = null;
175         for( int i = 0; i < methods.length; ++i )
176         {
177             final Method JavaDoc method = methods[ i];
178             
179             if ( method.getName().equals( methodName ) &&
180                 method.getParameterTypes().length == numArgs )
181             {
182                 testMethod = method;
183                 break;
184             }
185         }
186         
187         if ( testMethod == null )
188         {
189             throw new IllegalArgumentException JavaDoc( "Can't find method: " + methodName );
190         }
191         return( testMethod );
192     }
193     
194         private String JavaDoc
195     getMethodString( final Method JavaDoc m, final Object JavaDoc[] args )
196     {
197         String JavaDoc result = null;
198         
199         if ( args == null || args.length == 0 )
200         {
201             result = m.getName() + "()";
202         }
203         else
204         {
205             result = m.getName() + "(" + ArrayStringifier.stringify( args, ", ") + ")";
206         }
207
208         return result;
209     }
210     
211         private void
212     testMethod(
213         final AMX target,
214         final String JavaDoc methodName,
215         final Object JavaDoc[] args,
216         final int additionalIterations )
217         throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
218     {
219         final String JavaDoc interfaceName =
220             ClassUtil.stripPackagePrefix( Util.getExtra( target ).getInterfaceName() );
221         
222         final Method JavaDoc testMethod = findMethod( target, methodName, args );
223         
224         final long start = now();
225         final Object JavaDoc resultFirst = testMethod.invoke( target, args );
226         final long elapsedFirst = now() - start;
227         
228         String JavaDoc msg = interfaceName + "." + getMethodString( testMethod, args ) +
229                             ": " + elapsedFirst + "ms";
230         
231         if ( additionalIterations != 0 )
232         {
233             final long iterStart = now();
234             for( int i = 0; i < additionalIterations - 1; ++i )
235             {
236                 final Object JavaDoc result = testMethod.invoke( target, args );
237             }
238             final long iterElapsed = now() - iterStart;
239             
240             msg = msg + ", " + additionalIterations + " additional iterations: " + iterElapsed + "ms";
241         }
242         
243         printVerbose( msg );
244     }
245     
246     
247         public synchronized void
248     testQueryMgr()
249         throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
250     {
251         final DomainRoot domainRoot = getDomainRoot();
252         final QueryMgr queryMgr = domainRoot.getQueryMgr();
253         
254         final String JavaDoc domain = Util.getObjectName( queryMgr ).getDomain();
255         
256         printVerbose( "-- QueryMgr --- " );
257         
258         testMethod( domainRoot, "getQueryMgr", null, 1000 );
259         
260         final int ITER = 20;
261         testMethod( queryMgr, "queryAllSet", null, ITER );
262         testMethod( queryMgr, "querySingletonJ2EEType", new Object JavaDoc[] { XTypes.BULK_ACCESS}, ITER );
263         testMethod( queryMgr, "queryJ2EETypeSet", new Object JavaDoc[] { XTypes.SSL_CONFIG}, ITER );
264         testMethod( queryMgr, "queryJ2EENameSet", new Object JavaDoc[] { "server" }, ITER );
265         testMethod( queryMgr, "queryJ2EETypeNames", new Object JavaDoc[] { XTypes.CONFIG_CONFIG }, ITER );
266         testMethod( queryMgr, "queryPatternSet", new Object JavaDoc[] { domain, "j2eeType=" + XTypes.SERVLET_MONITOR }, ITER );
267         testMethod( queryMgr, "queryInterfaceSet", new Object JavaDoc[] { SSLConfig.class.getName(), null}, ITER );
268     }
269     
270     /*
271         public synchronized void
272     testTargets()
273         throws IllegalAccessException, InvocationTargetException
274     {
275         DomainConfig domainConfig = getDomainRoot().getDomainConfig();
276
277         final long start = now();
278         
279         final int ITER = 100;
280         for( int i = 0; i < ITER; ++i )
281         {
282             final Mapxxx servers = domainConfig.getStandaloneServerConfigMap();
283             final Mapxxx clusters = domainConfig.getClusterConfigMap();
284             
285             final String[] serverNames = GSetUtil.toStringArray( servers.keySet() );
286             final String[] clusterNames = GSetUtil.toStringArray( clusters.keySet() );
287         }
288         
289         final long elapsed = now() - start;
290         printVerbose( "testTargets: " + ITER + " iterations: " + elapsed);
291     }
292     */

293     
294         public synchronized void
295     testDomainConfig()
296         throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
297     {
298         final DomainRoot domainRoot = getDomainRoot();
299         final DomainConfig domainConfig = domainRoot.getDomainConfig();
300         
301         printVerbose( "-- DomainConfig --- " );
302         
303         final int ITER = 20;
304         
305         testMethod( domainConfig, "getNodeAgentConfigMap", null, ITER );
306         testMethod( domainConfig, "getConfigConfigMap", null, ITER );
307         testMethod( domainConfig, "getStandaloneServerConfigMap", null, ITER );
308         testMethod( domainConfig, "getClusteredServerConfigMap", null, ITER );
309         testMethod( domainConfig, "getServerConfigMap", null, ITER );
310         testMethod( domainConfig, "getClusterConfigMap", null, ITER );
311         
312         testMethod( domainConfig, "getCustomResourceConfigMap", null, ITER );
313         testMethod( domainConfig, "getJNDIResourceConfigMap", null, ITER );
314         testMethod( domainConfig, "getPersistenceManagerFactoryResourceConfigMap", null, ITER );
315         testMethod( domainConfig, "getJDBCResourceConfigMap", null, ITER );
316         testMethod( domainConfig, "getJDBCConnectionPoolConfigMap", null, ITER );
317         testMethod( domainConfig, "getConnectorResourceConfigMap", null, ITER );
318         testMethod( domainConfig, "getConnectorConnectionPoolConfigMap", null, ITER );
319         testMethod( domainConfig, "getAdminObjectResourceConfigMap", null, ITER );
320         testMethod( domainConfig, "getResourceAdapterConfigMap", null, ITER );
321         testMethod( domainConfig, "getMailResourceConfigMap", null, ITER );
322         testMethod( domainConfig, "getJ2EEApplicationConfigMap", null, ITER );
323         testMethod( domainConfig, "getEJBModuleConfigMap", null, ITER );
324         testMethod( domainConfig, "getWebModuleConfigMap", null, ITER );
325         testMethod( domainConfig, "getRARModuleConfigMap", null, ITER );
326         testMethod( domainConfig, "getAppClientModuleConfigMap", null, ITER );
327         testMethod( domainConfig, "getLifecycleModuleConfigMap", null, ITER );
328     }
329     
330 }
331
332
333
334
335
336
Popular Tags