KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > performance > synchronous > PerformanceClientTest


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.performance.synchronous;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
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 /**
27  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
28  */

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    // default transport and port
38
private String JavaDoc transport = "socket";
39    private int port = 9090;
40
41    // performance specific variables
42
private int numberOfCalls = 500;
43    private int sizeOfPayload = 1024;
44
45    // statics for the specific call methods
46
public static final String JavaDoc NUM_OF_CALLS = "numOfCalls";
47    public static final String JavaDoc 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 JavaDoc e)
61       {
62          log.error(e.getMessage(), e);
63       }
64    }
65
66    public String JavaDoc getTransport()
67    {
68       return transport;
69    }
70
71    public int getPort()
72    {
73       return port;
74    }
75
76    /**
77     * This will be used to create callback server
78     *
79     * @param port
80     * @return
81     * @throws Exception
82     */

83    private InvokerLocator initServer(int port) throws Exception JavaDoc
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 JavaDoc
102    {
103       String JavaDoc 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 JavaDoc 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 JavaDoc e)
119          {
120             e.printStackTrace();
121          }
122       }
123
124       String JavaDoc 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 JavaDoc e)
133          {
134             e.printStackTrace();
135          }
136       }
137
138       locator = initServer(-1);
139       init();
140    }
141
142    public void tearDown() throws Exception JavaDoc
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 JavaDoc
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       // Need to add callback listener to get callback when the server is done
175
client.addListener(handler, locator, client.getSessionId());
176
177       // simple invoke, should return bar
178
Object JavaDoc ret = makeInvocation(NUM_OF_CALLS, new Integer JavaDoc(getNumberOfCalls()));
179
180       // create the payload object
181
byte[] payload = new byte[getPayloadSize()];
182       Payload payloadWrapper = new Payload(payload);
183
184       // THIS IS WHERE TO START THE TIMING
185
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       //TODO: -TME Should make this configurable?
197
// will make timeout 2 seconds per call
198
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          //TODO: -TME - This needs to be replaced by benchmark code reporting
216
Map JavaDoc metadata = new HashMap JavaDoc();
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 JavaDoc testInvocation, Payload payloadWrapper) throws Throwable JavaDoc
237    {
238       makeInvocation(TEST_INVOCATION, payloadWrapper);
239    }
240
241    protected Object JavaDoc makeInvocation(String JavaDoc method, Object JavaDoc param) throws Throwable JavaDoc
242    {
243       Object JavaDoc ret = null;
244
245       if(param != null)
246       {
247          ret = client.invoke(new NameBasedInvocation(method,
248                                                      new Object JavaDoc[]{param},
249                                                      new String JavaDoc[]{param.getClass().getName()}),
250                              null);
251       }
252       else
253       {
254          throw new Exception JavaDoc("To make invocation, must pass a valid, non null, Object as parameter.");
255       }
256
257       return ret;
258    }
259
260    protected void makeOnewayInvocation(String JavaDoc method, Object JavaDoc param, boolean isClientSide) throws Throwable JavaDoc
261    {
262       if(param != null)
263       {
264          client.invokeOneway(new NameBasedInvocation(method,
265                                                      new Object JavaDoc[]{param},
266                                                      new String JavaDoc[]{param.getClass().getName()}),
267                              null,
268                              isClientSide);
269       }
270       else
271       {
272          throw new Exception JavaDoc("To make oneway invocation, must pass a valid, non null, Object as parameter.");
273       }
274    }
275
276
277    public static void main(String JavaDoc[] args)
278    {
279       PerformanceClientTest test = new PerformanceClientTest();
280       try
281       {
282          test.setUp();
283          test.testClientCalls();
284          test.tearDown();
285       }
286       catch(Throwable JavaDoc throwable)
287       {
288          throwable.printStackTrace();
289       }
290    }
291
292    public static class PerformanceCallbackHandler implements InvokerCallbackHandler
293    {
294       private String JavaDoc sessionId;
295       private Latch lock;
296       private int numberOfCallsProcessed = 0;
297
298       public PerformanceCallbackHandler(String JavaDoc 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       /**
310        * Will take the callback message and send back to client.
311        * If client locator is null, will store them till client polls to get them.
312        *
313        * @param callback
314        * @throws org.jboss.remoting.callback.HandleCallbackException
315        *
316        */

317       public void handleCallback(Callback callback) throws HandleCallbackException
318       {
319             Object JavaDoc ret = callback.getCallbackObject();
320             Integer JavaDoc numOfCallsHandled = (Integer JavaDoc) ret;
321             System.out.println("Server is done. Number of calls handled: " + numOfCallsHandled);
322             numberOfCallsProcessed = numOfCallsHandled.intValue();
323             Object JavaDoc obj = callback.getCallbackHandleObject();
324             String JavaDoc handbackObj = (String JavaDoc) 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