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

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

相关推荐
se-tester4 天前
JMeter、Postman 和 SoapUI 在做接口测试上的优势和缺点
测试工具·jmeter·接口测试·postman·soapui
feathered-feathered5 天前
测试实战【用例设计】自己写的项目+功能测试(1)
java·服务器·后端·功能测试·jmeter·单元测试·压力测试
质量保障小乔10 天前
JMeter+Jenkins+AI自动化测试
jmeter
装不满的克莱因瓶12 天前
Java高并发异步请求实战,Jmeter暴力压测下的解决方案
jmeter·线程池·多线程·并发·resttemplate·qps·压测
在坚持一下我可没意见17 天前
ideaPool论坛系统测试报告
java·spring boot·功能测试·selenium·jmeter·mybatis·压力测试
linweidong20 天前
别让老板等:千人并发下的实时大屏极致性能优化实录
jmeter·clickhouse·性能优化·sentinel·doris·物化视图·离线数仓
CesareCheung21 天前
JMeter核心组件使用方法及场景大全
jmeter
我会一直在的21 天前
Jmeter的基础使用
功能测试·jmeter·压力测试
choke23322 天前
软件性能测试实战:从基础概念到 JMeter 全组件使用
jmeter
网络安全-杰克23 天前
Jmeter压力测试工具安装与使用
自动化测试·软件测试·测试工具·jmeter·职场和发展