jmete 取配置文件的行数(二)

接上一篇未解决的问题,继续...

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 List<String> desc = new LinkedList<String>();
    
    static
      {
        desc.add(JMeterUtils.getResString("read_file_name"));
      }

    public List<String> 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<CompoundVariable> parameters)
            throws InvalidVariableException {
        // TODO Auto-generated method stub
        this.values = parameters.toArray();
        checkParameterCount(parameters, 1);
    }
    
}

注意事项:

1.包名必须包含.functions

2.继承AbstractFunction,实现抽象方法

3.打包成jar包放到jmeter/lib/ext下

查看调用图:

 

参数文件中共三行,问题终于解决了~~~

转载于:https://www.cnblogs.com/RayMin/p/4444664.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值