2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.performance.synchronous;
25
import EDU.oswego.cs.dl.util.concurrent.Latch;
26
import junit.framework.Test;
27
import junit.framework.TestCase;
28
import org.jboss.jrunit.controller.ThreadLocalBenchmark;
29
import org.jboss.jrunit.decorators.ThreadLocalDecorator;
30
import org.jboss.logging.Logger;
31
import org.jboss.remoting.Client;
32
import org.jboss.remoting.InvokerLocator;
33
import org.jboss.remoting.invocation.NameBasedInvocation;
34
import org.jboss.remoting.transport.Connector;
35
import org.jboss.test.remoting.TestUtil;
36
import org.jboss.test.remoting.performance.PerformanceReporter;
38
import java.util.HashMap;
43
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
45
public class PerformanceClientTest extends TestCase
47
private Client client;
48
private Connector connector;
49
private InvokerLocator locator;
51
private Latch serverDoneLock = new Latch();
53
// default transport and port
54
private String transport = "socket";
55
private String serialization = "";
56
private String metadata = null;
57
private int port = 9090;
59
// performance specific variables
60
private int numberOfCalls = 500;
61
private int sizeOfPayload = 1024;
63
private String sessionId = null;
65
// statics for the specific call methods
66
public static final String NUM_OF_CALLS = "numOfCalls";
67
public static final String TEST_INVOCATION = "testInvocation";
69
protected static final Logger log = Logger.getLogger(PerformanceClientTest.class);
71
protected String host = "localhost";
73
public static Test suite()
75
return new ThreadLocalDecorator(PerformanceClientTest.class, 1);
78
public InvokerLocator getInvokerLocator()
87
String locatorURI = getTransport() + "://" + host + ":" + getPort();
90
locatorURI = locatorURI + "/?" + metadata;
92
InvokerLocator locator = new InvokerLocator(locatorURI);
93
System.out.println("starting remoting client with locator of " + locator);
94
log.debug("starting remoting client with locator of " + locator);
95
client = new Client(locator, "performance");
97
log.info("Client connected to " + locator.getLocatorURI());
101
log.error(e.getMessage(), e);
104
sessionId = client.getSessionId();
107
public String getTransport()
112
public String getSerialization()
114
return serialization;
122
protected String getBenchmarkName()
124
return "PerformanceClientTest";
128
* This will be used to create callback server
134
protected InvokerLocator initServer(int port) throws Exception
138
port = TestUtil.getRandomPort();
140
log.debug("port = " + port);
142
connector = new Connector();
143
String locatorURI = getTransport() + "://" + host + ":" + port;
146
locatorURI = locatorURI + "/?" + metadata;
148
InvokerLocator locator = new InvokerLocator(locatorURI);
149
connector.setInvokerLocator(locator.getLocatorURI());
152
log.info("Callback server started on " + locator.getLocatorURI());
157
public void setUp() throws Exception
159
String newTransport = System.getProperty(PerformanceTestCase.REMOTING_TRANSPORT);
160
if(newTransport != null && newTransport.length() > 0)
162
transport = newTransport;
163
log.info("Using transport: " + transport);
166
String newHost = System.getProperty(PerformanceTestCase.REMOTING_HOST);
167
if(newHost != null && newHost.length() > 0)
170
log.info("Using host: " + host);
173
String newSerialization = System.getProperty(PerformanceTestCase.REMOTING_SERIALIZATION);
174
if(newSerialization != null && newSerialization.length() > 0)
176
serialization = newSerialization;
177
log.info("Using serialization: " + serialization);
180
String newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA);
181
if(newMetadata != null && newMetadata.length() > 0)
183
metadata = newMetadata;
184
log.info("Using metadata: " + metadata);
187
newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA_CALLBACK);
188
if(newMetadata != null && newMetadata.length() > 0)
192
metadata = newMetadata;
196
metadata += newMetadata;
199
log.info("Using metadata: " + metadata);
202
String payloadSize = System.getProperty(PerformanceTestCase.PAYLOAD_SIZE);
203
if(payloadSize != null && payloadSize.length() > 0)
207
sizeOfPayload = Integer.parseInt(payloadSize);
208
log.info("Using payload size: " + sizeOfPayload);
210
catch(NumberFormatException e)
216
String numOfCallsParam = System.getProperty(PerformanceTestCase.NUMBER_OF_CALLS);
217
if(numOfCallsParam != null && numOfCallsParam.length() > 0)
221
numberOfCalls = Integer.parseInt(numOfCallsParam);
222
log.info("Using number of calls: " + numberOfCalls);
224
catch(NumberFormatException e)
230
locator = initServer(-1);
234
public void tearDown() throws Exception
236
if(connector != null)
250
protected int getNumberOfCalls()
252
return numberOfCalls;
255
protected int getPayloadSize()
257
return sizeOfPayload;
260
public void testClientCalls() throws Throwable
262
Map metadata = new HashMap();
263
populateMetadata(metadata);
265
ThreadLocalBenchmark.openBench(getBenchmarkName(), metadata);
267
log.debug("running testSynchronousCalls()");
268
log.info("This class is " + getBenchmarkName());
271
log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
274
ThreadLocalBenchmark.openBench("Adding callback listener");
276
PerformanceCallbackKeeper handler = addCallbackListener(sessionId, serverDoneLock);
278
ThreadLocalBenchmark.closeBench("Adding callback listener");
280
ThreadLocalBenchmark.openBench("Simple invocation");
282
// simple invoke, should return bar
283
Object ret = makeInvocation(NUM_OF_CALLS, new Integer(getNumberOfCalls()));
285
ThreadLocalBenchmark.closeBench("Simple invocation");
287
// // create the payload object
288
// byte[] payload = new byte[getPayloadSize()];
289
// Payload payloadWrapper = new Payload(payload);
291
// THIS IS WHERE TO START THE TIMING
292
ThreadLocalBenchmark.openBench("Client calls");
293
long startTime = System.currentTimeMillis();
295
for(int x = 0; x < getNumberOfCalls(); x++)
297
// create the payload object
298
byte[] payload = new byte[getPayloadSize()];
299
Payload payloadWrapper = new Payload(payload);
300
// payloadWrapper.setCallNumber(callCounter.increment());
301
payloadWrapper.setCallNumber(x);
302
Object resp = makeClientCall(TEST_INVOCATION, payloadWrapper);
303
verifyResults(x, resp);
306
long endCallTime = System.currentTimeMillis();
307
System.out.println("Time to make all " + getNumberOfCalls() + " is " + (endCallTime - startTime));
308
ThreadLocalBenchmark.closeBench("Client calls");
310
//TODO: -TME Should make this configurable?
311
// will make timeout 2 seconds per call
312
long timeoutPeriod = 2000 * getNumberOfCalls();
313
boolean didComplete = serverDoneLock.attempt(timeoutPeriod);
314
long endProcessTime = System.currentTimeMillis();
317
int numProcessed = handler.getNumberOfCallsProcessed();
318
int numOfDuplicates = handler.getNumberOfDuplicates();
319
long totalTime = (endProcessTime - startTime);
320
System.out.println("Time for server to process " + numProcessed + " is " + totalTime);
321
if(getNumberOfCalls() == numProcessed)
323
System.out.println("PASSED - the server processed all calls.");
327
System.out.println("FAILED - the server did NOT process all calls.");
329
assertEquals("The total number of calls should equal total number processed.", getNumberOfCalls(), numProcessed);
330
assertEquals("The number of duplicates should be 0.", 0, numOfDuplicates);
332
//TODO: -TME - This needs to be replaced by benchmark code reporting
333
// metadata.put("server total count", String.valueOf(numProcessed));
336
// PerformanceReporter.writeReport(this.getClass().getName(),
337
// totalTime, getNumberOfCalls(), metadata);
342
System.out.println("FAILED - timed out waiting for server to call back when done processing. Waited for " + timeoutPeriod);
343
PerformanceReporter.writeReport("Error in test. Server never replied that it finished processing.", 0, 0, null);
346
ThreadLocalBenchmark.closeBench(getBenchmarkName());
351
protected PerformanceCallbackKeeper addCallbackListener(String sessionId, Latch serverDoneLock)
354
PerformanceCallbackHandler handler = new PerformanceCallbackHandler(sessionId, serverDoneLock);
355
// Need to add callback listener to get callback when the server is done
356
client.addListener(handler, getInvokerLocator(), sessionId);
360
protected void populateMetadata(Map metadata)
362
metadata.put("alias", getBenchmarkAlias());
363
metadata.put("transport", getTransport());
364
metadata.put("number of client calls", String.valueOf(getNumberOfCalls()));
365
metadata.put("payload size", String.valueOf(getPayloadSize()));
366
metadata.put("serialization", serialization);
369
protected Object getBenchmarkAlias()
371
String config = System.getProperty("alias");
372
if(config == null || config.length() == 0)
374
config = System.getProperty("jboss-junit-configuration");
375
if(config == null || config.length() == 0)
377
config = getTransport() + "_" + getNumberOfCalls() + "_" + getPayloadSize() + "_" + serialization;
383
protected void verifyResults(int x, Object resp)
385
assertEquals("The call number should be same as the numbe returned.", x, ((Integer) resp).intValue());
388
protected Object makeClientCall(String testInvocation, Payload payloadWrapper) throws Throwable
390
return makeInvocation(TEST_INVOCATION, payloadWrapper);
393
protected Object makeInvocation(String method, Object param) throws Throwable
399
ret = client.invoke(new NameBasedInvocation(method,
401
new String[]{param.getClass().getName()}),
406
throw new Exception("To make invocation, must pass a valid, non null, Object as parameter.");
412
protected Object makeOnewayInvocation(String method, Object param, boolean isClientSide) throws Throwable
416
client.invokeOneway(new NameBasedInvocation(method,
418
new String[]{param.getClass().getName()}),
424
throw new Exception("To make oneway invocation, must pass a valid, non null, Object as parameter.");
430
public static void main(String[] args)
432
PerformanceClientTest test = new PerformanceClientTest();
436
test.testClientCalls();
439
catch(Throwable throwable)
441
throwable.printStackTrace();