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

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

相关推荐
爆更小哇3 天前
jmeter插件下载+创建梯度线程组+命令生成性能测试报告
测试工具·jmeter·自动化
抓到小松鼠4 天前
jmeter-事务控制器
jmeter
张永清-老清4 天前
每周读书与学习->Jmeter中如何使用Bean Shell脚本(三)Bean Shell的基础语法之运算符和控制流语句
学习·jmeter·性能优化·性能测试·jmeter性能测试·beanshell·每周读书与学习
张永清5 天前
每周读书与学习->Jmeter中如何使用Bean Shell脚本(三)Bean Shell的基础语法之运算符和控制流语句
jmeter·性能测试·性能调优·jmeter性能测试·性能分析·性能监控·性能诊断·每周读书与学习·bean shell
半个俗人5 天前
13.jmeter性能测试高并发及高频率弱压力及持续高并发
jmeter
程序员杰哥6 天前
Jmeter正则表达式提取器和JSON提取器基础用法
自动化测试·软件测试·测试工具·jmeter·正则表达式·json·测试用例
半个俗人6 天前
12.jmeter跨越线程组传值-全局变量
jmeter
半个俗人6 天前
14.jmeter图形化报告
jmeter
爆更小哇6 天前
JMeter配置和使用入门指南
java·开发语言·测试工具·jmeter·自动化