1 18 19 package org.apache.jmeter.reporters; 20 21 22 import java.io.BufferedOutputStream ; 23 import java.io.BufferedReader ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.io.OutputStreamWriter ; 31 import java.io.PrintWriter ; 32 import java.io.RandomAccessFile ; 33 import java.io.Serializable ; 34 import java.util.HashMap ; 35 import java.util.HashSet ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 import org.apache.avalon.framework.configuration.Configuration; 40 import org.apache.avalon.framework.configuration.ConfigurationException; 41 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 42 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer; 43 import org.apache.jmeter.engine.event.LoopIterationEvent; 44 import org.apache.jmeter.engine.util.NoThreadClone; 45 import org.apache.jmeter.samplers.Clearable; 46 import org.apache.jmeter.samplers.Remoteable; 47 import org.apache.jmeter.samplers.SampleEvent; 48 import org.apache.jmeter.samplers.SampleListener; 49 import org.apache.jmeter.samplers.SampleResult; 50 import org.apache.jmeter.save.SaveService; 51 import org.apache.jmeter.testelement.TestListener; 52 import org.apache.jmeter.testelement.property.BooleanProperty; 53 import org.apache.jorphan.logging.LoggingManager; 54 import org.apache.log.Logger; 55 import org.xml.sax.SAXException ; 56 57 58 61 public class ResultCollector 62 extends AbstractListenerElement 63 implements 64 SampleListener, 65 Clearable, 66 Serializable , 67 TestListener, 68 Remoteable, 69 NoThreadClone 70 { 71 private static final String TESTRESULTS_START = "<testResults>"; private static final String TESTRESULTS_END = "</testResults>"; private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; private static final int MIN_XML_FILE_LEN = 75 XML_HEADER.length() + TESTRESULTS_START.length() + TESTRESULTS_END.length(); 76 transient private static Logger log = LoggingManager.getLoggerForClass(); 77 public final static String FILENAME = "filename"; transient private static boolean functionalMode = false; 79 public static final String ERROR_LOGGING = "ResultCollector.error_logging"; transient private DefaultConfigurationSerializer serializer; 83 transient private volatile PrintWriter out; 85 transient private boolean inTest = false; 86 transient private static Map files = new HashMap (); 87 transient private Set hosts = new HashSet (); 88 89 92 public ResultCollector() 93 { 94 serializer = new DefaultConfigurationSerializer(); 96 setErrorLogging(false); 97 } 98 99 private void setFilenameProperty(String f) 100 { 101 setProperty(FILENAME, f); 102 } 103 104 public String getFilename() 105 { 106 return getPropertyAsString(FILENAME); 107 } 108 109 public boolean isErrorLogging() 110 { 111 return getPropertyAsBoolean(ERROR_LOGGING); 112 } 113 114 public void setErrorLogging(boolean errorLogging) 115 { 116 setProperty(new BooleanProperty(ERROR_LOGGING, errorLogging)); 117 } 118 119 124 public void setFilename(String f) 125 { 126 if (inTest) 127 { 128 return; 129 } 130 setFilenameProperty(f); 131 } 132 133 public void testEnded(String host) 134 { 135 hosts.remove(host); 136 if (hosts.size() == 0) 137 { 138 finalizeFileOutput(); 139 inTest = false; 140 } 141 } 142 143 public void testStarted(String host) 144 { 145 hosts.add(host); 146 try 147 { 148 initializeFileOutput(); } 150 catch (Exception e) 151 { 152 log.error("", e); 153 } 154 inTest = true; 155 } 156 157 public void testEnded() 158 { 159 testEnded("local"); } 161 162 public void testStarted() 163 { 164 testStarted("local"); } 166 167 public void loadExistingFile() 168 throws SAXException , IOException , ConfigurationException 169 { 170 if (new File (getFilename()).exists()) 172 { 173 clearVisualizer(); 174 BufferedReader dataReader = null; 175 try 176 { 177 Configuration savedSamples = getConfiguration(getFilename()); 178 readSamples(savedSamples); 179 } 180 catch(SAXException e) 181 { 182 dataReader = new BufferedReader (new FileReader (getFilename())); 183 String line; 184 while((line = dataReader.readLine()) != null) 185 { 186 sendToVisualizer(SaveService.makeResultFromDelimitedString(line)); 187 } 188 } 189 catch (Exception e) 190 { 191 log.error("", e); 192 } 193 finally 194 { 195 if (dataReader != null) dataReader.close(); 196 } 197 } 198 } 200 201 private static void writeFileStart(PrintWriter writer) 202 { 203 if (SaveService.getOutputFormat() == SaveService.SAVE_AS_XML) 204 { 205 writer.println(XML_HEADER); 206 writer.println(TESTRESULTS_START); 207 } 208 else if (SaveService.getOutputFormat() == SaveService.SAVE_AS_CSV) 209 { 210 if (SaveService.getPrintFieldNames()) 211 { 212 writer.println(SaveService.printableFieldNamesToString()); 213 } 214 } 215 } 216 217 218 private static void writeFileEnd(PrintWriter pw) 219 { 220 if (SaveService.getOutputFormat() == SaveService.SAVE_AS_XML) 221 { 222 pw.print("\n"); pw.print(TESTRESULTS_END); 224 } 225 } 226 227 private static synchronized PrintWriter getFileWriter(String filename) 228 throws IOException 229 { 230 if (filename == null || filename.length() == 0) 231 { 232 return null; 233 } 234 PrintWriter writer = (PrintWriter ) files.get(filename); 235 boolean trimmed = true; 236 237 if (writer == null) 238 { 239 if (SaveService.getOutputFormat() == SaveService.SAVE_AS_XML) 240 { 241 trimmed = trimLastLine(filename); 242 } 243 writer = 244 new PrintWriter ( 245 new OutputStreamWriter ( 246 new BufferedOutputStream ( 247 new FileOutputStream (filename, trimmed)), 248 "UTF-8"), true); 250 files.put(filename, writer); 251 } 252 if (!trimmed) 253 { 254 writeFileStart(writer); 255 } 256 return writer; 257 } 258 259 private static boolean trimLastLine(String filename) 261 { 262 RandomAccessFile raf = null; 263 try 264 { 265 raf = new RandomAccessFile (filename,"rw"); long len = raf.length(); 267 if (len < MIN_XML_FILE_LEN) 268 { 269 return false; 270 } 271 raf.seek(len-TESTRESULTS_END.length()-10); String line; 273 long pos = raf.getFilePointer(); 274 int end=0; 275 while((line = raf.readLine()) != null) { 277 end = line.indexOf(TESTRESULTS_END); 278 if (end >= 0) { 280 break; 281 } 282 pos = raf.getFilePointer(); 283 } 284 if (line == null) 285 { 286 log.warn("Unexpected EOF trying to find XML end marker in "+filename); 287 raf.close(); 288 return false; 289 } 290 raf.setLength(pos+end); raf.close(); 292 } 293 catch (FileNotFoundException e) 294 { 295 return false; 296 } catch (IOException e) { 297 log.warn("Error trying to find XML terminator "+e.toString()); 298 try { 299 if (raf != null) raf.close(); 300 } catch (IOException e1) {} 301 return false; 302 } 303 return true; 304 } 305 306 public static void enableFunctionalMode(boolean mode) 307 { 308 functionalMode = mode; 309 } 310 311 public boolean getFunctionalMode() 312 { 313 return functionalMode || isErrorLogging(); 314 } 315 316 322 private String getSerializedSampleResult(SampleResult result) 323 throws SAXException , IOException , ConfigurationException 324 { 325 ByteArrayOutputStream tempOut = new ByteArrayOutputStream (); 326 327 serializer.serialize( 328 tempOut, 329 SaveService.getConfiguration(result, getFunctionalMode())); 330 String serVer = tempOut.toString(); 331 int index = serVer.indexOf(System.getProperty("line.separator")); 332 if(index > -1) 333 { 334 return serVer.substring(index); 335 } 336 else 337 { 338 return serVer; 339 } 340 } 341 342 private void readSamples(Configuration testResults) 343 throws IOException , SAXException , ConfigurationException 344 { 345 Configuration[] samples = testResults.getChildren(); 346 347 for (int i = 0; i < samples.length; i++) 348 { 349 SampleResult result = SaveService.getSampleResult(samples[i]); 350 351 sendToVisualizer(result); 352 recordResult(result); 353 } 354 } 355 356 361 private Configuration getConfiguration(String filename) 362 throws SAXException , IOException , ConfigurationException 363 { 364 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 365 366 return builder.buildFromFile(filename); 367 } 368 369 public void clearVisualizer() 370 { 371 if (getVisualizer() != null && getVisualizer() instanceof Clearable) 373 { 374 ((Clearable) getVisualizer()).clear(); 375 } 376 finalizeFileOutput(); 377 } 378 379 public void setListener(Object l) 380 { 381 } 382 383 public void sampleStarted(SampleEvent e) 384 { 385 } 386 387 public void sampleStopped(SampleEvent e) 388 { 389 } 390 391 392 396 public void sampleOccurred(SampleEvent e) 397 { 398 SampleResult result = e.getResult(); 399 400 if (!isErrorLogging() || !result.isSuccessful()) 401 { 402 sendToVisualizer(result); 403 404 try 405 { 406 if (SaveService.getOutputFormat() == SaveService.SAVE_AS_CSV) 407 { 408 if (out != null) 409 { 410 String savee = 411 SaveService.resultToDelimitedString(result); 412 out.println(savee); 413 } 414 } 415 else 417 { 418 recordResult(result); 419 } 420 } 421 catch (Exception err) 422 { 423 log.error("", err); } 425 } 426 } 427 428 protected void sendToVisualizer(SampleResult r) 429 { 430 if (getVisualizer() != null) 431 { 432 getVisualizer().add(r); 433 } 434 } 435 436 private void recordResult(SampleResult result) 437 throws SAXException , IOException , ConfigurationException 438 { 439 if (out != null) 440 { 441 if (!isResultMarked(result)) 442 { 443 out.print(getSerializedSampleResult(result)); 444 } 445 } 446 } 447 448 private synchronized boolean isResultMarked(SampleResult res) 449 { 450 String filename = getFilename(); 451 boolean marked = res.isMarked(filename); 452 453 if (!marked) 454 { 455 res.setMarked(filename); 456 } 457 return marked; 458 } 459 460 private synchronized void initializeFileOutput() 461 throws IOException , ConfigurationException, SAXException 462 { 463 464 String filename = getFilename(); 465 if (out == null && filename != null) 466 { 467 if (out == null) 468 { 469 try 470 { 471 out = getFileWriter(filename); 472 } 473 catch (FileNotFoundException e) 474 { 475 out = null; 476 } 477 } 478 } 479 } 480 481 private synchronized void finalizeFileOutput() 482 { 483 if (out != null) 484 { 485 writeFileEnd(out); 486 out.close(); 487 files.remove(getFilename()); 488 out = null; 489 } 490 } 491 492 495 public void testIterationStart(LoopIterationEvent event) 496 { 497 } 498 } 499 | Popular Tags |