1 22 package org.jboss.test.perf.test; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.StringWriter ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.security.CodeSource ; 30 import java.security.ProtectionDomain ; 31 import java.text.NumberFormat ; 32 import javax.naming.InitialContext ; 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 46 public class PerfTest implements PerfTestMBean 47 { 48 private static NumberFormat fmt = NumberFormat.getInstance(); 49 static 50 { 51 fmt.setMinimumFractionDigits(3); 52 fmt.setMaximumFractionDigits(3); 53 } 54 55 int iterationCount; 56 57 public String runTests(int iterationCount) 58 { 59 StringBuffer results = new StringBuffer ("<h1>PerfTest.results</h1><pre>\n"); 60 this.iterationCount = iterationCount; 61 int testCount = 0; 62 int failureCount = 0; 63 64 URL 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 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 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 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 results) throws Exception 110 { 111 results.append("\n+++ testTimings()"); 112 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 113 Thread.currentThread().setContextClassLoader(ProbeHome.class.getClassLoader()); 114 try 115 { 116 Object obj = new InitialContext ().lookup("perf.Probe"); 117 Class homeClass = obj.getClass(); 119 ClassLoader cl = homeClass.getClassLoader(); 120 results.append("\nProbeHome.ClassLoader="+cl); 121 ClassLoader parent = cl; 122 while( parent != null ) 123 { 124 results.append("\n.."+parent); 125 if( parent instanceof URLClassLoader ) 126 { 127 URLClassLoader ucl = (URLClassLoader ) parent; 128 URL [] 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 [] ifaces = homeClass.getInterfaces(); 140 for(int i = 0; i < ifaces.length; i ++) 141 { 142 results.append("\n++"+ifaces[i]); 143 ProtectionDomain pd = ifaces[i].getProtectionDomain(); 144 CodeSource cs = pd.getCodeSource(); 145 if( cs != null ) 146 results.append("\n++++CodeSource: "+cs); 147 else 148 results.append("\n++++Null CodeSource"); 149 } 150 CodeSource 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 } } 170 171 public void testTimingsCMT(StringBuffer results) throws Exception 172 { 173 results.append("\n+++ testTimingsCMT()"); 174 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 175 Thread.currentThread().setContextClassLoader(ProbeHome.class.getClassLoader()); 176 try 177 { 178 Object obj = new InitialContext ().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 } } 193 194 public void testTxTimings(StringBuffer results) throws Exception 195 { 196 results.append("\n+++ testTxTimings()"); 197 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 198 Thread.currentThread().setContextClassLoader(TxSessionHome.class.getClassLoader()); 199 try 200 { 201 Object obj = new InitialContext ().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 } } 219 220 private void warmup(Probe bean, StringBuffer results) throws Exception 221 { 222 bean.noop(); 223 bean.ping("Ping"); 224 bean.echo("Echo"); 225 } 226 private void noop(Probe bean, StringBuffer results) throws Exception 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 results) throws Exception 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 results) throws Exception 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 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 results) throws Exception 268 { 269 results.append("\nStarting "+iterationCount+" txRequired() invocations"); 270 long start = System.currentTimeMillis(); 271 for(int n = 0; n < iterationCount; n ++) 272 { 273 String 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 results) throws Exception 283 { 284 results.append("\nStarting "+iterationCount+" txRequired() invocations"); 285 long start = System.currentTimeMillis(); 286 for(int n = 0; n < iterationCount; n ++) 287 { 288 String 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 results) throws Exception 298 { 299 results.append("\nStarting "+iterationCount+" txSupports() invocations"); 300 long start = System.currentTimeMillis(); 301 for(int n = 0; n < iterationCount; n ++) 302 { 303 String 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 results) throws Exception 313 { 314 results.append("\nStarting "+iterationCount+" txNotSupported() invocations"); 315 long start = System.currentTimeMillis(); 316 for(int n = 0; n < iterationCount; n ++) 317 { 318 String 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 results) throws Exception 328 { 329 results.append("\nStarting "+iterationCount+" requiredToSupports() invocations"); 330 long start = System.currentTimeMillis(); 331 for(int n = 0; n < iterationCount; n ++) 332 { 333 String 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 results) throws Exception 343 { 344 results.append("\nStarting "+iterationCount+" requiredToMandatory() invocations"); 345 long start = System.currentTimeMillis(); 346 for(int n = 0; n < iterationCount; n ++) 347 { 348 String 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 results) throws Exception 358 { 359 results.append("\nStarting "+iterationCount+" requiredToRequiresNew() invocations"); 360 long start = System.currentTimeMillis(); 361 for(int n = 0; n < iterationCount; n ++) 362 { 363 String 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 t, String testName, StringBuffer results) 374 { 375 StringWriter sw = new StringWriter (); 376 PrintWriter pw = new PrintWriter (sw); 377 t.printStackTrace(pw); 378 results.append("\n"+testName+" failed:\n"); 379 results.append(sw.toString()); 380 } 381 382 } 383 | Popular Tags |