jmeter自定义函数

jmeter本身带有很多内带的函数,功能很强大,但是更强大的是,jmeter还可以扩展,让用户自定义函数。

下面来看下自定义函数是怎么做的。

需要在org.apache.jmeter.functions包中,添加我们的自定义函数,函数模板如下:

java 复制代码
package org.fast.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;


public class myFunction extends AbstractFunction{

    @Override
    public String execute(SampleResult result, Sampler sampler) throws InvalidVariableException{
      return "jmeter函数";
    }

    @Override
    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException{
       checkParameterCount(collection,0,0);
    }

    @Override
    public String getReferenceKey()
    {
        return "__NewFunction";
    }

    @Override
    public List<String> getArgumentDesc()
    {
        return desc;
    }

}

对以上代码按照自己的需求重构并打成jar包

java 复制代码
package org.fast.jmeter.functions;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableFunction;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sample;
import org.fast.jmeter.functions.RandomString;

import java.util.LinkedList;
import java.util.Collection;
import java.util.List;

@SuppressWarnings("unchecked")
public class RandomMail extends AbstractFunction{

   @Override
   public String execute(SampleResult result, Sampler sample) throws InvalidVariableException{
     ...
   }
 
   @Override
   public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException{
     checkParameterCount(collection,0,0);
   }

   @Override
   public String getReferencKey()
   {
       return "__NewFunction";
   }

   public String getArgumentDesc()
   {
       return new LinkedList();
   }

接下来把打包好的jar包放到${jmeter_home}/lib/ext目录下,重启jmeter,打开函数助手,可以看到有RandomMail这个函数在列。

这样自定义函数就做好了。

相关推荐
软测进阶2 天前
【超详细图文教程】2025年最新 Jmeter 详细安装教程及接口测试示例
jmeter
kebeiovo5 天前
常用的几种测试工具:selenium,jmeter,jenkins
selenium·测试工具·jmeter
AIZHINAN6 天前
如何使用 JMeter 进行接口测试。
jmeter·接口测试
程序员小远6 天前
基于jmeter+perfmon的稳定性测试记录
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·测试用例
文人sec6 天前
性能测试-jmeter9-直连数据库
数据库·测试工具·jmeter
IDOlaoluo7 天前
apache-jmeter-5.1.1安装部署与使用教程(小白一看就会)
jmeter·apache
文人sec9 天前
性能测试-jmeter7-元件提取器
python·jmeter·prometheus·模块测试
坚定信念,勇往无前10 天前
Jmeter元件的加载顺序
jmeter
明月与玄武14 天前
Docker化性能监控平台搭建:JMeter+InfluxDB+Grafana全攻略
jmeter·docker·grafana