zoukankan      html  css  js  c++  java
  • 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下

    查看调用图:

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

  • 相关阅读:
    CentOS 7.0关闭默认防火墙启用iptables防火墙
    centos7 启动httpd的时候为什么显示是这样的
    CentOS配置本地yum源/阿里云yum源/163yuan源,并配置yum源的优先级
    Linux如何用yum安装软件或服务
    IE浏览器和Firefox浏览器兼容性问题及解决办法
    Input的size与maxlength属性的区别
    下拉框默认选择数据库取出数据
    登录到 SQL Server 实例
    安装sql server 2008重启失败
    值栈
  • 原文地址:https://www.cnblogs.com/RayMin/p/4444664.html
Copyright © 2011-2022 走看看