resultcollector.java

来自「测试工具」· Java 代码 · 共 557 行 · 第 1/2 页

JAVA
557
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.reporters;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.engine.util.NoThreadClone;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.samplers.Clearable;
import org.apache.jmeter.samplers.Remoteable;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.CSVSaveService;
import org.apache.jmeter.save.OldSaveService;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.save.TestResultWrapper;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.testelement.property.ObjectProperty;
import org.apache.jmeter.visualizers.Visualizer;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JMeterError;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;

public class ResultCollector extends AbstractListenerElement implements SampleListener, Clearable, Serializable,
		TestListener, Remoteable, NoThreadClone {

	private static final Logger log = LoggingManager.getLoggerForClass();

	private static final long serialVersionUID = 231L;

	// This string is used to identify local test runs, so must not be a valid host name
	private static final String TEST_IS_LOCAL = "*local*"; // $NON-NLS-1$

	private static final String TESTRESULTS_START = "<testResults>"; // $NON-NLS-1$

	private static final String TESTRESULTS_START_V1_1_PREVER = "<testResults version=\"";  // $NON-NLS-1$
        private static final String TESTRESULTS_START_V1_1_POSTVER="\">"; // $NON-NLS-1$

	private static final String TESTRESULTS_END = "</testResults>"; // $NON-NLS-1$

	private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; // $NON-NLS-1$

	private static final int MIN_XML_FILE_LEN = XML_HEADER.length() + TESTRESULTS_START.length()
			+ TESTRESULTS_END.length();

	public final static String FILENAME = "filename"; // $NON-NLS-1$

	private final static String SAVE_CONFIG = "saveConfig"; // $NON-NLS-1$

	private static final String ERROR_LOGGING = "ResultCollector.error_logging"; // $NON-NLS-1$

	private static final String SUCCESS_ONLY_LOGGING = "ResultCollector.success_only_logging"; // $NON-NLS-1$

	transient private DefaultConfigurationSerializer serializer;

	transient private volatile PrintWriter out;

	private boolean inTest = false;

	private static Map files = new HashMap();

	private Set hosts = new HashSet();

	protected boolean isStats = false;

	/**
	 * No-arg constructor.
	 */
	public ResultCollector() {
		// current = -1;
		// serializer = new DefaultConfigurationSerializer();
		setErrorLogging(false);
		setSuccessOnlyLogging(false);
		setProperty(new ObjectProperty(SAVE_CONFIG, new SampleSaveConfiguration()));
	}

	// Ensure that the sample save config is not shared between copied nodes
	public Object clone(){
		ResultCollector clone = (ResultCollector) super.clone();
		clone.setSaveConfig((SampleSaveConfiguration)clone.getSaveConfig().clone());
		return clone;
	}

	private void setFilenameProperty(String f) {
		setProperty(FILENAME, f);
	}

	public String getFilename() {
		return getPropertyAsString(FILENAME);
	}

	public boolean isErrorLogging() {
		return getPropertyAsBoolean(ERROR_LOGGING);
	}

	public void setErrorLogging(boolean errorLogging) {
		setProperty(new BooleanProperty(ERROR_LOGGING, errorLogging));
	}

	public void setSuccessOnlyLogging(boolean value) {
		if (value) {
		    setProperty(new BooleanProperty(SUCCESS_ONLY_LOGGING, true));
		} else {
			removeProperty(SUCCESS_ONLY_LOGGING);
		}
	}

	public boolean isSuccessOnlyLogging() {
		return getPropertyAsBoolean(SUCCESS_ONLY_LOGGING,false);
	}

	/**
	 * Decides whether or not to a sample is wanted based on:
	 * - errorOnly
	 * - successOnly
	 * - sample success
	 * 
	 * @param success is sample successful
	 * @return whether to log/display the sample
	 */
	public boolean isSampleWanted(boolean success){
		boolean errorOnly = isErrorLogging();
		boolean successOnly = isSuccessOnlyLogging();
		return (!errorOnly && !successOnly) || 
		       (success && successOnly) ||
			   (!success && errorOnly);
		// successOnly and errorOnly cannot both be set
	}
	/**
	 * Sets the filename attribute of the ResultCollector object.
	 * 
	 * @param f
	 *            the new filename value
	 */
	public void setFilename(String f) {
		if (inTest) {
			return;
		}
		setFilenameProperty(f);
	}

	public void testEnded(String host) {
		hosts.remove(host);
		if (hosts.size() == 0) {
			finalizeFileOutput();
			inTest = false;
		}
	}

	public void testStarted(String host) {
		hosts.add(host);
		try {
			initializeFileOutput();
			if (getVisualizer() != null) {
				this.isStats = getVisualizer().isStats();
			}
		} catch (Exception e) {
			log.error("", e);
		}
		inTest = true;
	}

	public void testEnded() {
		testEnded(TEST_IS_LOCAL);
	}

	public void testStarted() {
		testStarted(TEST_IS_LOCAL);
	}

    /**
     * Loads an existing sample data (JTL) file.
     * This can be one of:
     * - XStream format
     * - Avalon format
     * - CSV format
     * 
     */
	public void loadExistingFile() {
		final Visualizer visualizer = getVisualizer();
		if (visualizer == null) {
			return; // No point reading the file if there's no visualiser
		}
		boolean parsedOK = false, errorDetected = false;
		String filename = getFilename();
        File file = new File(filename);
        if (file.exists()) {
			clearVisualizer();
			BufferedReader dataReader = null;
            BufferedInputStream bufferedInputStream = null;
            try {
                dataReader = new BufferedReader(new FileReader(file));
                // Get the first line, and see if it is XML
                String line = dataReader.readLine();
                if (line == null) {
                    log.warn(filename+" is empty");
                } else {
                    if (!line.startsWith("<?xml ")){// No, must be CSV //$NON-NLS-1$
                    	long lineNumber=1;
                    	SampleSaveConfiguration saveConfig = CSVSaveService.getSampleSaveConfiguration(line,filename);
                    	if (saveConfig == null) {// not a valid header
                    		saveConfig = (SampleSaveConfiguration) getSaveConfig().clone(); // CSVSaveService may change the format
                    	} else { // header line has been processed, so read the next
                            line = dataReader.readLine();
                            lineNumber++;
                    	}
                        while (line != null) { // Already read 1st line
                            SampleEvent event = CSVSaveService.makeResultFromDelimitedString(line,saveConfig,lineNumber);
                            if (event != null){
								final SampleResult result = event.getResult();
                            	if (isSampleWanted(result.isSuccessful())) {
									visualizer.add(result);
								}
                            }
                            line = dataReader.readLine();
                            lineNumber++;
                        }
                        parsedOK = true;                                
                    } else { // We are processing XML
                        try { // Assume XStream
                            bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                            readSamples(SaveService.loadTestResults(bufferedInputStream), visualizer);
                            parsedOK = true;
                        } catch (Exception e) {
                            log.info("Failed to load "+filename+" using XStream, trying old XML format. Error was: "+e);
                            try {
                                OldSaveService.processSamples(filename, visualizer, this);
                                parsedOK = true;
                            } catch (Exception e1) {
                                log.warn("Error parsing Avalon XML. " + e1.getLocalizedMessage());
                            }
                        }
                    }
                }
			} catch (IOException e) {
                log.warn("Problem reading JTL file: "+file);
			} catch (JMeterError e){
                log.warn("Problem reading JTL file: "+file);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?