1 57 package performance; 58 59 import java.io.File ; 60 import java.io.FileInputStream ; 61 import java.io.FileOutputStream ; 62 import java.util.Enumeration ; 63 import java.util.HashMap ; 64 import java.util.Iterator ; 65 import java.util.Properties ; 66 67 import junit.framework.Test; 68 import junit.framework.TestCase; 69 import junit.framework.TestSuite; 70 71 import org.apache.wsif.WSIFMessage; 72 import org.apache.wsif.WSIFOperation; 73 import org.apache.wsif.WSIFPort; 74 import org.apache.wsif.WSIFService; 75 import org.apache.wsif.WSIFServiceFactory; 76 import org.apache.wsif.base.WSIFServiceImpl; 77 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis; 78 import org.apache.wsif.util.WSIFPluggableProviders; 79 80 import util.TestUtilities; 81 82 import addressbook.wsiftypes.Address; 83 import addressbook.wsiftypes.Phone; 84 85 120 public class SpeedTest extends TestCase { 121 122 static final float TIME_PER_TEST = 20000.0F; 128 static final float MARGIN_OF_ERROR = 0.1F; 134 static final boolean UPDATE_STATS = true; 136 137 static final File propFile = new File ( "wsifPerformanceStats.txt" ); 139 140 static final int DEFAULT_ITERATIONS = 500; 141 142 static final boolean TEST_GETPORT = true; 143 static final boolean TEST_CREATEOP = true; 144 static final boolean TEST_EXECOP = true; 145 146 static Properties stats, newStats; 147 148 static final String wsdlLocation = 149 TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice") 150 + "AddressBook.wsdl"; 151 152 public SpeedTest(String name) { 153 super(name); 154 } 155 156 public static void main(String [] args) { 157 TestUtilities.startListeners(); 158 junit.textui.TestRunner.run (suite()); 159 TestUtilities.stopListeners(); 160 } 161 162 public static Test suite() { 163 TestSuite suite = new TestSuite("Speed Tests"); 164 suite.addTest(new TestSuite(SpeedTest.class) ); 165 return suite; 168 } 169 170 public void setUp() { 171 TestUtilities.setUpExtensionsAndProviders(); 172 newStats = new Properties (); 173 Monitor.clear(); 174 } 175 176 public void testJava() { 177 doit("JavaPort", "java"); 178 } 179 199 200 private void doit(String portName, String protocol) { 201 int iterations; 202 String testName; 203 String testNamePrefix = protocol + "." + portName; 204 205 if (portName.toUpperCase().indexOf("JMS") != -1 206 && !TestUtilities.areWeTesting("jms")) 207 return; 208 209 TestUtilities.setProviderForProtocol( protocol ); 210 211 try { 212 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 213 WSIFService service = factory.getService(wsdlLocation, null, null, "http://wsifservice.addressbook/", "AddressBook"); 218 service.mapType( 219 new javax.xml.namespace.QName ( 220 "http://wsiftypes.addressbook/", 221 "address"), 222 Class.forName("addressbook.wsiftypes.Address")); 223 224 service.mapType( 225 new javax.xml.namespace.QName ( 226 "http://wsiftypes.addressbook/", 227 "phone"), 228 Class.forName("addressbook.wsiftypes.Phone")); 229 230 WSIFPort port = null; 231 232 port = service.getPort(portName); 233 234 237 testName = testNamePrefix + ".getPort"; 238 iterations = (TEST_GETPORT)? getIterations( testName ) : 1; 239 System.out.println( "running " + iterations + " " + testName + " iterations..." ); 240 for (int i = 0; i < iterations; i++ ) { 241 Monitor.start( testName ); 242 port = service.getPort(portName); 243 Monitor.stop( testName ); 244 } 245 246 WSIFOperation operation = 247 port.createOperation("addEntry", "AddEntryWholeNameRequest", null); 248 249 WSIFMessage inputMessage = operation.createInputMessage(); 250 WSIFMessage outputMessage = operation.createOutputMessage(); 251 WSIFMessage faultMessage = operation.createFaultMessage(); 252 253 String nameToAdd = "Chris P. Bacon"; 255 Address addressToAdd = 256 new Address( 257 1, 258 "The Waterfront", 259 "Some City", 260 "NY", 261 47907, 262 new Phone(765, "494", "4900")); 263 264 inputMessage.setObjectPart("name", nameToAdd); 266 inputMessage.setObjectPart("address", addressToAdd); 267 268 boolean ok= 270 operation.executeRequestResponseOperation( 271 inputMessage, 272 outputMessage, 273 faultMessage); 274 275 assertTrue( "failed to add name and address!!", ok ); 276 277 280 testName = testNamePrefix + ".createOperation"; 281 iterations = (TEST_CREATEOP)? getIterations( testName ) : 1; 282 System.out.println( "running " + iterations + " " + testName + " iterations..." ); 283 for (int i = 0; i < iterations; i++ ) { 284 Monitor.start( testName ); 285 operation = port.createOperation("getAddressFromName"); 286 Monitor.stop( testName ); 287 } 288 289 292 testName = testNamePrefix + ".executeOperation"; 293 iterations = (TEST_EXECOP)? getIterations( testName ) : 1; 294 System.out.println( "running " + iterations + " " + testName + " iterations..." ); 295 for (int i = 0; i < iterations; i++ ) { 296 operation = port.createOperation("getAddressFromName"); 297 inputMessage = operation.createInputMessage(); 298 outputMessage = operation.createOutputMessage(); 299 faultMessage = operation.createFaultMessage(); 300 301 inputMessage.setObjectPart("name", nameToAdd); 302 303 Monitor.start( testName ); 304 boolean operationSucceeded = 305 operation.executeRequestResponseOperation( 306 inputMessage, 307 outputMessage, 308 faultMessage); 309 Monitor.stop( testName ); 310 if (!operationSucceeded) { 311 System.out.println("Failed to lookup name in addressbook"); 312 assertTrue("executing op returned false!!", false); 313 } 314 } 315 316 Address addressFound = 318 (Address) outputMessage.getObjectPart("address"); 319 assertTrue( "returned address not correct!!", 320 addressToAdd.equals( addressFound) ); 321 322 Monitor.printResults(); 323 324 boolean worse = testResults(); 326 setIterations( testNamePrefix ); 327 mergeNewStats(); 328 if ( UPDATE_STATS ) { 329 saveStatsToFile(); 330 } 331 332 assertTrue( "performance is worse than before!!", !worse ); 333 334 } catch (Exception ex) { 335 ex.printStackTrace(); 336 assertTrue("exception executing op!!", false); 337 } 338 } 339 340 private boolean testResults() { 341 float diff; 342 String s1, s2; 343 boolean worse = false; 344 345 HashMap results = Monitor.getAvgResults(); 346 String testName; 347 float duration, oldDuration; 348 for (Iterator i = results.keySet().iterator(); i.hasNext(); ) { 349 testName = (String )i.next(); 350 duration = ((Float )results.get( testName )).floatValue(); 351 duration = ((float)Math.round( duration * 1000 ) / 1000 ); 352 oldDuration = getfloatStat( testName ); 353 newStats.setProperty( testName, ""+duration ); 354 diff = duration - oldDuration; 355 if ( Math.abs(diff) - MARGIN_OF_ERROR > 0 ) { 356 worse = diff > 0; 357 System.err.println( testName + " significantly " + 358 ( worse? "worse" : "better" ) + 359 " by " + diff + " msecs, time = " + duration + " msecs" ); 360 } 361 } 362 363 return worse; 364 } 365 366 private int getIterations(String testName) { 367 int i; 368 i = getintStat( testName + ".Iterations" ); 369 if ( i == 0 ) { 370 i = DEFAULT_ITERATIONS; 371 } 372 return i; 373 } 374 375 private void setIterations(String prefix) { 376 String s; 377 int iterations; 378 float duration, createDuration; 379 HashMap results = Monitor.getAvgResults(); 380 381 s = prefix + ".getPort"; 382 duration = ((Float )results.get( s )).floatValue(); 383 iterations = (duration==0) ? 384 DEFAULT_ITERATIONS : (int)((TIME_PER_TEST*100) / (duration*100)); 385 newStats.setProperty( s + ".Iterations", ""+iterations ); 386 387 s = prefix + ".createOperation"; 388 duration = ((Float )results.get( s )).floatValue(); 389 iterations = (duration==0) ? 390 DEFAULT_ITERATIONS : (int)((TIME_PER_TEST*100) / (duration*100)); 391 newStats.setProperty( s + ".Iterations", ""+iterations ); 392 createDuration = duration; 393 394 s = prefix + ".executeOperation"; 395 duration = ((Float )results.get( s )).floatValue(); 396 iterations = (duration==0) ? 397 DEFAULT_ITERATIONS : (int)((TIME_PER_TEST*100) / ((duration+createDuration)*100)); 398 newStats.setProperty( s + ".Iterations", ""+iterations ); 399 400 } 401 402 private int getintStat(String name) { 403 int i; 404 Properties stats = getStats(); 405 try { 406 i = Integer.parseInt(stats.getProperty( name ) ); 407 } catch (Exception ex) { 408 i = 0; 409 } 410 return i; 411 } 412 413 private float getfloatStat(String name) { 414 float f; 415 Properties stats = getStats(); 416 try { 417 f = Float.parseFloat(stats.getProperty( name ) ); 418 } catch (Exception ex) { 419 f = 0; 420 } 421 return f; 422 } 423 424 private static Properties getStats() { 425 if ( stats == null ) { 426 loadStatsFromFile(); 427 } 428 return stats; 429 } 430 431 private static void mergeNewStats() { 432 Properties stats = getStats(); 433 String testName; 434 String value; 435 int iterations; 436 for (Enumeration i = newStats.keys(); i.hasMoreElements(); ) { 437 testName = (String )i.nextElement(); 438 value = (String )newStats.get( testName ); 439 stats.setProperty( testName, value ); 440 } 441 } 442 443 private static void loadStatsFromFile() { 444 try { 445 FileInputStream fis = new FileInputStream ( propFile ); 446 stats = new Properties (); 447 stats.load( fis ); 448 } catch (Exception ex) { 449 stats = new Properties (); 450 } 451 } 452 453 private static void saveStatsToFile() { 454 try { 455 FileOutputStream fos = new FileOutputStream ( propFile ); 456 stats.store( fos, "WSIF Performance stats" ); 457 } catch (Exception ex) { 458 ex.printStackTrace(); 459 } 460 } 461 462 } | Popular Tags |