接上一篇未解决的问题,继续...
beanshell前置处理器貌似因为作用域的问题解决不了,那这个问题怎么解决呢?
jmeter函数,可以自定义函数调用吗?答案是肯定的,下面附上代码:
其中FileRowColContainer为jmeter内部的类,刚好有文件行数的方法直接拿过来用,博友们也可以像上一篇beanshell中那样自己写
package try.jmeter.functions;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Collection;import java.util.LinkedList;import java.util.List;import org.apache.jmeter.engine.util.CompoundVariable;import org.apache.jmeter.functions.AbstractFunction;import org.apache.jmeter.functions.FileRowColContainer;import org.apache.jmeter.functions.InvalidVariableException;import org.apache.jmeter.samplers.SampleResult;import org.apache.jmeter.samplers.Sampler;import org.apache.jmeter.util.JMeterUtils;public class FileRowCount extends AbstractFunction{ private Object[] values; private FileRowColContainer fc; private static final Listdesc = new LinkedList (); static { desc.add(JMeterUtils.getResString("read_file_name")); } public List getArgumentDesc() { // TODO Auto-generated method stub return desc; } public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { // TODO Auto-generated method stub String myValue = ""; String fileName = ((CompoundVariable)this.values[0]).execute(); try { fc = new FileRowColContainer(fileName); myValue = String.valueOf(fc.getSize()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return myValue; } public String getReferenceKey() { // TODO Auto-generated method stub return "__FileRowCount"; } public synchronized void setParameters(Collection parameters) throws InvalidVariableException { // TODO Auto-generated method stub this.values = parameters.toArray(); checkParameterCount(parameters, 1); } }
注意事项:
1.包名必须包含.functions
2.继承AbstractFunction,实现抽象方法
3.打包成jar包放到jmeter/lib/ext下
查看调用图:
参数文件中共三行,问题终于解决了~~~