KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > test > TestClientMain


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-cli/cli-api/src/java/com/sun/cli/jmx/test/TestClientMain.java,v 1.3 2005/12/25 03:45:53 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:53 $
28  */

29  
30 package com.sun.cli.jmx.test;
31
32
33 import java.lang.reflect.Array JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.ListIterator JavaDoc;
36
37 import javax.management.MBeanServerConnection JavaDoc;
38 import javax.management.remote.JMXServiceURL JavaDoc;
39 import javax.management.remote.JMXConnector JavaDoc;
40 import javax.management.remote.JMXConnectorFactory JavaDoc;
41
42 import com.sun.cli.jmx.cmd.CmdReader;
43 import com.sun.cli.jmx.support.StringifierRegistryIniter;
44 import com.sun.cli.jmx.support.StandardAliasesIniter;
45 import com.sun.cli.jmx.cmd.ArgHelper;
46 import com.sun.cli.jmx.cmd.ArgHelperImpl;
47 import com.sun.cli.jmx.cmd.ArgHelperOptionsInfo;
48
49 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
50 import com.sun.cli.jmx.support.AliasMgrMBean;
51 import com.sun.cli.jmx.support.AliasMgrHashMapImpl;
52 import com.sun.cli.jmx.support.AliasMgr;
53 import com.sun.cli.jmx.support.CLISupport;
54 import com.sun.cli.jmx.support.CLISupportMBean;
55
56 import com.sun.cli.jmx.cmd.ArgHelperImpl;
57 import com.sun.cli.jmx.cmd.ArgHelperOptionsInfo;
58
59 /*
60     Trivial application that displays a string
61 */

62
63 public class TestClientMain implements NotificationTester.ThroughputCallback
64 {
65     long mMilliseconds = 0;
66     long mNumCalls = 0;
67     long mStartTime = 0;
68     long mTotalNumCalls = 0;
69     short mNumThreads = 1;
70     short mNumAveraged = 0;
71     Object JavaDoc mCritical = new Object JavaDoc();
72
73     
74     TestClientMain( )
75     {
76     }
77     
78         long
79     now()
80     {
81         return( System.currentTimeMillis() );
82     }
83
84         private static void
85     p( Object JavaDoc arg )
86     {
87         System.out.println( arg.toString() );
88     }
89     
90         public void
91     throughputReport( long milliseconds, long numCalls )
92     {
93         String JavaDoc progString = "";
94         
95         synchronized ( mCritical )
96         {
97             mTotalNumCalls += numCalls;
98             
99             ++mNumAveraged;
100             if ( mNumAveraged > mNumThreads * 8 )
101             {
102                 // reset periodically so we don't get hurt by blips in poor performance
103
mMilliseconds = 0;
104                 mNumCalls = 0;
105                 mNumAveraged = 0;
106             }
107             
108             mMilliseconds += milliseconds;
109             mNumCalls += numCalls;
110             
111             final double rollingCallsPerSec = mNumCalls / ((mMilliseconds / 1000.0) / (double)mNumThreads);
112             final double totalCallsPerSec = mTotalNumCalls / ((now() - mStartTime) / 1000.0);
113             
114             progString = mNumThreads + " threads: " +
115                             (long)rollingCallsPerSec + " (rolling) " +
116                             (long)totalCallsPerSec + " (total)";
117         }
118         
119         // keep p() outside synchronized section
120
p( progString );
121     }
122     
123         private void
124     testNotifications( String JavaDoc host, int port ) throws Exception JavaDoc
125     {
126         System.out.println( "Testing: " + host + ":" + port + ", threads = " + mNumThreads );
127         
128         final int kNumInvokes = 1 * 1024;
129         
130         mStartTime = System.currentTimeMillis();
131         for( int i = 0; i < mNumThreads; ++i )
132         {
133             NotificationTester test = new NotificationTester( host, port);
134             
135             // test.RunMultiplePerConnectionThreaded( "" + i, 1000, kNumInvokes, this);
136
test.RunNotifTest( 0 );
137         }
138     }
139     
140
141         private static JMXConnector JavaDoc
142     establishConnection( String JavaDoc host, int port )
143         throws Exception JavaDoc
144     {
145         final JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc( "service:jmx:jmxmp://" + host + ":" + port );
146             
147         JMXConnector JavaDoc conn = JMXConnectorFactory.connect( url );
148         
149         return( conn );
150     }
151     
152         private CLISupportMBeanProxy
153     createProxy(MBeanServerConnection JavaDoc managedServer, boolean runLocally ) throws Exception JavaDoc
154     {
155         CLISupportMBeanProxy proxy = null;
156         
157         AliasMgrMBean aliasMgr = null;
158         CLISupportMBean cliSupport = null;
159         
160         if ( runLocally )
161         {
162             final AliasMgrHashMapImpl aliasMgrImpl = new AliasMgrHashMapImpl();
163             aliasMgrImpl.load( AliasMgrHashMapImpl.DEFAULT_FILENAME );
164             aliasMgr = new AliasMgr( aliasMgrImpl );
165             StandardAliasesIniter.init( aliasMgr );
166             
167             cliSupport = new CLISupport( managedServer, aliasMgr );
168             
169         }
170         else
171         {
172             aliasMgr = CLISupportMBeanProxy.createAliasMgrProxy( managedServer );
173             cliSupport = CLISupportMBeanProxy.createCLISupportProxy( managedServer );
174         }
175         
176         proxy = new CLISupportMBeanProxy( aliasMgr, cliSupport);
177         
178         return( proxy );
179     }
180     
181         private void
182     testCLISupport( MBeanServerConnection JavaDoc conn, boolean runLocally) throws Exception JavaDoc
183     {
184         final CLISupportMBeanProxy proxy = createProxy( conn, runLocally );
185         
186         final CLISupportTester tester = new CLISupportTester( conn, proxy );
187         tester.Run();
188     }
189
190         
191     private final static String JavaDoc OPTIONS = "host=1 port=2 interactive localsupport";
192     
193         public static void
194     main(String JavaDoc args[])
195     {
196         try
197         {
198             final ArgHelperOptionsInfo optionInfo = new ArgHelperOptionsInfo( OPTIONS );
199             
200             final ListIterator JavaDoc iter = Arrays.asList( args ).listIterator();
201             final ArgHelper argHelper = new ArgHelperImpl( iter, optionInfo);
202             
203             final Integer JavaDoc port = argHelper.getInteger( "--port" );
204             final String JavaDoc host = argHelper.getString( "--host", "localhost");
205             final boolean interactive = argHelper.getBoolean( "--interactive", Boolean.FALSE).booleanValue();
206             final boolean runLocally = argHelper.getBoolean( "--localsupport", Boolean.FALSE ).booleanValue();
207             
208             if ( port == null )
209             {
210                 System.out.println( "USAGE: TestClient --port=<port-number>" );
211                 System.exit( 1 );
212             }
213             
214             final TestClientMain testMain = new TestClientMain( );
215             
216             new StringifierRegistryIniter();
217             
218             final JMXConnector JavaDoc jmxConnector = establishConnection( host, port.intValue() );
219             final MBeanServerConnection JavaDoc conn = jmxConnector.getMBeanServerConnection();
220             
221             p( "Connected to: " + host + ":" + port );
222             
223             p( ( runLocally ? "Running with local CLI support.":"Running with remote CLI support") );
224             
225             //testMain.testNotifications( host, port.intValue() );
226
testMain.testCLISupport( conn, runLocally );
227         }
228         catch( Exception JavaDoc e )
229         {
230             p( e );
231         }
232     }
233 };
234
235
236
Popular Tags