~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to .pc/0001-convert-to-official-Java-concurrent-packages.patch/src/tests/org/jboss/test/remoting/performance/synchronous/PerformanceClientTest.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
 
 
23
package org.jboss.test.remoting.performance.synchronous;
 
24
 
 
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;
 
37
 
 
38
import java.util.HashMap;
 
39
import java.util.Map;
 
40
 
 
41
 
 
42
/**
 
43
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
44
 */
 
45
public class PerformanceClientTest extends TestCase
 
46
{
 
47
   private Client client;
 
48
   private Connector connector;
 
49
   private InvokerLocator locator;
 
50
 
 
51
   private Latch serverDoneLock = new Latch();
 
52
 
 
53
   // default transport and port
 
54
   private String transport = "socket";
 
55
   private String serialization = "";
 
56
   private String metadata = null;
 
57
   private int port = 9090;
 
58
 
 
59
   // performance specific variables
 
60
   private int numberOfCalls = 500;
 
61
   private int sizeOfPayload = 1024;
 
62
 
 
63
   private String sessionId = null;
 
64
 
 
65
   // statics for the specific call methods
 
66
   public static final String NUM_OF_CALLS = "numOfCalls";
 
67
   public static final String TEST_INVOCATION = "testInvocation";
 
68
 
 
69
   protected static final Logger log = Logger.getLogger(PerformanceClientTest.class);
 
70
 
 
71
   protected String host = "localhost";
 
72
 
 
73
   public static Test suite()
 
74
   {
 
75
      return new ThreadLocalDecorator(PerformanceClientTest.class, 1);
 
76
   }
 
77
 
 
78
   public InvokerLocator getInvokerLocator()
 
79
   {
 
80
      return locator;
 
81
   }
 
82
 
 
83
   public void init()
 
84
   {
 
85
      try
 
86
      {
 
87
         String locatorURI = getTransport() + "://" + host + ":" + getPort();
 
88
         if(metadata != null)
 
89
         {
 
90
            locatorURI = locatorURI + "/?" + metadata;
 
91
         }
 
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");
 
96
         client.connect();
 
97
         log.info("Client connected to " + locator.getLocatorURI());
 
98
      }
 
99
      catch(Exception e)
 
100
      {
 
101
         log.error(e.getMessage(), e);
 
102
      }
 
103
 
 
104
      sessionId = client.getSessionId();
 
105
   }
 
106
 
 
107
   public String getTransport()
 
108
   {
 
109
      return transport;
 
110
   }
 
111
 
 
112
   public String getSerialization()
 
113
   {
 
114
      return serialization;
 
115
   }
 
116
 
 
117
   public int getPort()
 
118
   {
 
119
      return port;
 
120
   }
 
121
 
 
122
   protected String getBenchmarkName()
 
123
   {
 
124
      return "PerformanceClientTest";
 
125
   }
 
126
 
 
127
   /**
 
128
    * This will be used to create callback server
 
129
    *
 
130
    * @param port
 
131
    * @return
 
132
    * @throws Exception
 
133
    */
 
134
   protected InvokerLocator initServer(int port) throws Exception
 
135
   {
 
136
      if(port < 0)
 
137
      {
 
138
         port = TestUtil.getRandomPort();
 
139
      }
 
140
      log.debug("port = " + port);
 
141
 
 
142
      connector = new Connector();
 
143
      String locatorURI = getTransport() + "://" + host + ":" + port;
 
144
      if(metadata != null)
 
145
      {
 
146
         locatorURI = locatorURI + "/?" + metadata;
 
147
      }
 
148
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
149
      connector.setInvokerLocator(locator.getLocatorURI());
 
150
      connector.create();
 
151
      connector.start();
 
152
      log.info("Callback server started on " + locator.getLocatorURI());
 
153
      return locator;
 
154
   }
 
155
 
 
156
 
 
157
   public void setUp() throws Exception
 
158
   {
 
159
      String newTransport = System.getProperty(PerformanceTestCase.REMOTING_TRANSPORT);
 
160
      if(newTransport != null && newTransport.length() > 0)
 
161
      {
 
162
         transport = newTransport;
 
163
         log.info("Using transport: " + transport);
 
164
      }
 
165
 
 
166
      String newHost = System.getProperty(PerformanceTestCase.REMOTING_HOST);
 
167
      if(newHost != null && newHost.length() > 0)
 
168
      {
 
169
         host = newHost;
 
170
         log.info("Using host: " + host);
 
171
      }
 
172
 
 
173
      String newSerialization = System.getProperty(PerformanceTestCase.REMOTING_SERIALIZATION);
 
174
      if(newSerialization != null && newSerialization.length() > 0)
 
175
      {
 
176
         serialization = newSerialization;
 
177
         log.info("Using serialization: " + serialization);
 
178
      }
 
179
 
 
180
      String newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA);
 
181
      if(newMetadata != null && newMetadata.length() > 0)
 
182
      {
 
183
         metadata = newMetadata;
 
184
         log.info("Using metadata: " + metadata);
 
185
      }
 
186
 
 
187
      newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA_CALLBACK);
 
188
      if(newMetadata != null && newMetadata.length() > 0)
 
189
      {
 
190
         if(metadata == null)
 
191
         {
 
192
            metadata = newMetadata;
 
193
         }
 
194
         else
 
195
         {
 
196
            metadata += newMetadata;
 
197
         }
 
198
 
 
199
         log.info("Using metadata: " + metadata);
 
200
      }
 
201
 
 
202
      String payloadSize = System.getProperty(PerformanceTestCase.PAYLOAD_SIZE);
 
203
      if(payloadSize != null && payloadSize.length() > 0)
 
204
      {
 
205
         try
 
206
         {
 
207
            sizeOfPayload = Integer.parseInt(payloadSize);
 
208
            log.info("Using payload size: " + sizeOfPayload);
 
209
         }
 
210
         catch(NumberFormatException e)
 
211
         {
 
212
            e.printStackTrace();
 
213
         }
 
214
      }
 
215
 
 
216
      String numOfCallsParam = System.getProperty(PerformanceTestCase.NUMBER_OF_CALLS);
 
217
      if(numOfCallsParam != null && numOfCallsParam.length() > 0)
 
218
      {
 
219
         try
 
220
         {
 
221
            numberOfCalls = Integer.parseInt(numOfCallsParam);
 
222
            log.info("Using number of calls: " + numberOfCalls);
 
223
         }
 
224
         catch(NumberFormatException e)
 
225
         {
 
226
            e.printStackTrace();
 
227
         }
 
228
      }
 
229
 
 
230
      locator = initServer(-1);
 
231
      init();
 
232
   }
 
233
 
 
234
   public void tearDown() throws Exception
 
235
   {
 
236
      if(connector != null)
 
237
      {
 
238
         connector.stop();
 
239
         connector.destroy();
 
240
         connector = null;
 
241
      }
 
242
      locator = null;
 
243
      if(client != null)
 
244
      {
 
245
         client.disconnect();
 
246
         client = null;
 
247
      }
 
248
   }
 
249
 
 
250
   protected int getNumberOfCalls()
 
251
   {
 
252
      return numberOfCalls;
 
253
   }
 
254
 
 
255
   protected int getPayloadSize()
 
256
   {
 
257
      return sizeOfPayload;
 
258
   }
 
259
 
 
260
   public void testClientCalls() throws Throwable
 
261
   {
 
262
      Map metadata = new HashMap();
 
263
      populateMetadata(metadata);
 
264
 
 
265
      ThreadLocalBenchmark.openBench(getBenchmarkName(), metadata);
 
266
 
 
267
      log.debug("running testSynchronousCalls()");
 
268
      log.info("This class is " + getBenchmarkName());
 
269
      if(client != null)
 
270
      {
 
271
         log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
 
272
      }
 
273
 
 
274
      ThreadLocalBenchmark.openBench("Adding callback listener");
 
275
 
 
276
      PerformanceCallbackKeeper handler = addCallbackListener(sessionId, serverDoneLock);
 
277
 
 
278
      ThreadLocalBenchmark.closeBench("Adding callback listener");
 
279
 
 
280
      ThreadLocalBenchmark.openBench("Simple invocation");
 
281
 
 
282
      // simple invoke, should return bar
 
283
      Object ret = makeInvocation(NUM_OF_CALLS, new Integer(getNumberOfCalls()));
 
284
 
 
285
      ThreadLocalBenchmark.closeBench("Simple invocation");
 
286
 
 
287
//      // create the payload object
 
288
//      byte[] payload = new byte[getPayloadSize()];
 
289
//      Payload payloadWrapper = new Payload(payload);
 
290
 
 
291
      // THIS IS WHERE TO START THE TIMING
 
292
      ThreadLocalBenchmark.openBench("Client calls");
 
293
      long startTime = System.currentTimeMillis();
 
294
 
 
295
      for(int x = 0; x < getNumberOfCalls(); x++)
 
296
      {
 
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);
 
304
      }
 
305
 
 
306
      long endCallTime = System.currentTimeMillis();
 
307
      System.out.println("Time to make all " + getNumberOfCalls() + " is " + (endCallTime - startTime));
 
308
      ThreadLocalBenchmark.closeBench("Client calls");
 
309
 
 
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();
 
315
      if(didComplete)
 
316
      {
 
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)
 
322
         {
 
323
            System.out.println("PASSED - the server processed all calls.");
 
324
         }
 
325
         else
 
326
         {
 
327
            System.out.println("FAILED - the server did NOT process all calls.");
 
328
         }
 
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);
 
331
 
 
332
         //TODO: -TME - This needs to be replaced by benchmark code reporting
 
333
//         metadata.put("server total count", String.valueOf(numProcessed));
 
334
//
 
335
//
 
336
//         PerformanceReporter.writeReport(this.getClass().getName(),
 
337
//                                         totalTime, getNumberOfCalls(), metadata);
 
338
 
 
339
      }
 
340
      else
 
341
      {
 
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);
 
344
      }
 
345
 
 
346
      ThreadLocalBenchmark.closeBench(getBenchmarkName());
 
347
 
 
348
 
 
349
   }
 
350
 
 
351
   protected PerformanceCallbackKeeper addCallbackListener(String sessionId, Latch serverDoneLock)
 
352
         throws Throwable
 
353
   {
 
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);
 
357
      return handler;
 
358
   }
 
359
 
 
360
   protected void populateMetadata(Map metadata)
 
361
   {
 
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);
 
367
   }
 
368
 
 
369
   protected Object getBenchmarkAlias()
 
370
   {
 
371
      String config = System.getProperty("alias");
 
372
      if(config == null || config.length() == 0)
 
373
      {
 
374
         config = System.getProperty("jboss-junit-configuration");
 
375
         if(config == null || config.length() == 0)
 
376
         {
 
377
            config = getTransport() + "_" + getNumberOfCalls() + "_" + getPayloadSize() + "_" + serialization;
 
378
         }
 
379
      }
 
380
      return config;
 
381
   }
 
382
 
 
383
   protected void verifyResults(int x, Object resp)
 
384
   {
 
385
      assertEquals("The call number should be same as the numbe returned.", x, ((Integer) resp).intValue());
 
386
   }
 
387
 
 
388
   protected Object makeClientCall(String testInvocation, Payload payloadWrapper) throws Throwable
 
389
   {
 
390
      return makeInvocation(TEST_INVOCATION, payloadWrapper);
 
391
   }
 
392
 
 
393
   protected Object makeInvocation(String method, Object param) throws Throwable
 
394
   {
 
395
      Object ret = null;
 
396
 
 
397
      if(param != null)
 
398
      {
 
399
         ret = client.invoke(new NameBasedInvocation(method,
 
400
                                                     new Object[]{param},
 
401
                                                     new String[]{param.getClass().getName()}),
 
402
                             null);
 
403
      }
 
404
      else
 
405
      {
 
406
         throw new Exception("To make invocation, must pass a valid, non null, Object as parameter.");
 
407
      }
 
408
 
 
409
      return ret;
 
410
   }
 
411
 
 
412
   protected Object makeOnewayInvocation(String method, Object param, boolean isClientSide) throws Throwable
 
413
   {
 
414
      if(param != null)
 
415
      {
 
416
         client.invokeOneway(new NameBasedInvocation(method,
 
417
                                                     new Object[]{param},
 
418
                                                     new String[]{param.getClass().getName()}),
 
419
                             null,
 
420
                             isClientSide);
 
421
      }
 
422
      else
 
423
      {
 
424
         throw new Exception("To make oneway invocation, must pass a valid, non null, Object as parameter.");
 
425
      }
 
426
      return null;
 
427
   }
 
428
 
 
429
 
 
430
   public static void main(String[] args)
 
431
   {
 
432
      PerformanceClientTest test = new PerformanceClientTest();
 
433
      try
 
434
      {
 
435
         test.setUp();
 
436
         test.testClientCalls();
 
437
         test.tearDown();
 
438
      }
 
439
      catch(Throwable throwable)
 
440
      {
 
441
         throwable.printStackTrace();
 
442
      }
 
443
   }
 
444
 
 
445
}