File: PerformanceClientTest.java

package info (click to toggle)
libjboss-remoting-java 2.4.0~Beta2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 10,268 kB
  • ctags: 15,262
  • sloc: java: 115,889; xml: 1,019; makefile: 14; sh: 11
file content (445 lines) | stat: -rw-r--r-- 13,931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.test.remoting.performance.synchronous;

import EDU.oswego.cs.dl.util.concurrent.Latch;
import junit.framework.Test;
import junit.framework.TestCase;
import org.jboss.jrunit.controller.ThreadLocalBenchmark;
import org.jboss.jrunit.decorators.ThreadLocalDecorator;
import org.jboss.logging.Logger;
import org.jboss.remoting.Client;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.invocation.NameBasedInvocation;
import org.jboss.remoting.transport.Connector;
import org.jboss.test.remoting.TestUtil;
import org.jboss.test.remoting.performance.PerformanceReporter;

import java.util.HashMap;
import java.util.Map;


/**
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 */
public class PerformanceClientTest extends TestCase
{
   private Client client;
   private Connector connector;
   private InvokerLocator locator;

   private Latch serverDoneLock = new Latch();

   // default transport and port
   private String transport = "socket";
   private String serialization = "";
   private String metadata = null;
   private int port = 9090;

   // performance specific variables
   private int numberOfCalls = 500;
   private int sizeOfPayload = 1024;

   private String sessionId = null;

   // statics for the specific call methods
   public static final String NUM_OF_CALLS = "numOfCalls";
   public static final String TEST_INVOCATION = "testInvocation";

   protected static final Logger log = Logger.getLogger(PerformanceClientTest.class);

   protected String host = "localhost";

   public static Test suite()
   {
      return new ThreadLocalDecorator(PerformanceClientTest.class, 1);
   }

   public InvokerLocator getInvokerLocator()
   {
      return locator;
   }

   public void init()
   {
      try
      {
         String locatorURI = getTransport() + "://" + host + ":" + getPort();
         if(metadata != null)
         {
            locatorURI = locatorURI + "/?" + metadata;
         }
         InvokerLocator locator = new InvokerLocator(locatorURI);
         System.out.println("starting remoting client with locator of " + locator);
         log.debug("starting remoting client with locator of " + locator);
         client = new Client(locator, "performance");
         client.connect();
         log.info("Client connected to " + locator.getLocatorURI());
      }
      catch(Exception e)
      {
         log.error(e.getMessage(), e);
      }

      sessionId = client.getSessionId();
   }

   public String getTransport()
   {
      return transport;
   }

   public String getSerialization()
   {
      return serialization;
   }

   public int getPort()
   {
      return port;
   }

   protected String getBenchmarkName()
   {
      return "PerformanceClientTest";
   }

   /**
    * This will be used to create callback server
    *
    * @param port
    * @return
    * @throws Exception
    */
   protected InvokerLocator initServer(int port) throws Exception
   {
      if(port < 0)
      {
         port = TestUtil.getRandomPort();
      }
      log.debug("port = " + port);

      connector = new Connector();
      String locatorURI = getTransport() + "://" + host + ":" + port;
      if(metadata != null)
      {
         locatorURI = locatorURI + "/?" + metadata;
      }
      InvokerLocator locator = new InvokerLocator(locatorURI);
      connector.setInvokerLocator(locator.getLocatorURI());
      connector.create();
      connector.start();
      log.info("Callback server started on " + locator.getLocatorURI());
      return locator;
   }


   public void setUp() throws Exception
   {
      String newTransport = System.getProperty(PerformanceTestCase.REMOTING_TRANSPORT);
      if(newTransport != null && newTransport.length() > 0)
      {
         transport = newTransport;
         log.info("Using transport: " + transport);
      }

      String newHost = System.getProperty(PerformanceTestCase.REMOTING_HOST);
      if(newHost != null && newHost.length() > 0)
      {
         host = newHost;
         log.info("Using host: " + host);
      }

      String newSerialization = System.getProperty(PerformanceTestCase.REMOTING_SERIALIZATION);
      if(newSerialization != null && newSerialization.length() > 0)
      {
         serialization = newSerialization;
         log.info("Using serialization: " + serialization);
      }

      String newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA);
      if(newMetadata != null && newMetadata.length() > 0)
      {
         metadata = newMetadata;
         log.info("Using metadata: " + metadata);
      }

      newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA_CALLBACK);
      if(newMetadata != null && newMetadata.length() > 0)
      {
         if(metadata == null)
         {
            metadata = newMetadata;
         }
         else
         {
            metadata += newMetadata;
         }

         log.info("Using metadata: " + metadata);
      }

      String payloadSize = System.getProperty(PerformanceTestCase.PAYLOAD_SIZE);
      if(payloadSize != null && payloadSize.length() > 0)
      {
         try
         {
            sizeOfPayload = Integer.parseInt(payloadSize);
            log.info("Using payload size: " + sizeOfPayload);
         }
         catch(NumberFormatException e)
         {
            e.printStackTrace();
         }
      }

      String numOfCallsParam = System.getProperty(PerformanceTestCase.NUMBER_OF_CALLS);
      if(numOfCallsParam != null && numOfCallsParam.length() > 0)
      {
         try
         {
            numberOfCalls = Integer.parseInt(numOfCallsParam);
            log.info("Using number of calls: " + numberOfCalls);
         }
         catch(NumberFormatException e)
         {
            e.printStackTrace();
         }
      }

      locator = initServer(-1);
      init();
   }

   public void tearDown() throws Exception
   {
      if(connector != null)
      {
         connector.stop();
         connector.destroy();
         connector = null;
      }
      locator = null;
      if(client != null)
      {
         client.disconnect();
         client = null;
      }
   }

   protected int getNumberOfCalls()
   {
      return numberOfCalls;
   }

   protected int getPayloadSize()
   {
      return sizeOfPayload;
   }

   public void testClientCalls() throws Throwable
   {
      Map metadata = new HashMap();
      populateMetadata(metadata);

      ThreadLocalBenchmark.openBench(getBenchmarkName(), metadata);

      log.debug("running testSynchronousCalls()");
      log.info("This class is " + getBenchmarkName());
      if(client != null)
      {
         log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
      }

      ThreadLocalBenchmark.openBench("Adding callback listener");

      PerformanceCallbackKeeper handler = addCallbackListener(sessionId, serverDoneLock);

      ThreadLocalBenchmark.closeBench("Adding callback listener");

      ThreadLocalBenchmark.openBench("Simple invocation");

      // simple invoke, should return bar
      Object ret = makeInvocation(NUM_OF_CALLS, new Integer(getNumberOfCalls()));

      ThreadLocalBenchmark.closeBench("Simple invocation");

//      // create the payload object
//      byte[] payload = new byte[getPayloadSize()];
//      Payload payloadWrapper = new Payload(payload);

      // THIS IS WHERE TO START THE TIMING
      ThreadLocalBenchmark.openBench("Client calls");
      long startTime = System.currentTimeMillis();

      for(int x = 0; x < getNumberOfCalls(); x++)
      {
         // create the payload object
         byte[] payload = new byte[getPayloadSize()];
         Payload payloadWrapper = new Payload(payload);
//         payloadWrapper.setCallNumber(callCounter.increment());
         payloadWrapper.setCallNumber(x);
         Object resp = makeClientCall(TEST_INVOCATION, payloadWrapper);
         verifyResults(x, resp);
      }

      long endCallTime = System.currentTimeMillis();
      System.out.println("Time to make all " + getNumberOfCalls() + " is " + (endCallTime - startTime));
      ThreadLocalBenchmark.closeBench("Client calls");

      //TODO: -TME Should make this configurable?
      // will make timeout 2 seconds per call
      long timeoutPeriod = 2000 * getNumberOfCalls();
      boolean didComplete = serverDoneLock.attempt(timeoutPeriod);
      long endProcessTime = System.currentTimeMillis();
      if(didComplete)
      {
         int numProcessed = handler.getNumberOfCallsProcessed();
         int numOfDuplicates = handler.getNumberOfDuplicates();
         long totalTime = (endProcessTime - startTime);
         System.out.println("Time for server to process " + numProcessed + " is " + totalTime);
         if(getNumberOfCalls() == numProcessed)
         {
            System.out.println("PASSED - the server processed all calls.");
         }
         else
         {
            System.out.println("FAILED - the server did NOT process all calls.");
         }
         assertEquals("The total number of calls should equal total number processed.", getNumberOfCalls(), numProcessed);
         assertEquals("The number of duplicates should be 0.", 0, numOfDuplicates);

         //TODO: -TME - This needs to be replaced by benchmark code reporting
//         metadata.put("server total count", String.valueOf(numProcessed));
//
//
//         PerformanceReporter.writeReport(this.getClass().getName(),
//                                         totalTime, getNumberOfCalls(), metadata);

      }
      else
      {
         System.out.println("FAILED - timed out waiting for server to call back when done processing.  Waited for " + timeoutPeriod);
         PerformanceReporter.writeReport("Error in test.  Server never replied that it finished processing.", 0, 0, null);
      }

      ThreadLocalBenchmark.closeBench(getBenchmarkName());


   }

   protected PerformanceCallbackKeeper addCallbackListener(String sessionId, Latch serverDoneLock)
         throws Throwable
   {
      PerformanceCallbackHandler handler = new PerformanceCallbackHandler(sessionId, serverDoneLock);
      // Need to add callback listener to get callback when the server is done
      client.addListener(handler, getInvokerLocator(), sessionId);
      return handler;
   }

   protected void populateMetadata(Map metadata)
   {
      metadata.put("alias", getBenchmarkAlias());
      metadata.put("transport", getTransport());
      metadata.put("number of client calls", String.valueOf(getNumberOfCalls()));
      metadata.put("payload size", String.valueOf(getPayloadSize()));
      metadata.put("serialization", serialization);
   }

   protected Object getBenchmarkAlias()
   {
      String config = System.getProperty("alias");
      if(config == null || config.length() == 0)
      {
         config = System.getProperty("jboss-junit-configuration");
         if(config == null || config.length() == 0)
         {
            config = getTransport() + "_" + getNumberOfCalls() + "_" + getPayloadSize() + "_" + serialization;
         }
      }
      return config;
   }

   protected void verifyResults(int x, Object resp)
   {
      assertEquals("The call number should be same as the numbe returned.", x, ((Integer) resp).intValue());
   }

   protected Object makeClientCall(String testInvocation, Payload payloadWrapper) throws Throwable
   {
      return makeInvocation(TEST_INVOCATION, payloadWrapper);
   }

   protected Object makeInvocation(String method, Object param) throws Throwable
   {
      Object ret = null;

      if(param != null)
      {
         ret = client.invoke(new NameBasedInvocation(method,
                                                     new Object[]{param},
                                                     new String[]{param.getClass().getName()}),
                             null);
      }
      else
      {
         throw new Exception("To make invocation, must pass a valid, non null, Object as parameter.");
      }

      return ret;
   }

   protected Object makeOnewayInvocation(String method, Object param, boolean isClientSide) throws Throwable
   {
      if(param != null)
      {
         client.invokeOneway(new NameBasedInvocation(method,
                                                     new Object[]{param},
                                                     new String[]{param.getClass().getName()}),
                             null,
                             isClientSide);
      }
      else
      {
         throw new Exception("To make oneway invocation, must pass a valid, non null, Object as parameter.");
      }
      return null;
   }


   public static void main(String[] args)
   {
      PerformanceClientTest test = new PerformanceClientTest();
      try
      {
         test.setUp();
         test.testClientCalls();
         test.tearDown();
      }
      catch(Throwable throwable)
      {
         throwable.printStackTrace();
      }
   }

}