| 1 |
package org.astrogrid.samp.test; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.Iterator; |
| 7 |
import java.util.List; |
| 8 |
import java.util.Random; |
| 9 |
import java.util.logging.Level; |
| 10 |
import java.util.logging.Logger; |
| 11 |
import javax.swing.JFrame; |
| 12 |
import org.astrogrid.samp.client.ClientProfile; |
| 13 |
import org.astrogrid.samp.client.DefaultClientProfile; |
| 14 |
import org.astrogrid.samp.client.HubConnection; |
| 15 |
import org.astrogrid.samp.gui.HubMonitor; |
| 16 |
|
| 17 |
/** |
| 18 |
* Runs a load of Calculator clients at once all sending messages to each other. |
| 19 |
* Suitable for load testing or benchmarking a hub. |
| 20 |
* |
| 21 |
* @author Mark Taylor |
| 22 |
* @since 22 Jul 2008 |
| 23 |
*/ |
| 24 |
public class CalcStorm { |
| 25 |
|
| 26 |
private final ClientProfile profile_; |
| 27 |
private final Random random_; |
| 28 |
private final int nClient_; |
| 29 |
private final int nQuery_; |
| 30 |
private final Calculator.SendMode sendMode_; |
| 31 |
private static final Logger logger_ = |
| 32 |
Logger.getLogger( CalcStorm.class.getName() ); |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. |
| 36 |
* |
| 37 |
* @param profile hub connection factory |
| 38 |
* @param random random number generator |
| 39 |
* @param nClient number of clients to run |
| 40 |
* @param nQuery number of messages each client will send |
| 41 |
* @param sendMode delivery pattern for messages |
| 42 |
*/ |
| 43 |
public CalcStorm( ClientProfile profile, Random random, int nClient, |
| 44 |
int nQuery, Calculator.SendMode sendMode ) { |
| 45 |
profile_ = profile; |
| 46 |
random_ = random; |
| 47 |
nClient_ = nClient; |
| 48 |
nQuery_ = nQuery; |
| 49 |
sendMode_ = sendMode; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Runs a lot of calculators at once all talking to each other. |
| 54 |
* |
| 55 |
* @throws TestException if any tests fail |
| 56 |
*/ |
| 57 |
public void run() throws IOException { |
| 58 |
|
| 59 |
// Set up clients. |
| 60 |
final Calculator[] calcs = new Calculator[ nClient_ ]; |
| 61 |
final String[] ids = new String[ nClient_ ]; |
| 62 |
final Random[] randoms = new Random[ nClient_ ]; |
| 63 |
for ( int ic = 0; ic < nClient_; ic++ ) { |
| 64 |
HubConnection conn = profile_.register(); |
| 65 |
if ( conn == null ) { |
| 66 |
throw new IOException( "No hub is running" ); |
| 67 |
} |
| 68 |
randoms[ ic ] = new Random( random_.nextLong() ); |
| 69 |
ids[ ic ] = conn.getRegInfo().getSelfId(); |
| 70 |
calcs[ ic ] = new Calculator( conn, randoms[ ic ] ); |
| 71 |
} |
| 72 |
|
| 73 |
// Set up one thread per client to do the message sending. |
| 74 |
Thread[] calcThreads = new Thread[ nClient_ ]; |
| 75 |
final Throwable[] errors = new Throwable[ 1 ]; |
| 76 |
for ( int ic = 0; ic < nClient_; ic++ ) { |
| 77 |
final Calculator calc = calcs[ ic ]; |
| 78 |
final Random random = randoms[ ic ]; |
| 79 |
calcThreads[ ic ] = new Thread( "Calc" + ic ) { |
| 80 |
public void run() { |
| 81 |
try { |
| 82 |
for ( int iq = 0; iq < nQuery_ && errors[ 0 ] == null; |
| 83 |
iq++ ) { |
| 84 |
calc.sendMessage( ids[ random.nextInt( nClient_ ) ], |
| 85 |
sendMode_ ); |
| 86 |
} |
| 87 |
calc.flush(); |
| 88 |
} |
| 89 |
catch ( Throwable e ) { |
| 90 |
errors[ 0 ] = e; |
| 91 |
} |
| 92 |
} |
| 93 |
}; |
| 94 |
} |
| 95 |
|
| 96 |
// Start the threads running. |
| 97 |
for ( int ic = 0; ic < nClient_; ic++ ) { |
| 98 |
calcThreads[ ic ].start(); |
| 99 |
} |
| 100 |
|
| 101 |
// Wait for all the threads to finish. |
| 102 |
try { |
| 103 |
for ( int ic = 0; ic < nClient_; ic++ ) { |
| 104 |
calcThreads[ ic ].join(); |
| 105 |
} |
| 106 |
} |
| 107 |
catch ( InterruptedException e ) { |
| 108 |
throw new TestException( "Interrupted", e ); |
| 109 |
} |
| 110 |
|
| 111 |
// If we are using the notification delivery pattern, wait until |
| 112 |
// all the clients have received all the messages they are expecting. |
| 113 |
// In the case of call/response this is not necessary, since the |
| 114 |
// message sender threads will only complete their run() methods |
| 115 |
// when the responses have come back, which must mean that the |
| 116 |
// messages arrived at their recipients. |
| 117 |
if ( sendMode_ == Calculator.NOTIFY_MODE || |
| 118 |
sendMode_ == Calculator.RANDOM_MODE ) { |
| 119 |
for ( boolean done = false; ! done; ) { |
| 120 |
int totCalc = 0; |
| 121 |
for ( int ic = 0; ic < nClient_; ic++ ) { |
| 122 |
totCalc += calcs[ ic ].getReceiveCount(); |
| 123 |
} |
| 124 |
done = totCalc >= nClient_ * nQuery_; |
| 125 |
if ( ! done ) { |
| 126 |
Thread.yield(); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
// Unregister the clients. |
| 133 |
for ( int ic = 0; ic < nClient_; ic++ ) { |
| 134 |
calcs[ ic ].getConnection().unregister(); |
| 135 |
} |
| 136 |
|
| 137 |
// If any errors occurred on the sending thread, rethrow one of them |
| 138 |
// here. |
| 139 |
if ( errors[ 0 ] != null ) { |
| 140 |
throw new TestException( "Error in calculator thread: " |
| 141 |
+ errors[ 0 ].getMessage(), |
| 142 |
errors[ 0 ] ); |
| 143 |
} |
| 144 |
|
| 145 |
// Check that the number of messages sent and the number received |
| 146 |
// was what it should have been. |
| 147 |
int totCalc = 0; |
| 148 |
for ( int ic = 0; ic < nClient_; ic++ ) { |
| 149 |
Calculator calc = calcs[ ic ]; |
| 150 |
Tester.assertEquals( nQuery_, calc.getSendCount() ); |
| 151 |
totCalc += calc.getReceiveCount(); |
| 152 |
} |
| 153 |
Tester.assertEquals( totCalc, nClient_ * nQuery_ ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Does the work for the main method. |
| 158 |
* Use -help flag for documentation. |
| 159 |
* |
| 160 |
* @param args command-line arguments |
| 161 |
* @return 0 means success |
| 162 |
*/ |
| 163 |
public static int runMain( String[] args ) throws IOException { |
| 164 |
|
| 165 |
// Set up usage message. |
| 166 |
String usage = new StringBuffer() |
| 167 |
.append( "\n Usage:" ) |
| 168 |
.append( "\n " ) |
| 169 |
.append( CalcStorm.class.getName() ) |
| 170 |
.append( "\n " ) |
| 171 |
.append( " [-help]" ) |
| 172 |
.append( " [-/+verbose]" ) |
| 173 |
.append( "\n " ) |
| 174 |
.append( " [-gui]" ) |
| 175 |
.append( " [-nclient <n>]" ) |
| 176 |
.append( " [-nquery <n>]" ) |
| 177 |
.append( "\n " ) |
| 178 |
.append( " [-mode sync|async|notify|random]" ) |
| 179 |
.append( "\n" ) |
| 180 |
.toString(); |
| 181 |
|
| 182 |
// Prepare default values for test. |
| 183 |
Random random = new Random( 2333333 ); |
| 184 |
int nClient = 20; |
| 185 |
int nQuery = 100; |
| 186 |
Calculator.SendMode sendMode = Calculator.RANDOM_MODE; |
| 187 |
int verbAdjust = 0; |
| 188 |
boolean gui = false; |
| 189 |
|
| 190 |
// Parse arguments, modifying test parameters as appropriate. |
| 191 |
List argList = new ArrayList( Arrays.asList( args ) ); |
| 192 |
try { |
| 193 |
for ( Iterator it = argList.iterator(); it.hasNext(); ) { |
| 194 |
String arg = (String) it.next(); |
| 195 |
if ( arg.startsWith( "-nc" ) && it.hasNext() ) { |
| 196 |
it.remove(); |
| 197 |
String snc = (String) it.next(); |
| 198 |
it.remove(); |
| 199 |
nClient = Integer.parseInt( snc ); |
| 200 |
} |
| 201 |
else if ( arg.startsWith( "-nq" ) && it.hasNext() ) { |
| 202 |
it.remove(); |
| 203 |
String snq = (String) it.next(); |
| 204 |
it.remove(); |
| 205 |
nQuery = Integer.parseInt( snq ); |
| 206 |
} |
| 207 |
else if ( arg.equals( "-mode" ) && it.hasNext() ) { |
| 208 |
it.remove(); |
| 209 |
String smode = (String) it.next(); |
| 210 |
it.remove(); |
| 211 |
final Calculator.SendMode sm; |
| 212 |
if ( smode.toLowerCase().startsWith( "sync" ) ) { |
| 213 |
sm = Calculator.SYNCH_MODE; |
| 214 |
} |
| 215 |
else if ( smode.toLowerCase().startsWith( "async" ) ) { |
| 216 |
sm = Calculator.ASYNCH_MODE; |
| 217 |
} |
| 218 |
else if ( smode.toLowerCase().startsWith( "notif" ) ) { |
| 219 |
sm = Calculator.NOTIFY_MODE; |
| 220 |
} |
| 221 |
else if ( smode.toLowerCase().startsWith( "rand" ) ) { |
| 222 |
sm = Calculator.RANDOM_MODE; |
| 223 |
} |
| 224 |
else { |
| 225 |
System.err.println( usage ); |
| 226 |
return 1; |
| 227 |
} |
| 228 |
sendMode = sm; |
| 229 |
} |
| 230 |
else if ( arg.equals( "-gui" ) ) { |
| 231 |
it.remove(); |
| 232 |
gui = true; |
| 233 |
} |
| 234 |
else if ( arg.equals( "-nogui" ) ) { |
| 235 |
it.remove(); |
| 236 |
gui = false; |
| 237 |
} |
| 238 |
else if ( arg.startsWith( "-v" ) ) { |
| 239 |
it.remove(); |
| 240 |
verbAdjust--; |
| 241 |
} |
| 242 |
else if ( arg.startsWith( "+v" ) ) { |
| 243 |
it.remove(); |
| 244 |
verbAdjust++; |
| 245 |
} |
| 246 |
else if ( arg.startsWith( "-h" ) ) { |
| 247 |
System.out.println( usage ); |
| 248 |
return 0; |
| 249 |
} |
| 250 |
else { |
| 251 |
System.err.println( usage ); |
| 252 |
return 1; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
catch ( RuntimeException e ) { |
| 257 |
System.err.println( usage ); |
| 258 |
return 1; |
| 259 |
} |
| 260 |
if ( ! argList.isEmpty() ) { |
| 261 |
System.err.println( usage ); |
| 262 |
return 1; |
| 263 |
} |
| 264 |
|
| 265 |
// Adjust logging in accordance with verboseness flags. |
| 266 |
int logLevel = Level.WARNING.intValue() + 100 * verbAdjust; |
| 267 |
Logger.getLogger( "org.astrogrid.samp" ) |
| 268 |
.setLevel( Level.parse( Integer.toString( logLevel ) ) ); |
| 269 |
|
| 270 |
// Prepare profile. |
| 271 |
ClientProfile profile = DefaultClientProfile.getProfile(); |
| 272 |
|
| 273 |
// Set up GUI monitor if required. |
| 274 |
JFrame frame; |
| 275 |
if ( gui ) { |
| 276 |
frame = new JFrame( "CalcStorm Monitor" ); |
| 277 |
frame.getContentPane().add( new HubMonitor( profile, true, 1 ) ); |
| 278 |
frame.pack(); |
| 279 |
frame.setVisible( true ); |
| 280 |
} |
| 281 |
else { |
| 282 |
frame = null; |
| 283 |
} |
| 284 |
|
| 285 |
// Run the test. |
| 286 |
long start = System.currentTimeMillis(); |
| 287 |
new CalcStorm( profile, random, nClient, nQuery, sendMode ).run(); |
| 288 |
long time = System.currentTimeMillis() - start; |
| 289 |
System.out.println( "Elapsed time: " + time + " ms" |
| 290 |
+ " (" + (int) ( time * 1000. / ( nClient * nQuery ) ) |
| 291 |
+ " us per message)" ); |
| 292 |
|
| 293 |
// Tidy up and return. |
| 294 |
if ( frame != null ) { |
| 295 |
frame.dispose(); |
| 296 |
} |
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Main method. Use -help flag. |
| 302 |
*/ |
| 303 |
public static void main( String[] args ) throws IOException { |
| 304 |
int status = runMain( args ); |
| 305 |
if ( status != 0 ) { |
| 306 |
System.exit( status ); |
| 307 |
} |
| 308 |
} |
| 309 |
} |