1 23 24 29 30 package com.sun.cli.jmx.test; 31 32 33 import java.util.*; 36 import java.io.*; 37 import java.net.*; 38 39 import javax.management.ObjectName ; 42 import javax.management.MBeanServer ; 43 import javax.management.remote.JMXServiceURL ; 44 import javax.management.remote.JMXConnectorFactory ; 45 import javax.management.remote.JMXConnector ; 46 import javax.management.MBeanServerConnection ; 47 import javax.management.NotificationFilter ; 48 import javax.management.NotificationListener ; 49 import javax.management.Notification ; 50 import javax.management.Attribute ; 51 52 import com.sun.cli.jmx.support.CLISupportStrings; 53 54 55 58 59 public class NotificationTester extends Thread 60 implements NotificationListener 61 { 62 MBeanServerConnection mServer; 63 JMXConnector mConnection; 64 String mHost; 65 int mPort; 66 ThroughputCallback mThroughputCallback; 67 68 public static interface ThroughputCallback 69 { 70 public void throughputReport( long milliseconds, long numCalls ); 71 } 72 73 74 private static void 75 p( Object arg ) 76 { 77 System.out.println( arg.toString() ); 78 } 79 80 81 public 82 NotificationTester( String host, int port ) throws Exception 83 { 84 mHost = host; 85 mPort = port; 86 87 mConnection = Connect( host, port ); 88 mServer = mConnection.getMBeanServerConnection( ); 89 } 90 91 92 private JMXConnector 93 Connect( String host, int port ) throws Exception 94 { 95 final JMXServiceURL url = new JMXServiceURL ( "service:jmx:jmxmp://" + host + ":" + port ); 96 97 JMXConnector conn = JMXConnectorFactory.connect( url ); 98 return( conn ); 99 } 100 101 102 public void 103 RunSinglePerConnection( int numConnections ) throws Exception 104 { 105 final long startTime = System.currentTimeMillis(); 106 107 final ObjectName name = new ObjectName ( CLISupportStrings.CLI_SUPPORT_TARGET ); 108 for( int i = 0; i < numConnections; ++i ) 109 { 110 JMXConnector conn = Connect( mHost, mPort ); 111 MBeanServerConnection server = conn.getMBeanServerConnection( ); 112 113 Object result = server.getAttribute( name, "NbChanges" ); 114 conn.close(); 115 } 116 117 final long endTime = System.currentTimeMillis(); 118 final long elapsed = endTime - startTime; 119 120 p( "elapsed = " + elapsed ); 121 p( "connections per second = " + numConnections / (elapsed / 1000.0) ); 122 } 123 124 public void 125 RunMultiplePerConnection( int outerCount, int numIterations ) throws Exception 126 { 127 final ObjectName name = new ObjectName ( "foo" ); 128 130 for( int outer = 0; outer < outerCount; ++outer ) 131 { 132 final long startTime = System.currentTimeMillis(); 133 134 for( int i = 0; i < numIterations; ++i ) 135 { 136 Object result = mServer.getAttribute( name, "NotifsEmitted" ); 137 } 138 139 final long endTime = System.currentTimeMillis(); 140 final long elapsed = endTime - startTime; 141 142 mThroughputCallback.throughputReport( elapsed, numIterations ); 144 } 145 } 146 147 class MyFilter implements NotificationFilter , Serializable 148 { 149 151 public MyFilter( NotificationTester host ) 152 { 153 } 155 156 public boolean isNotificationEnabled(Notification notification) 157 { 158 return( true ); 159 } 160 } 161 162 long mNotifCount = 0; 163 164 public void 165 handleNotification(Notification notification, Object handback) 166 { 167 ++mNotifCount; 168 } 169 170 public void 171 RunNotifTest( long sleepMillis ) throws Exception 172 { 173 final ObjectName emitterName = new ObjectName ( CLISupportStrings.CLI_SIMPLE_TESTEE_TARGET ); 174 175 mNotifCount = 0; 176 boolean success = false;; 177 178 try 179 { 180 p( "adding listener" ); 181 mServer.addNotificationListener( emitterName, this, null, null); 182 183 p( "setting attribute" ); 184 mServer.setAttribute( emitterName, new Attribute ( "NotifMillis", new Long ( sleepMillis ) ) ); 185 186 p( "starting" ); 187 mServer.invoke( emitterName, "startNotif", null, null ); 188 success = true; 189 p( "started" ); 190 } 191 catch( Exception e ) 192 { 193 p( "caught exception: " + e ); 194 } 195 196 long startTime = System.currentTimeMillis(); 197 while( true ) 198 { 199 Thread.sleep( 500 ); 200 201 final long notifCount = mNotifCount; 202 final long elapsedMillis = System.currentTimeMillis() - startTime; 203 final double rate = (double)notifCount / (elapsedMillis / 1000.0 ); 204 System.out.println( "total notifications: " + notifCount + " = " + (rate *10.0)/10.0 + "/sec" ); 205 } 206 } 207 208 String mName; 209 int mOuterLoop; 210 int mNumIterations; 211 212 public void 213 run() 214 { 215 try 216 { 217 RunMultiplePerConnection( mOuterLoop, mNumIterations ); 218 } 219 catch( Exception e ) 220 { 221 p( e ); 222 } 223 } 224 225 public void 226 RunMultiplePerConnectionThreaded( 227 String name, 228 int outerLoop, 229 int numIterations, 230 ThroughputCallback throughputCallback) throws Exception 231 { 232 mName = name; 233 mOuterLoop = outerLoop; 234 mNumIterations = numIterations; 235 mThroughputCallback = throughputCallback; 236 237 this.start(); 238 } 239 240 241 }; 242 243 244 | Popular Tags |