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

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

相关推荐
hgz07101 天前
Nginx负载均衡策略详解与Session一致性解决方案
java·jmeter
write19941 天前
jmeter环境配置
jmeter
trayvontang1 天前
Jmeter脚本原理与实例
jmeter
hgz07102 天前
企业级Nginx反向代理与负载均衡实战
java·jmeter
无名小卒Rain2 天前
Jmeter性能测试-用JSON表达式获取关联参数
jmeter·压力测试·性能测试
无名小卒Rain2 天前
Jmeter性能测试-用正则表达式取关联参数:单个正则模版和多个正则模版
jmeter·压力测试·性能测试
write19944 天前
02 jmeter常用组件
jmeter
天才测试猿4 天前
Jmeter 命令行压测&生成HTML测试报告
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·jenkins
帝落若烟4 天前
jmeter下载安装-1
jmeter
古城小栈4 天前
性能测试:JMeter 压测 Spring Boot 微服务
spring boot·jmeter·微服务