KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > performance > oneway > OnewayTest


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.oneway;
8
9 import org.jboss.remoting.oneway.OnewayInvokerClientTest;
10 import org.jboss.remoting.performance.PerformanceReporter;
11 import org.jboss.remoting.performance.PerformanceTest;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * Makes oneway call a number of times using multiple threads
18  *
19  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
20  */

21 public abstract class OnewayTest extends OnewayInvokerClientTest
22 {
23    private final Object JavaDoc waitObj = new Object JavaDoc();
24    private int callCount = 0;
25
26    public OnewayTest(String JavaDoc name)
27    {
28       super(name);
29    }
30
31    public OnewayTest(int numberOfInstances)
32    {
33       super(numberOfInstances);
34    }
35
36    public OnewayTest(String JavaDoc transport, int port)
37    {
38       super(transport, port);
39    }
40
41    public OnewayTest(String JavaDoc transport, int port, int numberOfInstances)
42    {
43       super(transport, port, numberOfInstances);
44    }
45
46    protected int incrementCallCount()
47    {
48       int currentCount = callCount++;
49       if((currentCount % 100) == 0)
50       {
51          System.out.println("call count: " + currentCount);
52       }
53       if(callCount == (PerformanceTest.NUM_OF_THREADS * PerformanceTest.NUM_OF_CALLS))
54       {
55          synchronized(waitObj)
56          {
57             waitObj.notify();
58          }
59       }
60       return currentCount;
61    }
62
63    protected void setCallCount(int count)
64    {
65       this.callCount = count;
66    }
67
68    protected void makeClientOnewayInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
69    {
70       makeOnewayInvocation(method, param);
71    }
72
73    protected void makeServerOnewayInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
74    {
75       makeOnewayInvocation(method, param);
76    }
77
78    protected void superMakeClientOnewayInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
79    {
80       super.makeClientOnewayInvocation(method, param);
81    }
82
83    protected void superMakeServerOnewayInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
84    {
85       super.makeServerOnewayInvocation(method, param);
86    }
87
88    protected void makeOnewayInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
89    {
90       callCount = 0;
91
92       sendTotalCount();
93
94       printStartMessage();
95
96       long startTime = System.currentTimeMillis();
97
98       System.out.println("Start time: " + startTime);
99
100       for(int x = 0; x < PerformanceTest.NUM_OF_THREADS; x++)
101       {
102          if((PerformanceTest.NUM_OF_THREADS % 10) == 0)
103          {
104             System.out.println("started " + x + " threads");
105          }
106          new Thread JavaDoc(getRunner(method), "Test thread " + x).start();
107       }
108
109       synchronized(waitObj)
110       {
111          try
112          {
113             waitObj.wait(5 * 60 * 1000); // timeout if not notified
114
}
115          catch(InterruptedException JavaDoc e)
116          {
117
118          }
119       }
120
121       long endTime = System.currentTimeMillis();
122
123       System.out.println("End time: " + endTime);
124       System.out.println("Total number of calls: " + callCount);
125       long totalTime = endTime - startTime;
126       System.out.println("Total time: " + totalTime);
127
128       Thread.currentThread().sleep(5000);
129
130       int svrTotalCount = getServerTotalCount();
131
132       System.out.println("Server total count: " + svrTotalCount);
133       assertEquals(callCount, svrTotalCount);
134
135       Map JavaDoc metadata = new HashMap JavaDoc();
136       metadata.put("transport", getTransport());
137       metadata.put("server total count", String.valueOf(svrTotalCount));
138       metadata.put("number of client threads", String.valueOf(PerformanceTest.NUM_OF_THREADS));
139       metadata.put("number of client calls per thread", String.valueOf(PerformanceTest.NUM_OF_CALLS));
140
141       PerformanceReporter.writeReport(this.getClass().getName() + "::" + method,
142                                       totalTime, callCount, metadata);
143    }
144
145    protected void checkAssertion(String JavaDoc param, Object JavaDoc obj)
146    {
147       // NO OP - over ride from OnewayInvokerClientTest so don't get failures.
148
}
149
150
151    private int getServerTotalCount()
152    {
153       int svrCount = 0;
154
155       try
156       {
157          Object JavaDoc ret = makeInvocation("serverTotalCallCount", null);
158          if(ret != null && ret instanceof String JavaDoc)
159          {
160             svrCount = Integer.parseInt((String JavaDoc) ret);
161          }
162       }
163       catch(Throwable JavaDoc throwable)
164       {
165          throwable.printStackTrace();
166       }
167       return svrCount;
168    }
169
170    protected abstract Runnable JavaDoc getRunner(String JavaDoc method);
171
172    protected abstract void printStartMessage();
173
174    /**
175     * Used to tell the test server handler the number of calls to expect.
176     */

177    private void sendTotalCount() throws Throwable JavaDoc
178    {
179       makeInvocation("totalCallCount", String.valueOf(PerformanceTest.NUM_OF_CALLS * PerformanceTest.NUM_OF_THREADS));
180    }
181
182 }
Popular Tags