1 64 65 70 package com.jcorporate.expresso.ext.dbobj; 71 72 import com.jcorporate.expresso.core.controller.ControllerRequest; 73 import com.jcorporate.expresso.core.db.DBException; 74 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 75 import com.jcorporate.expresso.core.dbobj.ValidValue; 76 import com.jcorporate.expresso.core.misc.ConfigManager; 77 import com.jcorporate.expresso.core.misc.DateTime; 78 import com.jcorporate.expresso.core.misc.StringUtil; 79 import org.apache.log4j.Logger; 80 81 import java.io.IOException ; 82 import java.io.InputStream ; 83 import java.io.InputStreamReader ; 84 import java.net.HttpURLConnection ; 85 import java.net.MalformedURLException ; 86 import java.net.URL ; 87 import java.util.Hashtable ; 88 import java.util.Iterator ; 89 import java.util.List ; 90 import java.util.Vector ; 91 92 93 100 public class PerfTests 101 extends SecuredDBObject { 102 private static Vector httpValues = null; 103 private static Vector sessionValues = null; 104 private static Logger log = Logger.getLogger(PerfTests.class); 105 106 109 public PerfTests() 110 throws DBException { 111 super(); 112 } 113 114 121 public PerfTests(int uid) 122 throws DBException { 123 super(uid); 124 } 125 126 133 public PerfTests(ControllerRequest request) 134 throws DBException { 135 super(request); 136 } 137 138 143 public String getField(String fieldName) 144 throws DBException { 145 if (fieldName.equals("AverageTime")) { 146 long totCounts = 0; 147 148 try { 149 totCounts = new Long (getField("TestCount")).longValue(); 150 } catch (NumberFormatException ne) { 151 totCounts = 0; 152 } 153 154 long totTime = 0; 155 156 try { 157 totTime = new Long (getField("TotalTime")).longValue(); 158 } catch (NumberFormatException ne) { 159 totTime = 0; 160 } 161 if (totTime <= 0) { 162 return ("0"); 163 } else { 164 return ("" + totTime / totCounts); 165 } 166 } else if (fieldName.equals("FailedTests")) { 167 long totCounts = 0; 168 169 try { 170 totCounts = new Long (getField("TestCount")).longValue(); 171 } catch (NumberFormatException ne) { 172 totCounts = 0; 173 } 174 175 long successCount = 0; 176 177 try { 178 successCount = new Long (getField("SuccessfulTests")).longValue(); 179 } catch (NumberFormatException ne) { 180 successCount = 0; 181 } 182 183 return ("" + (totCounts - successCount)); 184 } 185 186 return super.getField(fieldName); 187 } 188 189 190 public Vector getValidValues(String fieldName) 191 throws DBException { 192 if (fieldName.equals("HttpMethod")) { 193 if (httpValues == null) { 194 httpValues = new Vector (); 195 httpValues.addElement(new ValidValue("GET", "GET")); 196 httpValues.addElement(new ValidValue("POST", "POST")); 197 } 198 199 return httpValues; 200 } else if (fieldName.equals("GetSession")) { 201 if (sessionValues == null) { 202 sessionValues = new Vector (); 203 sessionValues.addElement(new ValidValue("Y", "Yes")); 204 sessionValues.addElement(new ValidValue("N", "No")); 205 } 206 207 return sessionValues; 208 } 209 210 return super.getValidValues(fieldName); 211 } 212 213 214 217 public void setupFields() 218 throws DBException { 219 setTargetTable("PTESTS"); 220 setDescription("DBptests"); 221 setCharset("ISO-8859-1"); 222 addField("TestNumber", "auto-inc", 0, false, "testNumber"); 223 addField("URLToTest", "text", 0, false, "urlToTest"); 224 addField("Descrip", "varchar", 80, false, "descriptionOfTest"); 225 addField("NormTime", "int", 0, true, "normalTime"); 226 addField("WarnTime", "int", 0, true, "warningTime"); 227 addField("MaxTime", "int", 0, true, "maxTime"); 228 addField("LastTime", "int", 0, true, "lastRunTime"); 229 addField("TestCount", "int", 0, true, "nofRuns"); 230 addField("TotalTime", "int", 0, true, "totalTimeAllRuns"); 231 addVirtualField("AverageTime", "int", 0, "avgTimeAllRuns"); 232 addField("ExpectString", "text", 0, true, "expectText"); 233 addField("SuccessfulTests", "int", 0, true, "successfulTests"); 234 addVirtualField("FailedTests", "int", 0, "failedTests"); 235 addField("LastTestSucceeded", "char", 1, true, "lastTestSucceeded"); 236 addField("LastRun", "datetime", 0, true, "testLastRunOn"); 237 addField("LastFailureReason", "text", 0, true, "reasonForLastFailure"); 238 addField("HttpMethod", "char", 10, false, "httpMethod"); 239 addField("GetSession", "char", 1, false, "getSession"); 240 this.getJDBCMetaData().setAttribute("GetSession", "checkbox", "Y"); 241 setStringFilter("URLToTest", "rawFilter"); 242 setStringFilter("ExpectString", "rawFilter"); 243 addKey("TestNumber"); 244 245 258 setMultiValued("HttpMethod"); 259 setMultiValued("GetSession"); 260 } 261 262 263 269 public PerfTestResult run(String currentSessionId) 270 throws DBException { 271 PerfTestResult myResult = new PerfTestResult(); 272 URL myUrl = null; 273 HttpURLConnection c; 274 String reason = ""; 275 boolean failed = false; 276 String urlString = ConfigManager.expandValue(getField("URLToTest")); 277 278 if (currentSessionId != null) { 279 try { 280 URL myURL = new URL (urlString); 281 String queryPart = StringUtil.notNull(myURL.getQuery()); 282 String pathPart = myURL.getPath(); 283 int port = myURL.getPort(); 284 String protocol = myURL.getProtocol(); 285 String hostPart = myURL.getHost(); 286 287 294 295 urlString = protocol + "://" + hostPart + ":" + port + 297 pathPart + ";jsessionid=" + currentSessionId; 298 299 if (!queryPart.equals("")) { 300 urlString = urlString + "?" + queryPart; 301 } 302 303 log.info("Session was established: new URL '" + urlString + 305 "'"); 306 } catch (MalformedURLException me) { 307 throw new DBException(me); 308 } 309 } 310 311 long runTime = 0; 312 String encodedURL = null; 313 314 try { 315 encodedURL = urlString; 317 if (encodedURL == null) { 318 throw new IllegalArgumentException ("Unable to encode URL '" + 319 urlString + "'"); 320 } 321 322 myUrl = new URL (encodedURL); 323 324 if (myUrl == null) { 325 throw new IllegalArgumentException ("Unable to encode URL '" + 326 urlString + "'"); 327 } 328 } catch (MalformedURLException m) { 329 m.printStackTrace(System.out); 330 throw new IllegalArgumentException ("URL '" + urlString + 331 "' is not valid."); 332 } 333 try { 334 c = (HttpURLConnection ) myUrl.openConnection(); 335 336 if (c == null) { 337 throw new IllegalArgumentException ("No connection established to '" + urlString + "'"); 338 } 339 340 c.setDoOutput(true); 341 HttpURLConnection.setFollowRedirects(true); 342 c.setAllowUserInteraction(true); 343 c.setRequestMethod(getField("HttpMethod")); 344 345 346 boolean printme = false; 347 InputStream is = null; 348 349 try { 350 is = c.getInputStream(); 351 } catch (IOException ie) { 352 is = c.getErrorStream(); 353 printme = true; 354 355 if (is == null) { 356 System.out.println("No input or error stream from URL '" + 357 encodedURL + "'"); 358 } else { 359 System.out.println("Got error stream"); 360 } 361 try { 362 int responseCode = c.getResponseCode(); 363 System.out.println("Response code:" + responseCode); 364 } catch (Exception ee) { 365 System.out.println("No response code available:" + 366 ee.getMessage()); 367 ee.printStackTrace(System.out); 368 } 369 370 } 373 if (is == null) { 374 throw new IllegalArgumentException ("No input or error stream returned"); 375 } 376 377 InputStreamReader dis = new InputStreamReader (is); 378 long startTime = System.currentTimeMillis(); 379 URLReader ur = new URLReader(dis, getField("ExpectString"), 380 printme); 381 382 if (getField("GetSession").equals("Y")) { 383 ur.setRequireSession(true); 384 } 385 386 ur.start(); 387 388 long maxMillis = 0; 389 390 try { 391 maxMillis = new Long (getField("MaxTime")).longValue(); 392 } catch (NumberFormatException ne) { 393 maxMillis = 0; 394 } 395 if (maxMillis == 0) { 396 maxMillis = 99999; 397 } 398 399 ur.join(maxMillis); 400 401 boolean success = ur.completedSuccessfully(); 402 long endTime = System.currentTimeMillis(); 403 runTime = endTime - startTime; 404 myResult.setRunTime(runTime); 405 406 Hashtable allUsers = ConfigManager.getCurrentLogins(); 407 myResult.setCurrentUsers(allUsers.size()); 408 409 if (success) { 410 411 432 } 433 if (getField("GetSession").equals("Y")) { 434 currentSessionId = ur.getSessionId(); 435 myResult.setSessionId(currentSessionId); 436 currentSessionId = myResult.getSessionId(); 437 log.info("Logged in with session id '" + currentSessionId + 438 "'"); 439 440 if (currentSessionId == null) { 441 reason = "No jsessionid (Session ID) found in output"; 442 failed = true; 443 } 444 } 445 if (!success) { 446 failed = true; 447 reason = ur.getFailureReason(); 448 } 449 } catch (Exception io) { 450 log.error("I/O Error:", io); 451 io.printStackTrace(System.out); 452 reason = "I/O Error communicating with server:" + io.getMessage(); 453 failed = true; 454 } 455 if (failed) { 456 setField("LastTestSucceeded", "N"); 457 setField("LastFailureReason", reason); 458 } else { 459 setField("LastTestSucceeded", "Y"); 460 addToField("SuccessfulTests", 1); 461 } 462 463 addToField("TestCount", 1); 464 addToField("TotalTime", runTime); 465 setField("LastTime", "" + runTime); 466 setField("LastRun", DateTime.getDateTimeForDB(this.getDataContext())); 467 setCheckZeroUpdate(false); 468 update(); 469 470 471 long lastTime = 0; 472 StringBuffer detailMessage = new StringBuffer (); 473 474 if (getField("LastTestSucceeded").equals("N")) { 475 myResult.setFailureCount(1); 476 detailMessage.append("\nERROR: Test " + getField("TestNumber") + 477 " (" + getField("Descrip") + ") for URL '" + 478 getField("URLToTest") + 479 "' failed. Reason reported was '" + 480 getField("LastFailureReason") + "'\n"); 481 } else { 482 lastTime = getFieldLong("LastTime"); 483 484 if (lastTime > getFieldLong("WarnTime")) { 485 myResult.setWarningCount(1); 486 detailMessage.append("\nWARNING: Test " + 487 getField("TestNumber") + " (" + 488 getField("Descrip") + ") for URL '" + 489 getField("URLToTest") + 490 "' responded slower than it's set warning time of " + 491 getField("WarnTime") + 492 " milliseconds. It ran in " + 493 getField("LastTime") + 494 " milliseconds.\n"); 495 } else if (lastTime > getFieldLong("NormTime")) { 496 myResult.setCautionCount(1); 497 detailMessage.append("\nCAUTION: Test " + 498 getField("TestNumber") + " (" + 499 getField("Descrip") + ") for URL '" + 500 getField("URLToTest") + 501 "' responded slower than it's set normal time of " + 502 getField("NormTime") + 503 " milliseconds. It ran in " + 504 getField("LastTime") + 505 " milliseconds.\n"); 506 } 507 } 508 509 myResult.setMessage(detailMessage.toString()); 510 511 return myResult; 512 } 513 514 515 520 private void addToField(String fieldName, long valueToAdd) 521 throws DBException { 522 long currentValue = 0; 523 524 try { 525 currentValue = new Long (getField(fieldName)).longValue(); 526 } catch (NumberFormatException ne) { 527 throw new DBException("Unable to add to field '" + fieldName + 528 "', current value of '" + 529 getField(fieldName) + "' is not a number."); 530 } 531 532 currentValue = currentValue + valueToAdd; 533 setField(fieldName, "" + currentValue); 534 } 535 536 537 545 public static PerfTestResult runAll(String dbName, String currentSessionId) 546 throws DBException { 547 PerfTests tl = new PerfTests(); 548 tl.setDataContext(dbName); 549 550 PerfTestResult overallResult = new PerfTestResult(); 551 PerfTestResult oneResult = null; 552 PerfTests oneTest = null; 553 554 List list = tl.searchAndRetrieveList(); 555 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 556 oneTest = (PerfTests) iterator.next(); 557 oneResult = oneTest.run(currentSessionId); 558 overallResult.add(oneResult); 559 } 560 561 return overallResult; 562 } 563 564 565 573 public static PerfTestResult runSet(String dbName, String setNumber, 574 String currentSessionId) 575 throws DBException { 576 PerfTestResult overallResult = new PerfTestResult(); 577 PerfTestResult oneResult = null; 578 PerfTestSet ps = new PerfTestSet(); 579 ps.setDataContext(dbName); 580 ps.setField("SetNumber", setNumber); 581 ps.retrieve(); 582 583 PerfTestSetDet psd = new PerfTestSetDet(); 584 psd.setDataContext(dbName); 585 psd.setField("SetNumber", setNumber); 586 587 PerfTestSetDet oneDet = null; 588 PerfTests oneTest = new PerfTests(); 589 oneTest.setDataContext(dbName); 590 591 List list = psd.searchAndRetrieveList("SequenceNumber"); 592 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 593 oneDet = (PerfTestSetDet) iterator.next(); 594 oneTest.setField("TestNumber", oneDet.getField("TestNumber")); 595 oneTest.retrieve(); 596 oneResult = oneTest.run(currentSessionId); 597 598 if (currentSessionId == null) { 599 currentSessionId = oneResult.getSessionId(); 600 overallResult.setSessionId(currentSessionId); 601 log.info("Test " + oneDet.getField("TestNumber") + 602 " logged in - session id now '" + currentSessionId + 603 "'"); 604 } 605 606 overallResult.add(oneResult); 607 } 608 609 return overallResult; 610 } 611 612 613 622 667 668 } 669 | Popular Tags |