1 22 package org.jboss.test.perf.ejb; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.StringWriter ; 27 import java.text.NumberFormat ; 28 import javax.naming.InitialContext ; 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 45 public class PerfTestSessionBean extends SessionSupport 46 { 47 private static NumberFormat fmt = NumberFormat.getInstance(); 48 static 49 { 50 fmt.setMinimumFractionDigits(3); 51 fmt.setMaximumFractionDigits(3); 52 } 53 54 56 public PerfResult runProbeTests(int iterationCount) 57 { 58 StringBuffer results = new StringBuffer ("<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 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 84 public PerfResult runProbeLocalTests(int iterationCount) 85 { 86 StringBuffer results = new StringBuffer ("<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 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 results) throws Exception 112 { 113 results.append("\n+++ testTimings()\n"); 114 Object obj = new InitialContext ().lookup("java:comp/env/ejb/ProbeHome"); 115 Class 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 results) throws Exception 132 { 133 results.append("\n+++ testTimings()\n"); 134 Object obj = new InitialContext ().lookup("java:comp/env/ejb/ProbeLocalHome"); 135 Class 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 results) throws Exception 153 { 154 bean.noop(); 155 bean.ping("Ping"); 156 bean.echo("Echo"); 157 } 158 private void warmup(ProbeLocal bean, StringBuffer results) throws Exception 159 { 160 bean.noop(); 161 bean.ping("Ping"); 162 bean.echo("Echo"); 163 } 164 private void noop(Probe bean, int iterationCount, StringBuffer results) throws Exception 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 results) throws Exception 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 results) throws Exception 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 results) throws Exception 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 results) throws Exception 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 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 results) throws Exception 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 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 t, String testName, StringBuffer results) 250 { 251 StringWriter sw = new StringWriter (); 252 PrintWriter pw = new PrintWriter (sw); 253 t.printStackTrace(pw); 254 results.append("\n"+testName+" failed:\n"); 255 results.append(sw.toString()); 256 } 257 258 } 259 | Popular Tags |