View Javadoc

1   /*
2    * Copyright (c) 1998-2003 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.test.gm;
20  
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.rmi.RemoteException;
24  
25  import jgroup.core.GroupManager;
26  import jgroup.core.InternalGMIService;
27  import jgroup.core.JgroupException;
28  import jgroup.core.MemberId;
29  import jgroup.core.multicast.MulticastListener;
30  import jgroup.core.multicast.MulticastService;
31  import jgroup.relacs.config.AppConfig;
32  import jgroup.relacs.gm.DispatcherService;
33  import jgroup.relacs.rmi.SynchAckListener;
34  
35  import org.apache.log4j.Logger;
36  
37  
38  /**
39   *  Test layer for remote interaction styles; for the purpose of
40   *  testing the IGMI and Mcast layer performance.
41   *
42   *  @author Hein Meling
43   */
44  public class RemoteTestLayer
45    implements RemoteTestService, MulticastListener, InternalRemoteTest
46  {
47  
48    ////////////////////////////////////////////////////////////////////////////////////////////
49    // Logger
50    ////////////////////////////////////////////////////////////////////////////////////////////
51  
52    /** Obtain logger for this class */
53    private static Logger log = Logger.getLogger(RemoteTestLayer.class);
54  
55  
56    ////////////////////////////////////////////////////////////////////////////////////////////
57    // Static section
58    ////////////////////////////////////////////////////////////////////////////////////////////
59  
60    /** Values for the test runs */
61    private static final int NUM_CALLS = 2000;
62    private static final int INIT_CALLS = 2000;
63    private static final int INIT_SIZE = 0;
64    private static final int INC_SIZE = 5000;
65    private static final int MAX_SIZE = 20000;
66  
67    /** Protocol name used to distinguish messages from other protocols. */
68    private static final String PROTOCOL_NAME = "RTL";
69  
70  
71    ////////////////////////////////////////////////////////////////////////////////////////////
72    // Fields
73    ////////////////////////////////////////////////////////////////////////////////////////////
74  
75    /** The group manager instance that is in the process of being constructed */
76    private GroupManager gm;
77  
78    /** The multicast service */
79    private MulticastService multicastService;
80  
81    /** The dispatcher service */
82    private DispatcherService dispatcherService;
83  
84    /** The internal remote test interface */
85    private InternalRemoteTest irt;
86  
87    /** Configurable number of calls to perform. */
88    private int numCalls = NUM_CALLS;
89  
90    /** Configurable number of calls to perform as part of the initialization. */
91    private int initCalls = INIT_CALLS;
92  
93    /** Configurable size for the first run. */
94    private int initSize = INIT_SIZE;
95  
96    /** Configurable size to increment for each run. */
97    private int incSize = INC_SIZE;
98  
99    /** Configurable size for which we should not exceed. */
100   private int maxSize = MAX_SIZE;
101 
102   /** The interaction style to use between the RemoteTestLayer instances. */
103   private String interactionStyle;
104 
105   /** Set to true if the iteractions should halt. */
106   private volatile boolean halted = false;
107 
108 
109   ////////////////////////////////////////////////////////////////////////////////////////////
110   // Constructors
111   ////////////////////////////////////////////////////////////////////////////////////////////
112 
113   private RemoteTestLayer(GroupManager gm, DispatcherService dispatcher, MulticastService multicastService)
114     throws JgroupException, RemoteException
115   {
116     this.gm = gm;
117     this.dispatcherService = dispatcher;
118     this.multicastService = multicastService;
119   }
120 
121 
122   ////////////////////////////////////////////////////////////////////////////////////////////
123   // Static factory
124   ////////////////////////////////////////////////////////////////////////////////////////////
125 
126   /**
127    *  Get an instance of this layer; note that this layer needs the group manager
128    *  object and this must be passed to the layer as the first argument to the
129    *  getLayer method.
130    */
131   public static RemoteTestLayer getLayer(GroupManager gm, DispatcherService dispatcher,
132                                          InternalGMIService igmis, MulticastService mcast)
133     throws JgroupException, RemoteException
134   {
135     /*
136      * We ignore the IGMI service, since we don't need to invoke 
137      * addListener() on the igmis object.
138      */
139     return new RemoteTestLayer(gm, dispatcher, mcast);
140   }
141 
142 
143   ////////////////////////////////////////////////////////////////////////////////////////////
144   // Layer interface methods
145   ////////////////////////////////////////////////////////////////////////////////////////////
146 
147   /**
148    *  Add a server or layer that is listening for remote test layer events.
149    *  Actually, there is no events, so this is used for bootstrapping the
150    *  layer.  That is to read the configuration parameter.
151    */
152   public void addListener(Object listener)
153   {
154     AppConfig app = AppConfig.getApplication(listener);
155     interactionStyle = app.getParam("RemoteTest.interactionStyle");
156     numCalls = app.getIntParam("RemoteTest.numCalls", NUM_CALLS);
157     initCalls = app.getIntParam("RemoteTest.initCalls", INIT_CALLS);
158     initSize = app.getIntParam("RemoteTest.initSize", INIT_SIZE);
159     incSize = app.getIntParam("RemoteTest.incSize", INC_SIZE);
160     maxSize = app.getIntParam("RemoteTest.maxSize", MAX_SIZE);
161     if (interactionStyle.length() == 0) {
162       /* IGMI is the default */
163       interactionStyle = "igmi";
164     } else {
165       interactionStyle = interactionStyle.toLowerCase();
166       if (!(interactionStyle.equals("igmi")
167          || interactionStyle.equals("mcast-obj")
168          || interactionStyle.equals("mcast-stream"))) {
169         throw new IllegalArgumentException("Unsupported interaction style: "
170           + interactionStyle);
171       }
172     }
173   }
174 
175 
176   ////////////////////////////////////////////////////////////////////////////////////////////
177   // Methods from RemoteTestService
178   ////////////////////////////////////////////////////////////////////////////////////////////
179 
180   /**
181    * @see jgroup.test.gm.RemoteTestService#activate()
182    */
183   public void activate()
184   {
185     irt = (InternalRemoteTest) gm.getService(InternalRemoteTest.class);
186     System.out.println("\n\n-- RemoteTest: " + interactionStyle + " ---------------------------------------");
187     int calls = numCalls + initCalls;
188     long start = 0;
189     long[] timeVals = new long[calls];
190     byte[] buffer = new byte[0];
191     for (int size = initSize; size <= maxSize; size += incSize) {
192       buffer = new byte[size];
193       for (int i = 0; !halted && i < calls; i++) {
194         try {
195           if (interactionStyle.equals("igmi")) {
196 //            start = System.nanoTime();
197             irt.rtest(buffer);
198 //            timeVals[i] = System.nanoTime() - stop;
199           } else if (interactionStyle.equals("mcast-obj")) {
200 //            start = System.nanoTime();
201             SynchAckListener listener = new SynchAckListener(dispatcherService);
202             multicastService.mcast(PROTOCOL_NAME, "Some object", listener.getRemoteAckListener());
203             Object result = listener.getResults();
204 //            timeVals[i] = System.nanoTime() - stop;
205           } else if (interactionStyle.equals("mcast-stream")) {
206 //            start = System.nanoTime();
207             OutputStream msg = multicastService.getMessage(PROTOCOL_NAME);
208             msg.write(buffer);
209             SynchAckListener listener = new SynchAckListener(dispatcherService);
210             multicastService.mcast(msg, listener.getRemoteAckListener());
211             Object result = listener.getResults();
212 //            timeVals[i] = System.nanoTime() - stop;
213           }
214         } catch (Exception e) {
215           e.printStackTrace();
216         }
217       }
218       int begin = 0;
219       if (calls != numCalls) {
220         System.out.println("JIT Compiling and optimization phase (initCalls):");
221         System.out.println("InitCalls phase - Size=" + size + "--------");
222         for (int i = 0; i < initCalls; i++) {
223           System.out.println(timeVals[i]);
224         }
225         begin = initCalls;
226       }
227       System.out.println("\n\n-- RemoteTest: " + interactionStyle + " ---------------------------------------");
228       System.out.println("Normal run phase - Size=" + size + "--------");
229       for (int i = begin; i < calls; i++) {
230         System.out.println(timeVals[i]);
231       }
232       calls = numCalls;
233     }
234     System.out.println("------------------ COMPLETED ------------------");
235   }
236 
237 
238   public void halt()
239   {
240     halted = true;
241   }
242 
243 
244   ////////////////////////////////////////////////////////////////////////////////////////////
245   // Methods from InternalRemoteTest
246   ////////////////////////////////////////////////////////////////////////////////////////////
247 
248   /**
249    * @see jgroup.test.gm.InternalRemoteTest#rtest()
250    */
251   public void rtest(int[] array)
252     throws RemoteException
253   {
254     return;
255   }
256 
257   /**
258    * @see jgroup.test.gm.InternalRemoteTest#rtest(int)
259    */
260   public Object rtest(byte[] var)
261     throws RemoteException
262   {
263     return var;
264   }
265 
266   /**
267    * @see jgroup.test.gm.InternalRemoteTest#rtest(int)
268    */
269   public Object rtest(int var)
270     throws RemoteException
271   {
272     return new Integer(var);
273   }
274 
275   /**
276    * @see jgroup.test.gm.InternalRemoteTest#rtest(Object)
277    */
278   public Object rtest(Object obj)
279     throws RemoteException
280   {
281     return obj;
282   }
283 
284 
285   ////////////////////////////////////////////////////////////////////////////////////////////
286   // Methods from MulticastListener
287   ////////////////////////////////////////////////////////////////////////////////////////////
288 
289   /**
290    * @see jgroup.core.multicast.MulticastListener#deliverObject(Object)
291    */
292   public Object deliverObject(Object msg, MemberId sender, int seqNo)
293   {
294     log.debug("Received: " + msg);
295     return msg;
296   }
297 
298   /**
299    * @see jgroup.core.multicast.MulticastListener#deliverStream(InputStream)
300    */
301   public Object deliverStream(InputStream msg, MemberId sender, int seqNo)
302   {
303 //    try {
304 //      log.debug("Received: " + msg.read());
305 //    } catch (java.io.IOException e) {
306 //    }
307     return null;
308   }
309 
310   /**
311    * @see jgroup.core.multicast.MulticastListener#getProtocolName()
312    */
313   public String getProtocolName()
314   {
315     return PROTOCOL_NAME;
316   }
317 
318 } // End of RemoteTestLayer