resultcollector.java
来自「测试工具」· Java 代码 · 共 557 行 · 第 1/2 页
JAVA
557 行
} finally {
JOrphanUtils.closeQuietly(dataReader);
JOrphanUtils.closeQuietly(bufferedInputStream);
if (!parsedOK || errorDetected) {
GuiPackage.showErrorMessage(
"Error loading results file - see log file",
"Result file loader");
}
}
} else {
GuiPackage.showErrorMessage(
"Error loading results file - could not open file",
"Result file loader");
}
}
private static void writeFileStart(PrintWriter writer, SampleSaveConfiguration saveConfig) {
if (saveConfig.saveAsXml()) {
writer.print(XML_HEADER);
// Write the EOL separately so we generate LF line ends on Unix and Windows
writer.print("\n"); // $NON-NLS-1$
String pi=saveConfig.getXmlPi();
if (pi.length() > 0) {
writer.println(pi);
}
// Can't do it as a static initialisation, because SaveService
// is being constructed when this is called
writer.print(TESTRESULTS_START_V1_1_PREVER);
writer.print(SaveService.getVERSION());
writer.print(TESTRESULTS_START_V1_1_POSTVER);
// Write the EOL separately so we generate LF line ends on Unix and Windows
writer.print("\n"); // $NON-NLS-1$
} else if (saveConfig.saveFieldNames()) {
writer.println(CSVSaveService.printableFieldNamesToString(saveConfig));
}
}
private static void writeFileEnd(PrintWriter pw, SampleSaveConfiguration saveConfig) {
if (saveConfig.saveAsXml()) {
pw.print("\n"); // $NON-NLS-1$
pw.print(TESTRESULTS_END);
pw.print("\n");// Added in version 1.1 // $NON-NLS-1$
}
}
private static synchronized PrintWriter getFileWriter(String filename, SampleSaveConfiguration saveConfig)
throws IOException {
if (filename == null || filename.length() == 0) {
return null;
}
PrintWriter writer = (PrintWriter) files.get(filename);
boolean trimmed = true;
if (writer == null) {
if (saveConfig.saveAsXml()) {
trimmed = trimLastLine(filename);
} else {
trimmed = new File(filename).exists();
}
// Find the name of the directory containing the file
// and create it - if there is one
File pdir = new File(filename).getParentFile();
if (pdir != null)
pdir.mkdirs();
writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(filename,
trimmed)), SaveService.getFileEncoding("UTF-8")), true); // $NON-NLS-1$
files.put(filename, writer);
}
if (!trimmed) {
writeFileStart(writer, saveConfig);
}
return writer;
}
// returns false if the file did not contain the terminator
private static boolean trimLastLine(String filename) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(filename, "rw"); // $NON-NLS-1$
long len = raf.length();
if (len < MIN_XML_FILE_LEN) {
return false;
}
raf.seek(len - TESTRESULTS_END.length() - 10);// TODO: may not work on all OSes?
String line;
long pos = raf.getFilePointer();
int end = 0;
while ((line = raf.readLine()) != null)// reads to end of line OR end of file
{
end = line.indexOf(TESTRESULTS_END);
if (end >= 0) // found the string
{
break;
}
pos = raf.getFilePointer();
}
if (line == null) {
log.warn("Unexpected EOF trying to find XML end marker in " + filename);
raf.close();
return false;
}
raf.setLength(pos + end);// Truncate the file
raf.close();
raf = null;
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
log.warn("Error trying to find XML terminator " + e.toString());
return false;
} finally {
try {
if (raf != null)
raf.close();
} catch (IOException e1) {
log.info("Could not close " + filename + " " + e1.getLocalizedMessage());
}
}
return true;
}
// Only called if visualizer is non-null
private void readSamples(TestResultWrapper testResults, Visualizer visualizer) throws Exception {
Collection samples = testResults.getSampleResults();
Iterator iter = samples.iterator();
while (iter.hasNext()) {
SampleResult result = (SampleResult) iter.next();
if (isSampleWanted(result.isSuccessful())) {
visualizer.add(result);
}
}
}
public void clearVisualizer() {
// current = -1;
if (getVisualizer() != null && getVisualizer() instanceof Clearable) {
((Clearable) getVisualizer()).clearData();
}
finalizeFileOutput();
}
public void sampleStarted(SampleEvent e) {
}
public void sampleStopped(SampleEvent e) {
}
/**
* When a test result is received, display it and save it.
*
* @param event
* the sample event that was received
*/
public void sampleOccurred(SampleEvent event) {
SampleResult result = event.getResult();
if (isSampleWanted(result.isSuccessful())) {
sendToVisualizer(result);
if ( out != null) {// no point otherwise
SampleSaveConfiguration config = getSaveConfig();
result.setSaveConfig(config);
try {
if (config.saveAsXml()) {
recordResult(event);
} else {
String savee = CSVSaveService.resultToDelimitedString(event);
out.println(savee);
}
} catch (Exception err) {
log.error("Error trying to record a sample", err); // should throw exception back to caller
}
}
}
}
protected final void sendToVisualizer(SampleResult r) {
if (getVisualizer() != null) {
getVisualizer().add(r);
}
}
// Only called if out != null
private void recordResult(SampleEvent event) throws Exception {
SampleResult result = event.getResult();
if (!isResultMarked(result) && !this.isStats) {
if (SaveService.isSaveTestLogFormat20()) {
if (serializer == null) {
serializer = new DefaultConfigurationSerializer();
}
out.write(OldSaveService.getSerializedSampleResult(result, serializer, getSaveConfig()));
} else {
SaveService.saveSampleResult(event, out);
}
}
}
/**
* recordStats is used to save statistics generated by visualizers
*
* @param e
* @throws Exception
*/
public void recordStats(TestElement e) throws Exception {
if (out == null) {
initializeFileOutput();
}
if (out != null) {
SaveService.saveTestElement(e, out);
}
}
private synchronized boolean isResultMarked(SampleResult res) {
String filename = getFilename();
boolean marked = res.isMarked(filename);
if (!marked) {
res.setMarked(filename);
}
return marked;
}
private void initializeFileOutput() throws IOException {
String filename = getFilename();
if (out == null && filename != null) {
if (out == null) {
try {
out = getFileWriter(filename, getSaveConfig());
} catch (FileNotFoundException e) {
out = null;
}
}
}
}
private synchronized void finalizeFileOutput() {
if (out != null) {
writeFileEnd(out, getSaveConfig());
out.close();
files.remove(getFilename());
out = null;
}
}
/*
* (non-Javadoc)
*
* @see TestListener#testIterationStart(LoopIterationEvent)
*/
public void testIterationStart(LoopIterationEvent event) {
}
/**
* @return Returns the saveConfig.
*/
public SampleSaveConfiguration getSaveConfig() {
try {
return (SampleSaveConfiguration) getProperty(SAVE_CONFIG).getObjectValue();
} catch (ClassCastException e) {
setSaveConfig(new SampleSaveConfiguration());
return getSaveConfig();
}
}
/**
* @param saveConfig
* The saveConfig to set.
*/
public void setSaveConfig(SampleSaveConfiguration saveConfig) {
getProperty(SAVE_CONFIG).setObjectValue(saveConfig);
}
// This is required so that
// @see org.apache.jmeter.gui.tree.JMeterTreeModel.getNodesOfType()
// can find the Clearable nodes - the userObject has to implement the interface.
public void clearData() {
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?