⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sampleresult.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return new String(responseData,getDataEncodingWithDefault());
        } catch (UnsupportedEncodingException e) {
            log.warn("Using platform default as "+getDataEncodingWithDefault()+" caused "+e);
            return new String(responseData);
        }
    }

	public void setSamplerData(String s) {
		samplerData = s;
	}

	public String getSamplerData() {
		return samplerData;
	}

	/**
	 * Get the time it took this sample to occur.
	 * 
	 * @return elapsed time in milliseonds
	 * 
	 */
	public long getTime() {
		return time;
	}

	public boolean isSuccessful() {
		return success;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getDataType() {
		return dataType;
	}
    /**
     * Set Encoding and DataType from ContentType
     * @param ct - content type (may be null)
     */
    public void setEncodingAndType(String ct){
        if (ct != null) {
            // Extract charset and store as DataEncoding
            // N.B. The meta tag:
            // <META http-equiv="content-type" content="text/html; charset=foobar">
        	// is now processed by HTTPSampleResult#getDataEncodingWithDefault
            final String CS_PFX = "charset="; // $NON-NLS-1$
            int cset = ct.toLowerCase().indexOf(CS_PFX);
            if (cset >= 0) {
            	// TODO - assumes charset is not followed by anything else
                String charSet = ct.substring(cset + CS_PFX.length());
                // Check for quoted string
                if (charSet.startsWith("\"")){ // $NON-NLS-1$
                	setDataEncoding(charSet.substring(1, charSet.length()-1)); // remove quotes
                } else {
				    setDataEncoding(charSet);
                }
            }
            if (isTextType(ct)) {
                setDataType(TEXT);
            } else {
                setDataType(BINARY);
            }
        }
    }

    // List of types that can be processed as text
    private static final String[] TEXT_TYPES = {
    	"text/",                  //$NON-NLS-1$
    	"application/javascript", //$NON-NLS-1$
    	"application/json",       //$NON-NLS-1$
    	"application/xhtml+xml",  //$NON-NLS-1$
        };
 
    // Additional types as needed
    private static final String[] TEXT_TYPES_OPT =
    	JOrphanUtils.split(JMeterUtils.getPropDefault("content-type_text", ""), ","); //$NON-NLS-1$ $NON-NLS-2$ $NON-NLS-3$
 
    /*
     * Determine if content-type can be displayed as text or not.
     * 
     * @param ct content type
     * @return true if content-type is of type text.
     */
    private static boolean isTextType(String ct){
    	for (int i = 0; i < TEXT_TYPES.length; i++){
        	if (ct.startsWith(TEXT_TYPES[i])){
        		return true;
        	}    		
    	}
    	for (int i = 0; i < TEXT_TYPES_OPT.length; i++){
        	if (ct.startsWith(TEXT_TYPES_OPT[i])){
        		return true;
        	}    		
    	}
    	return false;
    }
	/**
	 * Sets the successful attribute of the SampleResult object.
	 * 
	 * @param success
	 *            the new successful value
	 */
	public void setSuccessful(boolean success) {
		this.success = success;
	}

	/**
	 * Returns the display name.
	 * 
	 * @return display name of this sample result
	 */
	public String toString() {
		return getSampleLabel();
	}

	/**
	 * Returns the dataEncoding or the default if no dataEncoding was provided
	 * 
	 * @deprecated use getDataEncodingWithDefault() or getDataEncodingNoDefault() as needed.
	 */
	public String getDataEncoding() {
		if (dataEncoding != null) {
			return dataEncoding;
		}
		return DEFAULT_ENCODING;
	}

	/**
	 * Returns the dataEncoding or the default if no dataEncoding was provided
	 */
	public String getDataEncodingWithDefault() {
		if (dataEncoding != null && dataEncoding.length() > 0) {
			return dataEncoding;
		}
		return DEFAULT_ENCODING;
	}

	/**
	 * Returns the dataEncoding or the default if no dataEncoding was provided
	 */
	public String getDataEncodingNoDefault() {
		return dataEncoding;
	}

	/**
	 * Sets the dataEncoding.
	 * 
	 * @param dataEncoding
	 *            the dataEncoding to set, e.g. ISO-8895-1, UTF-8
	 */
	public void setDataEncoding(String dataEncoding) {
		this.dataEncoding = dataEncoding;
	}

	/**
	 * @return whether to stop the test
	 */
	public boolean isStopTest() {
		return stopTest;
	}

	/**
	 * @return whether to stop this thread
	 */
	public boolean isStopThread() {
		return stopThread;
	}

	/**
	 * @param b
	 */
	public void setStopTest(boolean b) {
		stopTest = b;
	}

	/**
	 * @param b
	 */
	public void setStopThread(boolean b) {
		stopThread = b;
	}

	/**
	 * @return the request headers
	 */
	public String getRequestHeaders() {
		return requestHeaders;
	}

	/**
	 * @return the response headers
	 */
	public String getResponseHeaders() {
		return responseHeaders;
	}

	/**
	 * @param string -
	 *            request headers
	 */
	public void setRequestHeaders(String string) {
		requestHeaders = string;
	}

	/**
	 * @param string -
	 *            response headers
	 */
	public void setResponseHeaders(String string) {
		responseHeaders = string;
	}

	/**
	 * @return the full content type - e.g. text/html [;charset=utf-8 ]
	 */
	public String getContentType() {
		return contentType;
	}

    /**
     * Get the media type from the Content Type
     * @return the media type - e.g. text/html (without charset, if any)
     */
    public String getMediaType() {
        return JOrphanUtils.trim(contentType," ;").toLowerCase();
    }

	/**
	 * @param string
	 */
	public void setContentType(String string) {
		contentType = string;
	}

    /**
     * @return idleTime
     */
    public long getIdleTime() {
        return idleTime;
    }
    
	/**
	 * @return the end time
	 */
	public long getEndTime() {
		return endTime;
	}

	/**
	 * @return the start time
	 */
	public long getStartTime() {
		return startTime;
	}

	/*
	 * Helper methods N.B. setStartTime must be called before setEndTime
	 * 
	 * setStartTime is used by HTTPSampleResult to clone the parent sampler and
	 * allow the original start time to be kept
	 */
	protected final void setStartTime(long start) {
		startTime = start;
		if (startTimeStamp) {
			timeStamp = startTime;
		}
	}

	protected void setEndTime(long end) {
		endTime = end;
		if (!startTimeStamp) {
			timeStamp = endTime;
		}
		if (startTime == 0) {
			log.error("setEndTime must be called after setStartTime", new Throwable("Invalid call sequence"));
			// TODO should this throw an error?
		} else {
			time = endTime - startTime - idleTime;
		}
	}

	private void setTimes(long start, long end) {
		setStartTime(start);
		setEndTime(end);
	}

	/**
	 * Record the start time of a sample
	 * 
	 */
	public void sampleStart() {
		if (startTime == 0) {
			setStartTime(System.currentTimeMillis());
		} else {
			log.error("sampleStart called twice", new Throwable("Invalid call sequence"));
		}
	}

	/**
	 * Record the end time of a sample and calculate the elapsed time
	 * 
	 */
	public void sampleEnd() {
		if (endTime == 0) {
			setEndTime(System.currentTimeMillis());
		} else {
			log.error("sampleEnd called twice", new Throwable("Invalid call sequence"));
		}
	}

	/**
	 * Pause a sample
	 * 
	 */
	public void samplePause() {
		if (pauseTime != 0) {
			log.error("samplePause called twice", new Throwable("Invalid call sequence"));
		}
		pauseTime = System.currentTimeMillis();
	}

	/**
	 * Resume a sample
	 * 
	 */
	public void sampleResume() {
		if (pauseTime == 0) {
			log.error("sampleResume without samplePause", new Throwable("Invalid call sequence"));
		}
		idleTime += System.currentTimeMillis() - pauseTime;
		pauseTime = 0;
	}

	/**
	 * When a Sampler is working as a monitor
	 * 
	 * @param monitor
	 */
	public void setMonitor(boolean monitor) {
		isMonitor = monitor;
	}

	/**
	 * If the sampler is a monitor, method will return true.
	 * 
	 * @return true if the sampler is a monitor
	 */
	public boolean isMonitor() {
		return isMonitor;
	}

	/**
	 * For the JMS sampler, it can perform multiple samples for greater degree
	 * of accuracy.
	 * 
	 * @param count
	 */
	public void setSampleCount(int count) {
		sampleCount = count;
	}

	/**
	 * return the sample count. by default, the value is 1.
	 * 
	 * @return the sample count
	 */
	public int getSampleCount() {
		return sampleCount;
	}

	/**
	 * Returns the count of errors.
	 * 
	 * @return 0 - or 1 if the sample failed
	 */
	public int getErrorCount(){
		return success ? 0 : 1;
	}
	
	public void setErrorCount(int i){// for reading from CSV files
		// ignored currently
	}
	/*
	 * TODO: error counting needs to be sorted out after 2.3 final.
	 * At present the Statistical Sampler tracks errors separately
	 * It would make sense to move the error count here, but this would
	 * mean lots of changes.
	 * It's also tricky maintaining the count - it can't just be incremented/decremented
	 * when the success flag is set as this may be done multiple times.
	 * The work-round for now is to do the work in the StatisticalSampleResult,
	 * which overrides this method.
	 * Note that some JMS samplers also create samples with > 1 sample count
	 * Also the Transaction Controller probably needs to be changed to do
	 * proper sample and error accounting.
	 * The purpose of this work-round is to allow at least minimal support for
	 * errors in remote statistical batch mode.
	 * 
	 */
	/**
	 * In the event the sampler does want to pass back the actual contents, we
	 * still want to calculate the throughput. The bytes is the bytes of the
	 * response data.
	 * 
	 * @param length
	 */
	public void setBytes(int length) {
		bytes = length;
	}

	/**
	 * return the bytes returned by the response.
	 * 
	 * @return byte count
	 */
	public int getBytes() {
		return bytes == 0 ? responseData.length : bytes;
	}

	/**
	 * @return Returns the latency.
	 */
	public long getLatency() {
		return latency;
	}

    /**
     * Set the time to the first response
     *
     */
	public void latencyEnd() {
		latency = System.currentTimeMillis() - startTime - idleTime;
	}

	/**
     * This is only intended for use by SampleResultConverter!
     * 
	 * @param latency
	 *            The latency to set.
	 */
	public void setLatency(long latency) {
		this.latency = latency;
	}

	/**
     * This is only intended for use by SampleResultConverter!
     *
	 * @param timeStamp
	 *            The timeStamp to set.
	 */
	public void setTimeStamp(long timeStamp) {
		this.timeStamp = timeStamp;
	}

	private URL location;

	public void setURL(URL location) {
		this.location = location;
	}

	public URL getURL() {
		return location;
	}
	
	/**
	 * Get a String representation of the URL (if defined).
	 * 
	 * @return ExternalForm of URL, or empty string if url is null
	 */
	public String getUrlAsString() {
		return location == null ? "" : location.toExternalForm();
	}

	/**
	 * @return Returns the parent.
	 */
	public SampleResult getParent() {
		return parent;
	}

	/**
	 * @param parent
	 *            The parent to set.
	 */
	public void setParent(SampleResult parent) {
		this.parent = parent;
	}

    public String getResultFileName() {
        return resultFileName;
    }

    public void setResultFileName(String resultFileName) {
        this.resultFileName = resultFileName;
    }

	public int getGroupThreads() {
		return groupThreads;
	}

	public void setGroupThreads(int n) {
		this.groupThreads = n;
	}

	public int getAllThreads() {
		return allThreads;
	}

	public void setAllThreads(int n) {
		this.allThreads = n;
	}
}

⌨️ 快捷键说明

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