001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.perf.ejb;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027: import java.text.NumberFormat;
028: import javax.naming.InitialContext;
029:
030: import org.jboss.test.perf.interfaces.PerfResult;
031: import org.jboss.test.perf.interfaces.Probe;
032: import org.jboss.test.perf.interfaces.ProbeHome;
033: import org.jboss.test.perf.interfaces.ProbeLocal;
034: import org.jboss.test.perf.interfaces.ProbeLocalHome;
035: import org.jboss.test.util.Debug;
036: import org.jboss.test.util.ejb.SessionSupport;
037:
038: /** A session bean that tests intra-VM EJB call invocation overhead. The
039: runProbeTests method accepts the number of iterations and returns a simple
040: html report showing the output of each test run against the Probe session.
041:
042: @author Scott.Stark@jboss.org
043: @version $Revision: 57211 $
044: */
045: public class PerfTestSessionBean extends SessionSupport {
046: private static NumberFormat fmt = NumberFormat.getInstance();
047: static {
048: fmt.setMinimumFractionDigits(3);
049: fmt.setMaximumFractionDigits(3);
050: }
051:
052: /** Run the unit tests using Probe and return a report as a string
053: */
054: public PerfResult runProbeTests(int iterationCount) {
055: StringBuffer results = new StringBuffer("<h1>runProbeTests("
056: + iterationCount + ")</h1><pre>\n");
057: int testCount = 0;
058: int failureCount = 0;
059:
060: PerfResult result = new PerfResult();
061: try {
062: testCount++;
063: testProbeTimings(iterationCount, results);
064: } catch (Exception e) {
065: failureCount++;
066: formatException(e, "testTimings", results);
067: result.error = e;
068: }
069: results.append('\n');
070:
071: results.append("\nTotal tests: " + testCount);
072: results.append("\nTotal failures: " + failureCount);
073: results.append("\n</pre>");
074: result.report = results.toString();
075: return result;
076: }
077:
078: /** Run the unit tests using ProbeLocal and return a report as a string
079: */
080: public PerfResult runProbeLocalTests(int iterationCount) {
081: StringBuffer results = new StringBuffer(
082: "<h1>runProbeLocalTests(" + iterationCount
083: + ")</h1><pre>\n");
084: int testCount = 0;
085: int failureCount = 0;
086:
087: PerfResult result = new PerfResult();
088: try {
089: testCount++;
090: testProbeLocalTimings(iterationCount, results);
091: } catch (Exception e) {
092: failureCount++;
093: formatException(e, "testTimings", results);
094: result.error = e;
095: }
096: results.append('\n');
097:
098: results.append("\nTotal tests: " + testCount);
099: results.append("\nTotal failures: " + failureCount);
100: results.append("\n</pre>");
101: result.report = results.toString();
102: return result;
103: }
104:
105: private void testProbeTimings(int iterationCount,
106: StringBuffer results) throws Exception {
107: results.append("\n+++ testTimings()\n");
108: Object obj = new InitialContext()
109: .lookup("java:comp/env/ejb/ProbeHome");
110: Class homeClass = obj.getClass();
111: ProbeHome home = null;
112: results.append("ProbeHome Proxy class info:\n");
113: Debug.displayClassInfo(homeClass, results);
114: results.append("Local ProbeHome.class info:\n");
115: Debug.displayClassInfo(ProbeHome.class, results);
116: home = (ProbeHome) obj;
117:
118: results.append("\nFound ProbeHome");
119: Probe bean = home.create();
120: results.append("\nCreated Probe");
121: warmup(bean, results);
122: noop(bean, iterationCount, results);
123: ping(bean, iterationCount, results);
124: echo(bean, iterationCount, results);
125: }
126:
127: private void testProbeLocalTimings(int iterationCount,
128: StringBuffer results) throws Exception {
129: results.append("\n+++ testTimings()\n");
130: Object obj = new InitialContext()
131: .lookup("java:comp/env/ejb/ProbeLocalHome");
132: Class homeClass = obj.getClass();
133: ProbeLocalHome home = null;
134: results.append("ProbeLocalHome Proxy class info:\n");
135: Debug.displayClassInfo(homeClass, results);
136: results.append("Local ProbeLocalHome.class info:\n");
137: Debug.displayClassInfo(ProbeLocalHome.class, results);
138:
139: home = (ProbeLocalHome) obj;
140: results.append("\nFound ProbeLocalHome");
141: ProbeLocal bean = home.create();
142: results.append("\nCreated ProbeLocal");
143: warmup(bean, results);
144: noop(bean, iterationCount, results);
145: ping(bean, iterationCount, results);
146: echo(bean, iterationCount, results);
147: }
148:
149: private void warmup(Probe bean, StringBuffer results)
150: throws Exception {
151: bean.noop();
152: bean.ping("Ping");
153: bean.echo("Echo");
154: }
155:
156: private void warmup(ProbeLocal bean, StringBuffer results)
157: throws Exception {
158: bean.noop();
159: bean.ping("Ping");
160: bean.echo("Echo");
161: }
162:
163: private void noop(Probe bean, int iterationCount,
164: StringBuffer results) throws Exception {
165: results.append("\nStarting " + iterationCount
166: + " 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 = "
175: + elapsed + " ms, " + fmt.format(avgTime) + " ms/noop");
176: }
177:
178: private void noop(ProbeLocal bean, int iterationCount,
179: StringBuffer results) throws Exception {
180: results.append("\nStarting " + iterationCount
181: + " noop() invocations");
182: long start = System.currentTimeMillis();
183: for (int n = 0; n < iterationCount; n++)
184: bean.noop();
185: long end = System.currentTimeMillis();
186: long elapsed = end - start;
187: float avgTime = elapsed;
188: avgTime /= iterationCount;
189: results.append("\n" + iterationCount + " noop() invocations = "
190: + elapsed + " ms, " + fmt.format(avgTime) + " ms/noop");
191: }
192:
193: private void ping(Probe bean, int iterationCount,
194: StringBuffer results) throws Exception {
195: results.append("\nStarting " + iterationCount
196: + " ping(PING) invocations");
197: long start = System.currentTimeMillis();
198: for (int n = 0; n < iterationCount; n++)
199: bean.ping("PING");
200: long end = System.currentTimeMillis();
201: long elapsed = end - start;
202: float avgTime = elapsed;
203: avgTime /= iterationCount;
204: results.append("\n" + iterationCount + " ping() invocations = "
205: + elapsed + " ms, " + fmt.format(avgTime) + " ms/ping");
206: }
207:
208: private void ping(ProbeLocal bean, int iterationCount,
209: StringBuffer results) throws Exception {
210: results.append("\nStarting " + iterationCount
211: + " ping(PING) invocations");
212: long start = System.currentTimeMillis();
213: for (int n = 0; n < iterationCount; n++)
214: bean.ping("PING");
215: long end = System.currentTimeMillis();
216: long elapsed = end - start;
217: float avgTime = elapsed;
218: avgTime /= iterationCount;
219: results.append("\n" + iterationCount + " ping() invocations = "
220: + elapsed + " ms, " + fmt.format(avgTime) + " ms/ping");
221: }
222:
223: private void echo(Probe bean, int iterationCount,
224: StringBuffer results) throws Exception {
225: results.append("\nStarting " + iterationCount
226: + " echo(ECHO) invocations");
227: long start = System.currentTimeMillis();
228: for (int n = 0; n < iterationCount; n++) {
229: String echo = bean.echo("ECHO");
230: }
231: long end = System.currentTimeMillis();
232: long elapsed = end - start;
233: float avgTime = elapsed;
234: avgTime /= iterationCount;
235: results.append("\n" + iterationCount + " echo() invocations = "
236: + elapsed + " ms, " + fmt.format(avgTime) + " ms/echo");
237: }
238:
239: private void echo(ProbeLocal bean, int iterationCount,
240: StringBuffer results) throws Exception {
241: results.append("\nStarting " + iterationCount
242: + " echo(ECHO) invocations");
243: long start = System.currentTimeMillis();
244: for (int n = 0; n < iterationCount; n++) {
245: String echo = bean.echo("ECHO");
246: }
247: long end = System.currentTimeMillis();
248: long elapsed = end - start;
249: float avgTime = elapsed;
250: avgTime /= iterationCount;
251: results.append("\n" + iterationCount + " echo() invocations = "
252: + elapsed + " ms, " + fmt.format(avgTime) + " ms/echo");
253: }
254:
255: private void formatException(Throwable t, String testName,
256: StringBuffer results) {
257: StringWriter sw = new StringWriter();
258: PrintWriter pw = new PrintWriter(sw);
259: t.printStackTrace(pw);
260: results.append("\n" + testName + " failed:\n");
261: results.append(sw.toString());
262: }
263:
264: }
|