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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/bisocket/BisocketPerformanceTestClient.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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
package org.jboss.test.remoting.transport.bisocket;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
 
 
27
import javax.management.MBeanServer;
 
28
 
 
29
import junit.framework.TestCase;
 
30
 
 
31
import org.jboss.logging.Logger;
 
32
import org.jboss.remoting.Client;
 
33
import org.jboss.remoting.InvocationRequest;
 
34
import org.jboss.remoting.InvokerLocator;
 
35
import org.jboss.remoting.ServerInvocationHandler;
 
36
import org.jboss.remoting.ServerInvoker;
 
37
import org.jboss.remoting.callback.Callback;
 
38
import org.jboss.remoting.callback.HandleCallbackException;
 
39
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
40
 
 
41
/** 
 
42
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
43
 * @version $Revision: 1702 $
 
44
 * <p>
 
45
 * Copyright Nov 25, 2006
 
46
 * </p>
 
47
 */
 
48
public class BisocketPerformanceTestClient extends TestCase
 
49
{
 
50
   private static Logger log = Logger.getLogger(BisocketPerformanceTestClient.class);
 
51
   private static String transport = "bisocket";
 
52
   private static int invocations = 1000;
 
53
   
 
54
   
 
55
   public void testBisocket() throws Throwable
 
56
   {
 
57
      String host = InetAddress.getLocalHost().getHostAddress();
 
58
      int port = BisocketPerformanceTestServer.port;
 
59
      String locatorURI = transport + "://" + host + ":" + port; 
 
60
      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
 
61
      System.out.println("Connecting to: " + serverLocator);
 
62
      Client client = new Client(serverLocator);
 
63
      client.connect();
 
64
      System.out.println("client is connected");
 
65
      InvokerCallbackHandler callbackHandler = new SampleCallbackHandler();
 
66
      client.addListener(callbackHandler, new HashMap(), null, true);
 
67
      System.out.println("client added callback handler");
 
68
      
 
69
      long start = System.currentTimeMillis();
 
70
      for (int i = 0; i < invocations; i++)
 
71
      {
 
72
         client.invoke("test");
 
73
      }
 
74
      long finish = System.currentTimeMillis();
 
75
      System.out.println("time to make " + invocations + " invocations: " + (finish - start));
 
76
      client.removeListener(callbackHandler);
 
77
      client.disconnect();
 
78
   }
 
79
   
 
80
   
 
81
   /**
 
82
    * Can pass transport and port to be used as parameters.
 
83
    *
 
84
    * @param args
 
85
    */
 
86
   public static void main(String[] args)
 
87
   {
 
88
      if(args != null && args.length > 0)
 
89
      {
 
90
         transport = args[0];
 
91
         if (args.length > 1)
 
92
            invocations = Integer.parseInt(args[1]);
 
93
      }
 
94
      BisocketPerformanceTestClient testCase = new BisocketPerformanceTestClient();
 
95
      try
 
96
      {
 
97
         testCase.setUp();
 
98
         testCase.testBisocket();
 
99
         Thread.sleep(10000);
 
100
         testCase.tearDown();
 
101
         System.out.println("done");
 
102
      }
 
103
      catch(Throwable e)
 
104
      {
 
105
         e.printStackTrace();
 
106
      }
 
107
   }
 
108
 
 
109
   /**
 
110
    * Simple invocation handler implementation.  When callback client's are registered, will
 
111
    * generate callbacks periodically.
 
112
    */
 
113
   static class SampleInvocationHandler implements ServerInvocationHandler
 
114
   {
 
115
      public void addListener(InvokerCallbackHandler callbackHandler)
 
116
      {
 
117
         System.out.println("Adding callback listener.");
 
118
         try
 
119
         {
 
120
            callbackHandler.handleCallback(new Callback("callback"));
 
121
         }
 
122
         catch (HandleCallbackException e)
 
123
         {
 
124
            log.error("Unable to send callback");
 
125
         }
 
126
      }
 
127
 
 
128
      public Object invoke(InvocationRequest invocation) throws Throwable {return null;}
 
129
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
130
      public void setMBeanServer(MBeanServer server) {}
 
131
      public void setInvoker(ServerInvoker invoker) {}
 
132
   }
 
133
   
 
134
   
 
135
   static class SampleCallbackHandler implements InvokerCallbackHandler
 
136
   {
 
137
      int counter;
 
138
      
 
139
      public void handleCallback(Callback callback) throws HandleCallbackException
 
140
      {
 
141
         counter++;
 
142
         if ((counter + 1) % 1000 == 0)
 
143
         {
 
144
            System.out.println("received callback " + (counter + 1));
 
145
         }
 
146
      }
 
147
   }
 
148
}