Jmeter二次开发代码(1)
package org.apache.jmeter.functions;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.JMeterUtils;
public class MyExcel extends AbstractFunction {
private static final List<String> desc = new LinkedList<>();
private static final String KEY = "__MyExcel"; // Function name //$NON-NLS-1$
private Object[] values;
//初始化
public MyExcel() {
}
static {
desc.add(JMeterUtils.getResString("EXCEL文件路径")); //文字说明
desc.add(JMeterUtils.getResString("所要读取EXCEL文件的SHEET页")); //文字说明
desc.add(JMeterUtils.getResString("读取的起始行")); //文字说明
desc.add(JMeterUtils.getResString("读取的起始列")); //文字说明
}
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
String myExcelValue = "";
// 定义Jmeter传参
String excelFilePath = (((CompoundVariable) values[0]).execute()).toString();
String sheetName = (((CompoundVariable) values[1]).execute()).toString();
int rowNum = new Integer(((CompoundVariable) values[2]).execute().trim());
int columnNum = new Integer(((CompoundVariable) values[3]).execute().trim());
myExcelValue = MyExcelRead.getColumn(excelFilePath, sheetName, rowNum, columnNum);
return myExcelValue;
}
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
//检查参数个数
checkParameterCount(parameters, 4);
values = parameters.toArray();
MyExcelRead.clearAll();
}
/** {@inheritDoc} */
@Override
public String getReferenceKey() {
return KEY;
}
/** {@inheritDoc} */
@Override
public List<String> getArgumentDesc() {
return desc;
}
}