1 64 65 package com.jcorporate.expresso.ext.controller; 66 67 import com.jcorporate.expresso.core.controller.ControllerException; 68 import com.jcorporate.expresso.core.controller.ControllerRequest; 69 import com.jcorporate.expresso.core.controller.ControllerResponse; 70 import com.jcorporate.expresso.core.controller.DBController; 71 import com.jcorporate.expresso.core.controller.Input; 72 import com.jcorporate.expresso.core.controller.NonHandleableException; 73 import com.jcorporate.expresso.core.controller.Output; 74 import com.jcorporate.expresso.core.controller.State; 75 import com.jcorporate.expresso.core.controller.Transition; 76 import com.jcorporate.expresso.core.db.DBException; 77 import com.jcorporate.expresso.core.misc.DateTime; 78 import com.jcorporate.expresso.core.misc.EventHandler; 79 import com.jcorporate.expresso.core.misc.StringUtil; 80 import com.jcorporate.expresso.ext.dbobj.PerfTestResult; 81 import com.jcorporate.expresso.ext.dbobj.PerfTestSet; 82 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 83 import com.jcorporate.expresso.services.dbobj.Event; 84 85 import java.util.Enumeration ; 86 import java.util.Iterator ; 87 import java.util.StringTokenizer ; 88 import java.util.Vector ; 89 90 91 97 public class RunTests 98 extends DBController { 99 100 public RunTests() { 101 State prompt = new State("prompt", "Prompt for Command"); 102 addState(prompt); 103 setInitialState("prompt"); 104 105 State runtests = new State("runtests", "Run Tests"); 106 runtests.addRequiredParameter("sets"); 107 runtests.addRequiredParameter("repeats"); 108 runtests.addRequiredParameter("threads"); 109 addState(runtests); 110 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 111 } 112 113 118 public ControllerResponse newState(String newState, 119 ControllerRequest params) 120 throws ControllerException, 121 NonHandleableException { 122 ControllerResponse myResponse = super.newState(newState, params); 123 124 if (newState.equals("prompt")) { 125 promptState(myResponse, params); 126 } else if (newState.equals("runtests")) { 127 runTestsState(myResponse, params); 128 } 129 130 return myResponse; 131 } 132 133 134 private void promptState(ControllerResponse myResponse, 135 ControllerRequest params) 136 throws ControllerException { 137 myResponse.addInput(new Input("sets", 138 "Test Sets To Run (Comma separator)")); 139 myResponse.addInput(new Input("repeats", "Repeats")); 140 myResponse.addInput(new Input("threads", "Threads")); 141 142 Transition runTests = new Transition("Run Tests", getClass().getName()); 143 runTests.setName("runtests"); 144 runTests.addParam(STATE_PARAM_KEY, "runtests"); 145 myResponse.addTransition(runTests); 146 } 147 148 151 public String getTitle() { 152 return "Run Performance Test Sets"; 153 } 154 155 161 private void runTestsState(ControllerResponse myResponse, 162 ControllerRequest params) 163 throws ControllerException { 164 try { 165 FastStringBuffer mailMessage = new FastStringBuffer(1024); 166 mailMessage.append("Tests Start at " + DateTime.getDateTimeString() + 167 "\n"); 168 myResponse.addOutput(new Output("Test Start - db '" + params.getDataContext() + 169 "'")); 170 171 StringBuffer message = new StringBuffer ("RunTests\n"); 172 message.append("\nRunTests at " + DateTime.getDateTimeForDB(params.getDataContext()) + "\n"); 173 message.append("For database/context '" + params.getDataContext() + 174 "'\n\n"); 175 176 String setsToRunString = StringUtil.notNull((String ) params.getParameter("sets")); 177 178 if (setsToRunString.equals("")) { 179 setsToRunString = "all"; 180 } 181 182 String repeatsStr = StringUtil.notNull((String ) params.getParameter("repeats")); 183 184 if (repeatsStr.equals("")) { 185 repeatsStr = "1"; 186 } 187 188 String threadCountStr = StringUtil.notNull((String ) params.getParameter("threads")); 189 190 if (threadCountStr.equals("")) { 191 threadCountStr = "1"; 192 } 193 194 int repeats = 1; 195 196 try { 197 repeats = new Integer (repeatsStr).intValue(); 198 } catch (NumberFormatException ne) { 199 myResponse.addOutput(new Output("Number of repeats '" + repeatsStr + 200 "' not valid. Running only once.")); 201 } 202 203 int threadCount = 1; 204 205 try { 206 threadCount = new Integer (threadCountStr).intValue(); 207 } catch (NumberFormatException ne) { 208 myResponse.addOutput(new Output("Number of threads '" + threadCountStr + 209 "' not valid. Running only one thread.")); 210 } 211 212 Vector setsToRun = new Vector (); 213 boolean noProblems = true; 214 PerfTestResult overallResult = new PerfTestResult(); 215 216 try { 217 if (setsToRunString.equals("all")) { 218 PerfTestSet ps = new PerfTestSet(); 219 ps.setDataContext(params.getDataContext()); 220 221 PerfTestSet oneSet = null; 222 223 for (Iterator eps = ps.searchAndRetrieveList().iterator(); 224 eps.hasNext();) { 225 oneSet = (PerfTestSet) eps.next(); 226 setsToRun.addElement(oneSet.getField("SetNumber")); 227 } 228 } else { 229 StringTokenizer stk = new StringTokenizer (setsToRunString, ","); 230 231 while (stk.hasMoreTokens()) { 232 setsToRun.addElement(stk.nextElement()); 233 } 234 } 235 236 myResponse.addOutput(new Output("Running tests " + repeats + 237 " time(s)")); 238 message.append("\nRunning " + setsToRun.size() + " test sets."); 239 240 int activeThreads = 0; 241 Vector threadVector = new Vector (); 242 243 for (int i = 1; i <= threadCount; i++) { 244 RunTestThread rt = new RunTestThread(); 245 threadVector.addElement(rt); 246 rt.setDBName(params.getDataContext()); 247 rt.setTests((Vector ) setsToRun.clone()); 248 rt.setThreadNumber(i); 249 rt.setRepeats(repeats); 250 rt.start(); 251 activeThreads++; 252 } 253 254 255 myResponse.addOutput(new Output("" + activeThreads + 256 " threads started")); 257 258 RunTestThread oneThread = null; 259 260 while (activeThreads > 0) { 261 262 activeThreads = 0; 266 267 for (Enumeration te = threadVector.elements(); 268 te.hasMoreElements();) { 269 oneThread = (RunTestThread) te.nextElement(); 270 271 if (!oneThread.isAlive()) { 272 myResponse.addOutput(new Output("Thread " + oneThread.getThreadNumber() + 273 " completed.")); 274 275 if (activeThreads > 0) { 276 activeThreads--; 277 } 278 } else { 279 activeThreads++; 280 } 281 } 282 283 System.gc(); 284 285 } 287 for (Enumeration te = threadVector.elements(); 288 te.hasMoreElements();) { 289 oneThread = (RunTestThread) te.nextElement(); 290 overallResult.add(oneThread.getResults()); 291 } 292 293 message.append(overallResult.getMessage()); 294 message.append("\nThere were "); 295 296 if (overallResult.getFailureCount() > 0) { 297 if (overallResult.getFailureCount() == 1) { 298 message.append("was 1 failure, "); 299 } else { 300 message.append("were " + overallResult.getFailureCount() + 301 " failures, "); 302 } 303 } else { 304 message.append("no failures, "); 305 } 306 if (overallResult.getWarningCount() > 0) { 307 if (overallResult.getWarningCount() == 1) { 308 message.append("1 warning, "); 309 } else { 310 message.append("" + overallResult.getWarningCount() + 311 " warnings, "); 312 } 313 } else { 314 message.append("no warnings, "); 315 } 316 317 message.append("and "); 318 319 if (overallResult.getCautionCount() > 0) { 320 if (overallResult.getCautionCount() == 1) { 321 message.append("1 caution, "); 322 } else { 323 message.append("were " + overallResult.getCautionCount() + 324 " cautions, "); 325 } 326 } else { 327 message.append("no cautions."); 328 } 329 330 message.append("\n"); 331 message.append("There were " + overallResult.getCurrentUsers() + 332 " users logged in during tests\n"); 333 message.append("Total execution time: " + 334 overallResult.getRunTime() + " milliseconds"); 335 336 if ((overallResult.getFailureCount() > 0) || 337 (overallResult.getWarningCount() > 0) || 338 (overallResult.getCautionCount() > 0)) { 339 noProblems = false; 340 } 341 try { 342 new Event(params.getDataContext(), "SYSERROR", message.toString(), 343 noProblems); 344 EventHandler.flush(); 345 } catch (Exception me) { 346 myResponse.addOutput(new Output("WARNING: Unble to send email of test results:" + me.getMessage())); 347 me.printStackTrace(System.out); 348 } 349 350 myResponse.addOutput(new Output(message.toString())); 351 } catch (Exception de) { 352 de.printStackTrace(System.err); 353 throw new ControllerException(de); 354 } 355 356 myResponse.addOutput(new Output("All Tests Complete.")); 357 } catch (DBException dbe) { 358 throw new ControllerException(dbe); 359 } 360 } 361 362 363 } 364 | Popular Tags |