KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > perf > ejb > PerfTestSessionBean


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.perf.ejb;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.text.NumberFormat JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import org.jboss.test.perf.interfaces.PerfResult;
31 import org.jboss.test.perf.interfaces.Probe;
32 import org.jboss.test.perf.interfaces.ProbeHome;
33 import org.jboss.test.perf.interfaces.ProbeLocal;
34 import org.jboss.test.perf.interfaces.ProbeLocalHome;
35 import org.jboss.test.util.Debug;
36 import org.jboss.test.util.ejb.SessionSupport;
37
38 /** A session bean that tests intra-VM EJB call invocation overhead. The
39 runProbeTests method accepts the number of iterations and returns a simple
40 html report showing the output of each test run against the Probe session.
41  
42 @author Scott.Stark@jboss.org
43 @version $Revision: 37406 $
44 */

45 public class PerfTestSessionBean extends SessionSupport
46 {
47    private static NumberFormat JavaDoc fmt = NumberFormat.getInstance();
48    static
49    {
50       fmt.setMinimumFractionDigits(3);
51       fmt.setMaximumFractionDigits(3);
52    }
53
54    /** Run the unit tests using Probe and return a report as a string
55     */

56    public PerfResult runProbeTests(int iterationCount)
57    {
58       StringBuffer JavaDoc results = new StringBuffer JavaDoc("<h1>runProbeTests("+iterationCount+")</h1><pre>\n");
59       int testCount = 0;
60       int failureCount = 0;
61
62       PerfResult result = new PerfResult();
63       try
64       {
65          testCount ++;
66          testProbeTimings(iterationCount, results);
67       }
68       catch(Exception JavaDoc e)
69       {
70          failureCount ++;
71          formatException(e, "testTimings", results);
72          result.error = e;
73       }
74       results.append('\n');
75
76       results.append("\nTotal tests: "+testCount);
77       results.append("\nTotal failures: "+failureCount);
78       results.append("\n</pre>");
79       result.report = results.toString();
80       return result;
81    }
82    /** Run the unit tests using ProbeLocal and return a report as a string
83     */

84    public PerfResult runProbeLocalTests(int iterationCount)
85    {
86       StringBuffer JavaDoc results = new StringBuffer JavaDoc("<h1>runProbeLocalTests("+iterationCount+")</h1><pre>\n");
87       int testCount = 0;
88       int failureCount = 0;
89
90       PerfResult result = new PerfResult();
91       try
92       {
93          testCount ++;
94          testProbeLocalTimings(iterationCount, results);
95       }
96       catch(Exception JavaDoc e)
97       {
98          failureCount ++;
99          formatException(e, "testTimings", results);
100          result.error = e;
101       }
102       results.append('\n');
103
104       results.append("\nTotal tests: "+testCount);
105       results.append("\nTotal failures: "+failureCount);
106       results.append("\n</pre>");
107       result.report = results.toString();
108       return result;
109    }
110
111    private void testProbeTimings(int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
112    {
113       results.append("\n+++ testTimings()\n");
114       Object JavaDoc obj = new InitialContext JavaDoc().lookup("java:comp/env/ejb/ProbeHome");
115       Class JavaDoc homeClass = obj.getClass();
116       ProbeHome home = null;
117       results.append("ProbeHome Proxy class info:\n");
118       Debug.displayClassInfo(homeClass, results);
119       results.append("Local ProbeHome.class info:\n");
120       Debug.displayClassInfo(ProbeHome.class, results);
121       home = (ProbeHome) obj;
122
123       results.append("\nFound ProbeHome");
124       Probe bean = home.create();
125       results.append("\nCreated Probe");
126       warmup(bean, results);
127       noop(bean, iterationCount, results);
128       ping(bean, iterationCount, results);
129       echo(bean, iterationCount, results);
130    }
131    private void testProbeLocalTimings(int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
132    {
133       results.append("\n+++ testTimings()\n");
134       Object JavaDoc obj = new InitialContext JavaDoc().lookup("java:comp/env/ejb/ProbeLocalHome");
135       Class JavaDoc homeClass = obj.getClass();
136       ProbeLocalHome home = null;
137       results.append("ProbeLocalHome Proxy class info:\n");
138       Debug.displayClassInfo(homeClass, results);
139       results.append("Local ProbeLocalHome.class info:\n");
140       Debug.displayClassInfo(ProbeLocalHome.class, results);
141
142       home = (ProbeLocalHome) obj;
143       results.append("\nFound ProbeLocalHome");
144       ProbeLocal bean = home.create();
145       results.append("\nCreated ProbeLocal");
146       warmup(bean, results);
147       noop(bean, iterationCount, results);
148       ping(bean, iterationCount, results);
149       echo(bean, iterationCount, results);
150    }
151
152    private void warmup(Probe bean, StringBuffer JavaDoc results) throws Exception JavaDoc
153    {
154       bean.noop();
155       bean.ping("Ping");
156       bean.echo("Echo");
157    }
158    private void warmup(ProbeLocal bean, StringBuffer JavaDoc results) throws Exception JavaDoc
159    {
160       bean.noop();
161       bean.ping("Ping");
162       bean.echo("Echo");
163    }
164    private void noop(Probe bean, int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
165    {
166       results.append("\nStarting "+iterationCount+" noop() invocations");
167       long start = System.currentTimeMillis();
168       for(int n = 0; n < iterationCount; n ++)
169          bean.noop();
170       long end = System.currentTimeMillis();
171       long elapsed = end - start;
172       float avgTime = elapsed;
173       avgTime /= iterationCount;
174       results.append("\n"+iterationCount+" noop() invocations = "+elapsed+" ms, "
175          + fmt.format(avgTime)+" ms/noop");
176    }
177    private void noop(ProbeLocal bean, int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
178    {
179       results.append("\nStarting "+iterationCount+" noop() invocations");
180       long start = System.currentTimeMillis();
181       for(int n = 0; n < iterationCount; n ++)
182          bean.noop();
183       long end = System.currentTimeMillis();
184       long elapsed = end - start;
185       float avgTime = elapsed;
186       avgTime /= iterationCount;
187       results.append("\n"+iterationCount+" noop() invocations = "+elapsed+" ms, "
188          + fmt.format(avgTime)+" ms/noop");
189    }
190
191    private void ping(Probe bean, int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
192    {
193       results.append("\nStarting "+iterationCount+" ping(PING) invocations");
194       long start = System.currentTimeMillis();
195       for(int n = 0; n < iterationCount; n ++)
196          bean.ping("PING");
197       long end = System.currentTimeMillis();
198       long elapsed = end - start;
199       float avgTime = elapsed;
200       avgTime /= iterationCount;
201       results.append("\n"+iterationCount+" ping() invocations = "+elapsed+" ms, "
202          + fmt.format(avgTime)+" ms/ping");
203    }
204    private void ping(ProbeLocal bean, int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
205    {
206       results.append("\nStarting "+iterationCount+" ping(PING) invocations");
207       long start = System.currentTimeMillis();
208       for(int n = 0; n < iterationCount; n ++)
209          bean.ping("PING");
210       long end = System.currentTimeMillis();
211       long elapsed = end - start;
212       float avgTime = elapsed;
213       avgTime /= iterationCount;
214       results.append("\n"+iterationCount+" ping() invocations = "+elapsed+" ms, "
215          + fmt.format(avgTime)+" ms/ping");
216    }
217
218    private void echo(Probe bean, int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
219    {
220       results.append("\nStarting "+iterationCount+" echo(ECHO) invocations");
221       long start = System.currentTimeMillis();
222       for(int n = 0; n < iterationCount; n ++)
223       {
224          String JavaDoc echo = bean.echo("ECHO");
225       }
226       long end = System.currentTimeMillis();
227       long elapsed = end - start;
228       float avgTime = elapsed;
229       avgTime /= iterationCount;
230       results.append("\n"+iterationCount+" echo() invocations = "+elapsed+" ms, "
231          + fmt.format(avgTime)+" ms/echo");
232    }
233    private void echo(ProbeLocal bean, int iterationCount, StringBuffer JavaDoc results) throws Exception JavaDoc
234    {
235       results.append("\nStarting "+iterationCount+" echo(ECHO) invocations");
236       long start = System.currentTimeMillis();
237       for(int n = 0; n < iterationCount; n ++)
238       {
239          String JavaDoc echo = bean.echo("ECHO");
240       }
241       long end = System.currentTimeMillis();
242       long elapsed = end - start;
243       float avgTime = elapsed;
244       avgTime /= iterationCount;
245       results.append("\n"+iterationCount+" echo() invocations = "+elapsed+" ms, "
246          + fmt.format(avgTime)+" ms/echo");
247    }
248
249    private void formatException(Throwable JavaDoc t, String JavaDoc testName, StringBuffer JavaDoc results)
250    {
251       StringWriter JavaDoc sw = new StringWriter JavaDoc();
252       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
253       t.printStackTrace(pw);
254       results.append("\n"+testName+" failed:\n");
255       results.append(sw.toString());
256    }
257
258 }
259
Popular Tags