1 7 package org.jboss.test.remoting.performance.synchronous; 8 9 import java.util.HashMap ; 10 import java.util.Map ; 11 import org.jboss.logging.Logger; 12 import org.jboss.remoting.Client; 13 import org.jboss.remoting.InvokerLocator; 14 import org.jboss.remoting.callback.Callback; 15 import org.jboss.remoting.callback.HandleCallbackException; 16 import org.jboss.remoting.callback.InvokerCallbackHandler; 17 import org.jboss.remoting.invocation.NameBasedInvocation; 18 import org.jboss.remoting.transport.Connector; 19 import org.jboss.test.remoting.TestUtil; 20 import org.jboss.test.remoting.performance.PerformanceReporter; 21 22 import EDU.oswego.cs.dl.util.concurrent.Latch; 23 import junit.framework.TestCase; 24 25 26 29 public class PerformanceClientTest extends TestCase 30 { 31 private Client client; 32 private Connector connector; 33 private InvokerLocator locator; 34 35 private Latch serverDoneLock = new Latch(); 36 37 private String transport = "socket"; 39 private int port = 9090; 40 41 private int numberOfCalls = 500; 43 private int sizeOfPayload = 1024; 44 45 public static final String NUM_OF_CALLS = "numOfCalls"; 47 public static final String TEST_INVOCATION = "testInvocation"; 48 49 private static final Logger log = Logger.getLogger(PerformanceClientTest.class); 50 51 public void init() 52 { 53 try 54 { 55 InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + getPort()); 56 client = new Client(locator, "performance"); 57 client.connect(); 58 log.info("Client connected to " + locator.getLocatorURI()); 59 } 60 catch(Exception e) 61 { 62 log.error(e.getMessage(), e); 63 } 64 } 65 66 public String getTransport() 67 { 68 return transport; 69 } 70 71 public int getPort() 72 { 73 return port; 74 } 75 76 83 private InvokerLocator initServer(int port) throws Exception 84 { 85 if(port < 0) 86 { 87 port = TestUtil.getRandomPort(); 88 } 89 log.debug("port = " + port); 90 91 connector = new Connector(); 92 InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port); 93 connector.setInvokerLocator(locator.getLocatorURI()); 94 connector.create(); 95 connector.start(); 96 log.info("Callback server started on " + locator.getLocatorURI()); 97 return locator; 98 } 99 100 101 public void setUp() throws Exception 102 { 103 String newTransport = System.getProperty(PerformanceTestCase.REMOTING_TRANSPORT); 104 if(newTransport != null && newTransport.length() > 0) 105 { 106 transport = newTransport; 107 log.info("Using transport: " + transport); 108 } 109 110 String payloadSize = System.getProperty(PerformanceTestCase.PAYLOAD_SIZE); 111 if(payloadSize != null && payloadSize.length() > 0) 112 { 113 try 114 { 115 sizeOfPayload = Integer.parseInt(payloadSize); 116 log.info("Using payload size: " + sizeOfPayload); 117 } 118 catch(NumberFormatException e) 119 { 120 e.printStackTrace(); 121 } 122 } 123 124 String numOfCallsParam = System.getProperty(PerformanceTestCase.NUMBER_OF_CALLS); 125 if(numOfCallsParam != null && numOfCallsParam.length() > 0) 126 { 127 try 128 { 129 numberOfCalls = Integer.parseInt(numOfCallsParam); 130 log.info("Using number of calls: " + numberOfCalls); 131 } 132 catch(NumberFormatException e) 133 { 134 e.printStackTrace(); 135 } 136 } 137 138 locator = initServer(-1); 139 init(); 140 } 141 142 public void tearDown() throws Exception 143 { 144 if(connector != null) 145 { 146 connector.stop(); 147 connector.destroy(); 148 connector = null; 149 } 150 locator = null; 151 if(client != null) 152 { 153 client.disconnect(); 154 client = null; 155 } 156 } 157 158 protected int getNumberOfCalls() 159 { 160 return numberOfCalls; 161 } 162 163 protected int getPayloadSize() 164 { 165 return sizeOfPayload; 166 } 167 168 public void testClientCalls() throws Throwable 169 { 170 log.debug("running testSynchronousCalls()"); 171 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 172 173 PerformanceCallbackHandler handler = new PerformanceCallbackHandler(client.getSessionId(), serverDoneLock); 174 client.addListener(handler, locator, client.getSessionId()); 176 177 Object ret = makeInvocation(NUM_OF_CALLS, new Integer (getNumberOfCalls())); 179 180 byte[] payload = new byte[getPayloadSize()]; 182 Payload payloadWrapper = new Payload(payload); 183 184 long startTime = System.currentTimeMillis(); 186 187 for(int x = 0; x < getNumberOfCalls(); x++) 188 { 189 payloadWrapper.setCallNumber(x); 190 makeClientCall(TEST_INVOCATION, payloadWrapper); 191 } 192 193 long endCallTime = System.currentTimeMillis(); 194 System.out.println("Time to make all " + getNumberOfCalls() + " is " + (endCallTime - startTime)); 195 196 long timeoutPeriod = 2000 * getNumberOfCalls(); 199 boolean didComplete = serverDoneLock.attempt(timeoutPeriod); 200 long endProcessTime = System.currentTimeMillis(); 201 if(didComplete) 202 { 203 int numProcessed = handler.getNumberOfCallsProcessed(); 204 long totalTime = (endProcessTime - startTime); 205 System.out.println("Time for server to process " + numProcessed + " is " + totalTime); 206 if(getNumberOfCalls() == numProcessed) 207 { 208 System.out.println("PASSED - the server processed all calls."); 209 } 210 else 211 { 212 System.out.println("FAILED - the server did NOT process all calls."); 213 } 214 215 Map metadata = new HashMap (); 217 metadata.put("transport", getTransport()); 218 metadata.put("server total count", String.valueOf(numProcessed)); 219 metadata.put("number of client calls", String.valueOf(getNumberOfCalls())); 220 metadata.put("payload size", String.valueOf(getPayloadSize())); 221 222 223 PerformanceReporter.writeReport(this.getClass().getName(), 224 totalTime, getNumberOfCalls(), metadata); 225 226 } 227 else 228 { 229 System.out.println("FAILED - timed out waiting for server to call back when done processing. Waited for " + timeoutPeriod); 230 PerformanceReporter.writeReport("Error in test. Server never replied that it finished processing.", 0, 0, null); 231 } 232 233 234 } 235 236 protected void makeClientCall(String testInvocation, Payload payloadWrapper) throws Throwable 237 { 238 makeInvocation(TEST_INVOCATION, payloadWrapper); 239 } 240 241 protected Object makeInvocation(String method, Object param) throws Throwable 242 { 243 Object ret = null; 244 245 if(param != null) 246 { 247 ret = client.invoke(new NameBasedInvocation(method, 248 new Object []{param}, 249 new String []{param.getClass().getName()}), 250 null); 251 } 252 else 253 { 254 throw new Exception ("To make invocation, must pass a valid, non null, Object as parameter."); 255 } 256 257 return ret; 258 } 259 260 protected void makeOnewayInvocation(String method, Object param, boolean isClientSide) throws Throwable 261 { 262 if(param != null) 263 { 264 client.invokeOneway(new NameBasedInvocation(method, 265 new Object []{param}, 266 new String []{param.getClass().getName()}), 267 null, 268 isClientSide); 269 } 270 else 271 { 272 throw new Exception ("To make oneway invocation, must pass a valid, non null, Object as parameter."); 273 } 274 } 275 276 277 public static void main(String [] args) 278 { 279 PerformanceClientTest test = new PerformanceClientTest(); 280 try 281 { 282 test.setUp(); 283 test.testClientCalls(); 284 test.tearDown(); 285 } 286 catch(Throwable throwable) 287 { 288 throwable.printStackTrace(); 289 } 290 } 291 292 public static class PerformanceCallbackHandler implements InvokerCallbackHandler 293 { 294 private String sessionId; 295 private Latch lock; 296 private int numberOfCallsProcessed = 0; 297 298 public PerformanceCallbackHandler(String sessionId, Latch lock) 299 { 300 this.sessionId = sessionId; 301 this.lock = lock; 302 } 303 304 public int getNumberOfCallsProcessed() 305 { 306 return numberOfCallsProcessed; 307 } 308 309 317 public void handleCallback(Callback callback) throws HandleCallbackException 318 { 319 Object ret = callback.getCallbackObject(); 320 Integer numOfCallsHandled = (Integer ) ret; 321 System.out.println("Server is done. Number of calls handled: " + numOfCallsHandled); 322 numberOfCallsProcessed = numOfCallsHandled.intValue(); 323 Object obj = callback.getCallbackHandleObject(); 324 String handbackObj = (String ) obj; 325 System.out.println("Handback object should be " + sessionId + " and server called back with " + handbackObj); 326 lock.release(); 327 } 328 } 329 } 330 | Popular Tags |