Jmeter自定義函數,取csv文件中指定行列的數據

 

public class GetCSVValue extends AbstractFunction {

 

private static final List<String> desc = new LinkedList<String>();

private static final String KEY = "__MyCSVRead";

 

private CompoundVariable varName;

private CompoundVariable fileUrl;

private CompoundVariable row;

private CompoundVariable col;

private BufferedReader reader;

 

static {

desc.add("fileUrl");

desc.add("row");

desc.add("col");

desc.add(JMeterUtils.getResString("function_name_paropt"));

}

 

/** {@inheritDoc} */

@Override

public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {

 

String fileUrlV = String.valueOf(fileUrl.execute().trim());

int rowv = Integer.valueOf(row.execute().trim());

int colv = Integer.valueOf(col.execute().trim());

 

String last = null;

try {

reader = new BufferedReader(new FileReader(fileUrlV));

// reader.readLine();// 第一行信息,爲標題信息,不用,如果需要,註釋掉

String line = null;

int index = 0;

while ((line = reader.readLine()) != null) {

String item[] = line.split(",");

if (index == rowv - 1) {

if (item.length >= colv - 1) {

last = item[colv - 1];

}

}

index++;

}

 

} catch (Exception e) {

e.printStackTrace();

}

 

if (varName != null) {

JMeterVariables vars = getVariables();

final String varTrim = varName.execute().trim();

if (vars != null && varTrim.length() > 0) {// vars will be null on TestPlan

vars.put(varTrim, last);

}

}

 

return last;

 

}

 

/** {@inheritDoc} */

@Override

public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {

checkParameterCount(parameters, 3, 4);

Object[] values = parameters.toArray();

 

fileUrl = (CompoundVariable) values[0];

row = (CompoundVariable) values[1];

col = (CompoundVariable) values[2];

if (values.length > 3) {

varName = (CompoundVariable) values[3];

} else {

varName = null;

}

 

}

 

/** {@inheritDoc} */

@Override

public String getReferenceKey() {

return KEY;

}

 

/** {@inheritDoc} */

public List<String> getArgumentDesc() {

return desc;

}

 

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章