File: MultiThreadedPerformanceClientTest.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 (525 lines) | stat: -rw-r--r-- 16,683 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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* 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 EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
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.callback.Callback;
import org.jboss.remoting.callback.HandleCallbackException;
import org.jboss.remoting.callback.InvokerCallbackHandler;
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 MultiThreadedPerformanceClientTest 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";

   private static final Logger log = Logger.getLogger(MultiThreadedPerformanceClientTest.class);

   private SynchronizedInt callCounter = new SynchronizedInt(0);

   private int numOfThreads = 10;

   protected String host = "localhost";

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

   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 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;
   }

   /**
    * 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()");
      if(client != null)
      {
         log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
      }

      PerformanceCallbackKeeper handler = addCallbackListener(sessionId, serverDoneLock);

      ThreadLocalBenchmark.openBench("Adding callback listener");

      // Need to add callback listener to get callback when the server is done
//      client.addListener(handler, locator, client.getSessionId());

      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
      final byte[] payload = new byte[getPayloadSize()];
      // THIS IS WHERE TO START THE TIMING
      ThreadLocalBenchmark.openBench("Client calls");
      long startTime = System.currentTimeMillis();


      Thread[] threads = new Thread[numOfThreads];
      for(int x = 0; x < numOfThreads; x++)
      {
         Thread newThread = new Thread(new Runnable()
         {
            public void run()
            {
               try
               {
                  makeTestInvocation(payload);
               }
               catch(Throwable throwable)
               {
                  throwable.printStackTrace();
               }
            }
         });
         threads[x] = newThread;
         newThread.start();
      }

      // will make timeout 2 seconds per call
      long timeoutPeriod = 2000 * getNumberOfCalls();

      for(int i = 0; i < threads.length; i++)
      {
         threads[i].join(timeoutPeriod);
      }

      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?
      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());


   }

   public InvokerLocator getInvokerLocator()
   {
      return locator;
   }

   protected PerformanceCallbackKeeper addCallbackListener(String sessionId, Latch serverDoneLock)
         throws Throwable
   {
      org.jboss.test.remoting.performance.synchronous.PerformanceCallbackHandler handler = new org.jboss.test.remoting.performance.synchronous.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);
   }

   private String getBenchmarkName()
   {
      return "MultiThreadedPerformanceClientTest";
   }

   private void makeTestInvocation(byte[] payload)
         throws Throwable
   {
      Payload payloadWrapper = new Payload(payload);

      while(callCounter.get() < getNumberOfCalls())
      {
         int currentVal = callCounter.increment();
         payloadWrapper.setCallNumber(currentVal);
         Object resp = makeClientCall(TEST_INVOCATION, payloadWrapper);
         assertEquals(currentVal, ((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)
   {
      MultiThreadedPerformanceClientTest test = new MultiThreadedPerformanceClientTest();
      try
      {
         test.setUp();
         test.testClientCalls();
         test.tearDown();
      }
      catch(Throwable throwable)
      {
         throwable.printStackTrace();
      }
   }

   public static class PerformanceCallbackHandler implements InvokerCallbackHandler
   {
      private String sessionId;
      private Latch lock;
      private int numberOfCallsProcessed = 0;
      private int numberofDuplicates = 0;

      public PerformanceCallbackHandler(String sessionId, Latch lock)
      {
         this.sessionId = sessionId;
         this.lock = lock;
      }

      public int getNumberOfCallsProcessed()
      {
         return numberOfCallsProcessed;
      }

      public int getNumberOfDuplicates()
      {
         return numberofDuplicates;
      }

      /**
       * Will take the callback message and send back to client.
       * If client locator is null, will store them till client polls to get them.
       *
       * @param callback
       * @throws org.jboss.remoting.callback.HandleCallbackException
       *
       */
      public void handleCallback(Callback callback) throws HandleCallbackException
      {
         Object ret = callback.getCallbackObject();
         Integer[] handledArray = (Integer[]) ret;
         Integer numOfCallsHandled = (Integer) handledArray[0];
         Integer numOfDuplicates = (Integer) handledArray[1];
         System.out.println("Server is done.  Number of calls handled: " + numOfCallsHandled);
         numberOfCallsProcessed = numOfCallsHandled.intValue();
         System.out.println("Number of duplicate calls: " + numOfDuplicates);
         numberofDuplicates = numOfDuplicates.intValue();
         Object obj = callback.getCallbackHandleObject();
         String handbackObj = (String) obj;
         System.out.println("Handback object should be " + sessionId + " and server called back with " + handbackObj);
         lock.release();
      }
   }
}