KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > perf > test > PerfTest


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.test;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.security.CodeSource JavaDoc;
30 import java.security.ProtectionDomain JavaDoc;
31 import java.text.NumberFormat JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33
34 import org.jboss.test.perf.interfaces.Probe;
35 import org.jboss.test.perf.interfaces.ProbeHome;
36 import org.jboss.test.perf.interfaces.TxSession;
37 import org.jboss.test.perf.interfaces.TxSessionHome;
38
39 /** An MBean that tests intra-VM EJB call invocation overhead. The runTests
40  *operation accepts the number of iterations and returns a simple html report
41  *showing the output of each test run.
42  
43  @author Scott.Stark@jboss.org
44  @version $Revision: 37406 $
45  */

46 public class PerfTest implements PerfTestMBean
47 {
48    private static NumberFormat JavaDoc fmt = NumberFormat.getInstance();
49    static
50    {
51       fmt.setMinimumFractionDigits(3);
52       fmt.setMaximumFractionDigits(3);
53    }
54
55    int iterationCount;
56
57    public String JavaDoc runTests(int iterationCount)
58    {
59       StringBuffer JavaDoc results = new StringBuffer JavaDoc("<h1>PerfTest.results</h1><pre>\n");
60       this.iterationCount = iterationCount;
61       int testCount = 0;
62       int failureCount = 0;
63
64       // Print out some codebase info
65
URL JavaDoc thisURL = getClass().getProtectionDomain().getCodeSource().getLocation();
66       results.append("\nPertTest.ClassLoader="+getClass().getClassLoader());
67       results.append("\nPertTest.codebase="+thisURL);
68       try
69       {
70          testCount ++;
71          testTimings(results);
72       }
73       catch(Throwable JavaDoc e)
74       {
75          failureCount ++;
76          formatException(e, "testTimings", results);
77       }
78       results.append('\n');
79
80       try
81       {
82          testCount ++;
83          testTimingsCMT(results);
84       }
85       catch(Throwable JavaDoc e)
86       {
87          failureCount ++;
88          formatException(e, "testTimingsCMT", results);
89       }
90       results.append('\n');
91
92       try
93       {
94          testCount ++;
95          testTxTimings(results);
96       }
97       catch(Throwable JavaDoc e)
98       {
99          failureCount ++;
100          formatException(e, "testTxTimings", results);
101       }
102       results.append("\nTotal tests: "+testCount);
103       results.append("\nTotal failures: "+failureCount);
104       results.append("\n<pre>");
105
106       return results.toString();
107    }
108
109    public void testTimings(StringBuffer JavaDoc results) throws Exception JavaDoc
110    {
111       results.append("\n+++ testTimings()");
112       ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
113       Thread.currentThread().setContextClassLoader(ProbeHome.class.getClassLoader());
114       try
115       {
116          Object JavaDoc obj = new InitialContext JavaDoc().lookup("perf.Probe");
117          // Print out some codebase info for the ProbeHome
118
Class JavaDoc homeClass = obj.getClass();
119          ClassLoader JavaDoc cl = homeClass.getClassLoader();
120          results.append("\nProbeHome.ClassLoader="+cl);
121          ClassLoader JavaDoc parent = cl;
122          while( parent != null )
123          {
124             results.append("\n.."+parent);
125             if( parent instanceof URLClassLoader JavaDoc )
126             {
127                URLClassLoader JavaDoc ucl = (URLClassLoader JavaDoc) parent;
128                URL JavaDoc[] urls = ucl.getURLs();
129                int length = urls != null ? urls.length : 0;
130                for(int u = 0; u < length; u ++)
131                {
132                   results.append("\n...."+urls[u]);
133                }
134             }
135             if( parent != null )
136                parent = parent.getParent();
137          }
138          results.append("\nProbeHome Interfaces:");
139          Class JavaDoc[] ifaces = homeClass.getInterfaces();
140          for(int i = 0; i < ifaces.length; i ++)
141          {
142             results.append("\n++"+ifaces[i]);
143             ProtectionDomain JavaDoc pd = ifaces[i].getProtectionDomain();
144             CodeSource JavaDoc cs = pd.getCodeSource();
145             if( cs != null )
146                results.append("\n++++CodeSource: "+cs);
147             else
148                results.append("\n++++Null CodeSource");
149          }
150          CodeSource JavaDoc homeCS = ProbeHome.class.getProtectionDomain().getCodeSource();
151          if( homeCS != null )
152             results.append("\nPerfTest ProbHome CodeSource: "+homeCS);
153          else
154             results.append("\nPerfTest ProbHome CodeSource is NULL");
155          
156          ProbeHome home = (ProbeHome) obj;
157          results.append("\n\nFound ProbeHome @ jndiName=Probe");
158          Probe bean = home.create();
159          results.append("\nCreated Probe");
160          warmup(bean, results);
161          noop(bean, results);
162          ping(bean, results);
163          echo(bean, results);
164       }
165       finally
166       {
167          Thread.currentThread().setContextClassLoader(oldCl);
168       } // end of finally
169
}
170
171    public void testTimingsCMT(StringBuffer JavaDoc results) throws Exception JavaDoc
172    {
173       results.append("\n+++ testTimingsCMT()");
174       ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
175       Thread.currentThread().setContextClassLoader(ProbeHome.class.getClassLoader());
176       try
177       {
178          Object JavaDoc obj = new InitialContext JavaDoc().lookup("perf.ProbeCMT");
179          ProbeHome home = (ProbeHome) obj;
180          results.append("\nFound ProbeHome @ jndiName=ProbeCMT");
181          Probe bean = home.create();
182          results.append("\nCreated ProbeCMT");
183          warmup(bean, results);
184          noop(bean, results);
185          ping(bean, results);
186          echo(bean, results);
187       }
188       finally
189       {
190          Thread.currentThread().setContextClassLoader(oldCl);
191       } // end of finally
192
}
193
194    public void testTxTimings(StringBuffer JavaDoc results) throws Exception JavaDoc
195    {
196       results.append("\n+++ testTxTimings()");
197       ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
198       Thread.currentThread().setContextClassLoader(TxSessionHome.class.getClassLoader());
199       try
200       {
201          Object JavaDoc obj = new InitialContext JavaDoc().lookup("perf.TxSession");
202          TxSessionHome home = (TxSessionHome) obj;
203          results.append("\nFound TxSession @ jndiName=TxSession");
204          TxSession bean = home.create();
205          results.append("\nCreated TxSession");
206          txRequired(bean, results);
207          txRequiresNew(bean, results);
208          txSupports(bean, results);
209          txNotSupported(bean, results);
210          requiredToSupports(bean, results);
211          requiredToMandatory(bean, results);
212          requiredToRequiresNew(bean, results);
213       }
214       finally
215       {
216          Thread.currentThread().setContextClassLoader(oldCl);
217       } // end of finally
218
}
219
220    private void warmup(Probe bean, StringBuffer JavaDoc results) throws Exception JavaDoc
221    {
222       bean.noop();
223       bean.ping("Ping");
224       bean.echo("Echo");
225    }
226    private void noop(Probe bean, StringBuffer JavaDoc results) throws Exception JavaDoc
227    {
228       results.append("\nStarting "+iterationCount+" noop() invocations");
229       long start = System.currentTimeMillis();
230       for(int n = 0; n < iterationCount; n ++)
231          bean.noop();
232       long end = System.currentTimeMillis();
233       long elapsed = end - start;
234       float avgTime = elapsed;
235       avgTime /= iterationCount;
236       results.append("\n"+iterationCount+" noop() invocations = "+elapsed+" ms, "
237          + fmt.format(avgTime)+" ms/noop");
238    }
239    private void ping(Probe bean, StringBuffer JavaDoc results) throws Exception JavaDoc
240    {
241       results.append("\nStarting "+iterationCount+" ping(PING) invocations");
242       long start = System.currentTimeMillis();
243       for(int n = 0; n < iterationCount; n ++)
244          bean.ping("PING");
245       long end = System.currentTimeMillis();
246       long elapsed = end - start;
247       float avgTime = elapsed;
248       avgTime /= iterationCount;
249       results.append("\n"+iterationCount+" ping() invocations = "+elapsed+" ms, "
250          + fmt.format(avgTime)+" ms/ping");
251    }
252    private void echo(Probe bean, StringBuffer JavaDoc results) throws Exception JavaDoc
253    {
254       results.append("\nStarting "+iterationCount+" echo(ECHO) invocations");
255       long start = System.currentTimeMillis();
256       for(int n = 0; n < iterationCount; n ++)
257       {
258          String JavaDoc echo = bean.echo("ECHO");
259       }
260       long end = System.currentTimeMillis();
261       long elapsed = end - start;
262       float avgTime = elapsed;
263       avgTime /= iterationCount;
264       results.append("\n"+iterationCount+" echo() invocations = "+elapsed+" ms, "
265          + fmt.format(avgTime)+" ms/echo");
266    }
267    private void txRequired(TxSession bean, StringBuffer JavaDoc results) throws Exception JavaDoc
268    {
269       results.append("\nStarting "+iterationCount+" txRequired() invocations");
270       long start = System.currentTimeMillis();
271       for(int n = 0; n < iterationCount; n ++)
272       {
273          String JavaDoc echo = bean.txRequired();
274       }
275       long end = System.currentTimeMillis();
276       long elapsed = end - start;
277       float avgTime = elapsed;
278       avgTime /= iterationCount;
279       results.append("\n"+iterationCount+" txRequired() invocations = "+elapsed+" ms, "
280          + fmt.format(avgTime)+" ms/txRequired");
281    }
282    private void txRequiresNew(TxSession bean, StringBuffer JavaDoc results) throws Exception JavaDoc
283    {
284       results.append("\nStarting "+iterationCount+" txRequired() invocations");
285       long start = System.currentTimeMillis();
286       for(int n = 0; n < iterationCount; n ++)
287       {
288          String JavaDoc echo = bean.txRequiresNew();
289       }
290       long end = System.currentTimeMillis();
291       long elapsed = end - start;
292       float avgTime = elapsed;
293       avgTime /= iterationCount;
294       results.append("\n"+iterationCount+" txRequiresNew() invocations = "+elapsed+" ms, "
295          + fmt.format(avgTime)+" ms/txRequiresNew");
296    }
297    private void txSupports(TxSession bean, StringBuffer JavaDoc results) throws Exception JavaDoc
298    {
299       results.append("\nStarting "+iterationCount+" txSupports() invocations");
300       long start = System.currentTimeMillis();
301       for(int n = 0; n < iterationCount; n ++)
302       {
303          String JavaDoc echo = bean.txSupports();
304       }
305       long end = System.currentTimeMillis();
306       long elapsed = end - start;
307       float avgTime = elapsed;
308       avgTime /= iterationCount;
309       results.append("\n"+iterationCount+" txSupports() invocations = "+elapsed+" ms, "
310          + fmt.format(avgTime)+" ms/txSupports");
311    }
312    private void txNotSupported(TxSession bean, StringBuffer JavaDoc results) throws Exception JavaDoc
313    {
314       results.append("\nStarting "+iterationCount+" txNotSupported() invocations");
315       long start = System.currentTimeMillis();
316       for(int n = 0; n < iterationCount; n ++)
317       {
318          String JavaDoc echo = bean.txNotSupported();
319       }
320       long end = System.currentTimeMillis();
321       long elapsed = end - start;
322       float avgTime = elapsed;
323       avgTime /= iterationCount;
324       results.append("\n"+iterationCount+" txNotSupported() invocations = "+elapsed+" ms, "
325          + fmt.format(avgTime)+" ms/txNotSupported");
326    }
327    private void requiredToSupports(TxSession bean, StringBuffer JavaDoc results) throws Exception JavaDoc
328    {
329       results.append("\nStarting "+iterationCount+" requiredToSupports() invocations");
330       long start = System.currentTimeMillis();
331       for(int n = 0; n < iterationCount; n ++)
332       {
333          String JavaDoc echo = bean.requiredToSupports();
334       }
335       long end = System.currentTimeMillis();
336       long elapsed = end - start;
337       float avgTime = elapsed;
338       avgTime /= iterationCount;
339       results.append("\n"+iterationCount+" requiredToSupports() invocations = "+elapsed+" ms, "
340          + fmt.format(avgTime)+" ms/requiredToSupports");
341    }
342    private void requiredToMandatory(TxSession bean, StringBuffer JavaDoc results) throws Exception JavaDoc
343    {
344       results.append("\nStarting "+iterationCount+" requiredToMandatory() invocations");
345       long start = System.currentTimeMillis();
346       for(int n = 0; n < iterationCount; n ++)
347       {
348          String JavaDoc echo = bean.requiredToMandatory();
349       }
350       long end = System.currentTimeMillis();
351       long elapsed = end - start;
352       float avgTime = elapsed;
353       avgTime /= iterationCount;
354       results.append("\n"+iterationCount+" requiredToMandatory() invocations = "+elapsed+" ms, "
355          + fmt.format(avgTime)+" ms/requiredToMandatory");
356    }
357    private void requiredToRequiresNew(TxSession bean, StringBuffer JavaDoc results) throws Exception JavaDoc
358    {
359       results.append("\nStarting "+iterationCount+" requiredToRequiresNew() invocations");
360       long start = System.currentTimeMillis();
361       for(int n = 0; n < iterationCount; n ++)
362       {
363          String JavaDoc echo = bean.requiredToRequiresNew();
364       }
365       long end = System.currentTimeMillis();
366       long elapsed = end - start;
367       float avgTime = elapsed;
368       avgTime /= iterationCount;
369       results.append("\n"+iterationCount+" requiredToRequiresNew() invocations = "+elapsed+" ms, "
370          + fmt.format(avgTime)+" ms/requiredToRequiresNew");
371    }
372
373    private void formatException(Throwable JavaDoc t, String JavaDoc testName, StringBuffer JavaDoc results)
374    {
375       StringWriter JavaDoc sw = new StringWriter JavaDoc();
376       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
377       t.printStackTrace(pw);
378       results.append("\n"+testName+" failed:\n");
379       results.append(sw.toString());
380    }
381
382 }
383
Popular Tags