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这个函数在列。

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

相关推荐
banyoyo7 天前
jmeter测试使用方法
jmeter
汪汪8888888 天前
jmeter压测下载功能影响本地内存的方案
jmeter
跨境数据猎手8 天前
跨境电商系统JMeter 和 Gatling 分工
jmeter
han688910 天前
jmeter操作数据库
数据库·jmeter
川石课堂软件测试12 天前
涨薪技术|0到1学会性能测试第8课-Analysis分析图
服务器·数据库·python·sql·jmeter·mysql·容器
川石课堂软件测试12 天前
JMeter异步接口测试实战
数据库·python·jmeter·mysql·kubernetes·测试用例·prometheus
zuozewei14 天前
第02篇-k6-脚本结构与生命周期
软件测试·jmeter·性能测试·k6·ai 编程
fen_fen14 天前
Jmeter5.1.1连接oracle数据库的错误复盘
数据库·jmeter·oracle
川石课堂软件测试18 天前
自动化测试|持续集成Git使用详解
git·python·jmeter·mysql·ci/cd·容器·kubernetes
wuhuhuan20 天前
JMeter 连接数据库并取值(JDBC 实战)
数据库·jmeter