KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > performance > standard > StandardTest


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.performance.standard;
8
9 import org.apache.log4j.Level;
10 import org.jboss.dtf.MultipleTestRunner;
11 import org.jboss.remoting.oneway.OnewayInvokerClientTest;
12 import org.jboss.remoting.performance.PerformanceReporter;
13 import org.jboss.remoting.performance.PerformanceTest;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * Makes oneway call a number of times using multiple threads
20  *
21  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
22  */

23 public class StandardTest extends OnewayInvokerClientTest
24 {
25
26    private final Object JavaDoc waitObj = new Object JavaDoc();
27    private int callCount = 0;
28
29    public StandardTest(String JavaDoc name)
30    {
31       super(name);
32    }
33
34    public StandardTest(int numberOfInstances)
35    {
36       super(numberOfInstances);
37    }
38
39    public StandardTest(String JavaDoc transport, int port)
40    {
41       super(transport, port);
42    }
43
44    public StandardTest(String JavaDoc transport, int port, int numberOfInstances)
45    {
46       super(transport, port, numberOfInstances);
47    }
48
49    protected int incrementCallCount()
50    {
51       int currentCount = callCount++;
52       if((currentCount % 100) == 0)
53       {
54          System.out.println("call count: " + currentCount);
55       }
56       if(callCount == (PerformanceTest.NUM_OF_THREADS * PerformanceTest.NUM_OF_CALLS))
57       {
58          synchronized(waitObj)
59          {
60             waitObj.notify();
61          }
62       }
63       return currentCount;
64    }
65
66    protected void setCallCount(int count)
67    {
68       this.callCount = count;
69    }
70
71 // protected void makeClientOnewayInvocation(String method, String param) throws Throwable
72
// {
73
// makeOnewayInvocation(method, param);
74
// }
75
//
76
// protected void makeServerOnewayInvocation(String method, String param) throws Throwable
77
// {
78
// makeOnewayInvocation(method, param);
79
// }
80
//
81
// protected void superMakeClientOnewayInvocation(String method, String param) throws Throwable
82
// {
83
// super.makeClientOnewayInvocation(method, param);
84
// }
85
//
86
// protected void superMakeServerOnewayInvocation(String method, String param) throws Throwable
87
// {
88
// super.makeServerOnewayInvocation(method, param);
89
// }
90

91    protected Object JavaDoc superMakeClientInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
92    {
93       return super.makeInvocation(method, param);
94    }
95
96 // protected void makeOnewayInvocation(String method, String param) throws Throwable
97
protected Object JavaDoc makeClientInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
98    {
99       callCount = 0;
100
101       sendTotalCount();
102
103       printStartMessage();
104
105       long startTime = System.currentTimeMillis();
106
107       System.out.println("Start time: " + startTime);
108
109       for(int x = 0; x < PerformanceTest.NUM_OF_THREADS; x++)
110       {
111          if((PerformanceTest.NUM_OF_THREADS % 10) == 0)
112          {
113             System.out.println("started " + x + " threads");
114          }
115          new Thread JavaDoc(getRunner(method)).start();
116       }
117
118       synchronized(waitObj)
119       {
120          try
121          {
122             waitObj.wait(5 * 60 * 1000); // timeout if not notified
123
}
124          catch(InterruptedException JavaDoc e)
125          {
126
127          }
128       }
129
130       long endTime = System.currentTimeMillis();
131
132       System.out.println("End time: " + endTime);
133       System.out.println("Total number of calls: " + callCount);
134       long totalTime = endTime - startTime;
135       System.out.println("Total time: " + totalTime);
136
137       int svrTotalCount = getServerTotalCount();
138
139       System.out.println("Server total count: " + svrTotalCount);
140       assertEquals(callCount, svrTotalCount);
141
142       Map JavaDoc metadata = new HashMap JavaDoc();
143       metadata.put("transport", getTransport());
144       metadata.put("server total count", String.valueOf(svrTotalCount));
145       metadata.put("number of client threads", String.valueOf(PerformanceTest.NUM_OF_THREADS));
146       metadata.put("number of client calls per thread", String.valueOf(PerformanceTest.NUM_OF_CALLS));
147
148
149       PerformanceReporter.writeReport(this.getClass().getName() + "::" + method,
150                                       totalTime, callCount, metadata);
151
152       return null;
153    }
154
155    protected void checkAssertion(String JavaDoc param, Object JavaDoc obj)
156    {
157       // NO OP - over ride from OnewayInvokerClientTest so don't get failures.
158
}
159
160
161    private int getServerTotalCount()
162    {
163       int svrCount = 0;
164
165       try
166       {
167          Object JavaDoc ret = makeInvocation("serverTotalCallCount", null);
168          if(ret != null && ret instanceof String JavaDoc)
169          {
170             svrCount = Integer.parseInt((String JavaDoc) ret);
171          }
172       }
173       catch(Throwable JavaDoc throwable)
174       {
175          throwable.printStackTrace();
176       }
177       return svrCount;
178    }
179
180    protected void printStartMessage()
181    {
182       System.out.println("\n*****************************************************\n" +
183                          "Starting standard preformance test with client.\n" +
184                          "*****************************************************\n");
185    }
186
187    /**
188     * Used to tell the test server handler the number of calls to expect.
189     */

190    private void sendTotalCount() throws Throwable JavaDoc
191    {
192       makeInvocation("totalCallCount", String.valueOf(PerformanceTest.NUM_OF_CALLS * PerformanceTest.NUM_OF_THREADS));
193    }
194
195    protected Runnable JavaDoc getRunner(String JavaDoc method)
196    {
197       return new ClientRunner(method);
198    }
199
200    public void testOnewayServerInvocation() throws Throwable JavaDoc
201    {
202       //NO OP since don't want to run this one in this case
203
}
204
205    public void testOnewayClientInvocation() throws Throwable JavaDoc
206    {
207       //NO OP since don't want to run this one in this case
208
}
209
210
211    public class ClientRunner implements Runnable JavaDoc
212    {
213       private String JavaDoc method;
214
215       public ClientRunner(String JavaDoc method)
216       {
217          this.method = method;
218       }
219
220       public void run()
221       {
222          for(int i = 0; i < PerformanceTest.NUM_OF_CALLS; i++)
223          {
224             try
225             {
226                superMakeClientInvocation(this.method, String.valueOf(incrementCallCount()));
227             }
228             catch(Throwable JavaDoc throwable)
229             {
230                throwable.printStackTrace();
231             }
232          }
233       }
234
235    }
236
237    public static void main(String JavaDoc[] args)
238    {
239
240       org.apache.log4j.BasicConfigurator.configure();
241       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
242       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
243       //org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
244
//org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
245

246       StandardTest client = null;
247       if(args.length == 1)
248       {
249          int instances = Integer.parseInt(args[0]);
250          client = new StandardTest(instances);
251       }
252       else if(args.length == 2)
253       {
254          String JavaDoc transport = args[0];
255          int port = Integer.parseInt(args[1]);
256          client = new StandardTest(transport, port);
257       }
258       else if(args.length == 3)
259       {
260          String JavaDoc transport = args[0];
261          int port = Integer.parseInt(args[1]);
262          int instances = Integer.parseInt(args[2]);
263          client = new StandardTest(transport, port, instances);
264       }
265       else
266       {
267          client = new StandardTest(OnewayInvokerClientTest.class.getName());
268          System.out.println("Using default transport (" + client.getTransport() +
269                             ") and default port (" + client.getPort() + ") and " +
270                             "default number of instances (" + client.getNumberOfInstances() + ")" +
271                             "\nCan enter transport, port, and instances via command line.");
272       }
273
274       try
275       {
276          //regular class run
277
//client.runInvokers();
278
MultipleTestRunner runner = new MultipleTestRunner();
279          runner.doRun(client, true);
280       }
281       catch(Throwable JavaDoc e)
282       {
283          e.printStackTrace();
284          System.exit(1);
285       }
286       System.exit(0);
287    }
288
289
290 }
Popular Tags