📄 sampleresultconverter.java
字号:
/*
* 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.save.converters;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.SaveService;
import org.apache.jorphan.util.Converter;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
/**
* XStream Converter for the SampleResult class
*/
public class SampleResultConverter extends AbstractCollectionConverter {
//private static final Logger log = LoggingManager.getLoggerForClass();
private static final String JAVA_LANG_STRING = "java.lang.String"; //$NON-NLS-1$
private static final String ATT_CLASS = "class"; //$NON-NLS-1$
// Element tags. Must be unique. Keep sorted.
protected static final String TAG_COOKIES = "cookies"; //$NON-NLS-1$
protected static final String TAG_METHOD = "method"; //$NON-NLS-1$
protected static final String TAG_QUERY_STRING = "queryString"; //$NON-NLS-1$
protected static final String TAG_REDIRECT_LOCATION = "redirectLocation"; //$NON-NLS-1$
protected static final String TAG_REQUEST_HEADER = "requestHeader"; //$NON-NLS-1$
//NOT USED protected static final String TAG_URL = "requestUrl"; //$NON-NLS-1$
protected static final String TAG_RESPONSE_DATA = "responseData"; //$NON-NLS-1$
protected static final String TAG_RESPONSE_HEADER = "responseHeader"; //$NON-NLS-1$
protected static final String TAG_SAMPLER_DATA = "samplerData"; //$NON-NLS-1$
protected static final String TAG_RESPONSE_FILE = "responseFile"; //$NON-NLS-1$
// samplerData attributes. Must be unique. Keep sorted by string value.
// Ensure the Listener documentation is updated when new attributes are added
private static final String ATT_BYTES = "by"; //$NON-NLS-1$
private static final String ATT_DATA_ENCODING = "de"; //$NON-NLS-1$
private static final String ATT_DATA_TYPE = "dt"; //$NON-NLS-1$
private static final String ATT_ERROR_COUNT = "ec"; //$NON-NLS-1$
private static final String ATT_HOSTNAME = "hn"; //$NON-NLS-1$
private static final String ATT_LABEL = "lb"; //$NON-NLS-1$
private static final String ATT_LATENCY = "lt"; //$NON-NLS-1$
private static final String ATT_ALL_THRDS = "na"; //$NON-NLS-1$
private static final String ATT_GRP_THRDS = "ng"; //$NON-NLS-1$
// N.B. Originally the response code was saved with the code "rs"
// but retrieved with the code "rc". Changed to always use "rc", but
// allow for "rs" when restoring values.
private static final String ATT_RESPONSE_CODE = "rc"; //$NON-NLS-1$
private static final String ATT_RESPONSE_MESSAGE = "rm"; //$NON-NLS-1$
private static final String ATT_RESPONSE_CODE_OLD = "rs"; //$NON-NLS-1$
private static final String ATT_SUCCESS = "s"; //$NON-NLS-1$
private static final String ATT_SAMPLE_COUNT = "sc"; //$NON-NLS-1$
private static final String ATT_TIME = "t"; //$NON-NLS-1$
private static final String ATT_THREADNAME = "tn"; //$NON-NLS-1$
private static final String ATT_TIME_STAMP = "ts"; //$NON-NLS-1$
/**
* Returns the converter version; used to check for possible
* incompatibilities
*/
public static String getVersion() {
return "$Revision: 595166 $"; //$NON-NLS-1$
}
/*
* (non-Javadoc)
*
* @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
*/
public boolean canConvert(Class arg0) {
return SampleResult.class.equals(arg0);
}
/*
* (non-Javadoc)
*
* @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
* com.thoughtworks.xstream.io.HierarchicalStreamWriter,
* com.thoughtworks.xstream.converters.MarshallingContext)
*/
public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
SampleResult res = (SampleResult) obj;
SampleSaveConfiguration save = res.getSaveConfig();
setAttributes(writer, context, res, save);
saveAssertions(writer, context, res, save);
saveSubResults(writer, context, res, save);
saveResponseHeaders(writer, context, res, save);
saveRequestHeaders(writer, context, res, save);
saveResponseData(writer, context, res, save);
saveSamplerData(writer, context, res, save);
}
/**
* @param writer
* @param res
* @param save
*/
protected void saveSamplerData(HierarchicalStreamWriter writer, MarshallingContext context, SampleResult res,
SampleSaveConfiguration save) {
if (save.saveSamplerData(res)) {
writeString(writer, TAG_SAMPLER_DATA, res.getSamplerData());
}
if (save.saveUrl()) {
final URL url = res.getURL();
if (url != null) {
writeItem(url, context, writer);
}
}
}
/**
* @param writer
* @param res
* @param save
*/
protected void saveResponseData(HierarchicalStreamWriter writer, MarshallingContext context, SampleResult res,
SampleSaveConfiguration save) {
if (save.saveResponseData(res)) {
writer.startNode(TAG_RESPONSE_DATA);
writer.addAttribute(ATT_CLASS, JAVA_LANG_STRING);
try {
if (SampleResult.TEXT.equals(res.getDataType())){
writer.setValue(new String(res.getResponseData(), res.getDataEncodingWithDefault()));
}
// Otherwise don't save anything - no point
} catch (UnsupportedEncodingException e) {
writer.setValue("Unsupported encoding in response data, can't record.");
}
writer.endNode();
}
if (save.saveFileName()){
writer.startNode(TAG_RESPONSE_FILE);
writer.addAttribute(ATT_CLASS, JAVA_LANG_STRING);
writer.setValue(res.getResultFileName());
writer.endNode();
}
}
/**
* @param writer
* @param res
* @param save
*/
protected void saveRequestHeaders(HierarchicalStreamWriter writer, MarshallingContext context, SampleResult res,
SampleSaveConfiguration save) {
if (save.saveRequestHeaders()) {
writeString(writer, TAG_REQUEST_HEADER, res.getRequestHeaders());
}
}
/**
* @param writer
* @param res
* @param save
*/
protected void saveResponseHeaders(HierarchicalStreamWriter writer, MarshallingContext context, SampleResult res,
SampleSaveConfiguration save) {
if (save.saveResponseHeaders()) {
writeString(writer, TAG_RESPONSE_HEADER, res.getResponseHeaders());
}
}
/**
* @param writer
* @param context
* @param res
* @param save
*/
protected void saveSubResults(HierarchicalStreamWriter writer, MarshallingContext context, SampleResult res,
SampleSaveConfiguration save) {
if (save.saveSubresults()) {
SampleResult[] subResults = res.getSubResults();
for (int i = 0; i < subResults.length; i++) {
subResults[i].setSaveConfig(save);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -