当前位置: 首页 > news >正文

jmeter添加自定义扩展函数之图片base64编码

打开eclipse,新建maven工程,在pom中引入jmeter核心jar包:

<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core -->
<dependency>
    <groupId>org.apache.jmeter</groupId>
    <artifactId>ApacheJMeter_core</artifactId>
    <version>3.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_functions -->
<dependency>
    <groupId>org.apache.jmeter</groupId>
    <artifactId>ApacheJMeter_functions</artifactId>
    <version>3.2</version>
</dependency>

 

1. 新建一个包com.mytest.functions,包名要包含functions,因为jmeter.properties对这块有配置,可见该文件的classfinder.functions.contain=.functions.

2. 在该包下新建一个类并继承AbstractFunction,重写该类的所有方法,具体如下:

package com.mytest.functions;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;

import sun.misc.BASE64Encoder;

public class MyBase64 extends AbstractFunction{

    //自定义function的描述
    private static final List<String> desc = new LinkedList<String>();
    static {
        desc.add("图片路径");
    }
    static {
        desc.add("图片base64后存放变量");
    }
    private static final String KEY = "__MyBase64";
   
    //存放传入参数的值的变量
    private Object[] values;
    
   
    //描述参数
    public List<String> getArgumentDesc() {
        // TODO Auto-generated method stub
        return desc;
    }

    @Override
    //函数的执行
    public synchronized String execute(SampleResult arg0, Sampler arg1) throws InvalidVariableException {
        // TODO Auto-generated method stub
        JMeterVariables localJMeterVariables = getVariables();
        String str1 = ((CompoundVariable)this.values[0]).execute();

        String str2 = getImgBase64(str1);

        if ((localJMeterVariables != null) && (this.values.length > 1)) {
          String str3 = ((CompoundVariable)this.values[1]).execute().trim();
          localJMeterVariables.put(str3, str2);
        }

        return str2;
    }

    @Override
    public String getReferenceKey() {
        // TODO Auto-generated method stub
        //提供jmeter函数助手显示的名称
        return KEY;
    }

    @Override
    public synchronized void setParameters(Collection<CompoundVariable> arg0) throws InvalidVariableException {
        // TODO Auto-generated method stub
        //检查参数的个数,支持的方法有2个,具体用法参加api:
        /**
         * protected void checkParameterCount(Collection<CompoundVariable> parameters,
                                   int count)
                            throws InvalidVariableException
            Utility method to check parameter counts.
            Parameters:
            parameters - collection of parameters
            count - number of parameters expected
         * */
        //-----------------
        /**
         * 
         * protected void checkParameterCount(Collection<CompoundVariable> parameters,
                                   int min,
                                   int max)
                            throws InvalidVariableException
            Utility method to check parameter counts.
            Parameters:
            parameters - collection of parameters
            min - minimum number of parameters allowed
            max - maximum number of parameters allowed
         * */
        //checkParameterCount(arg0, 1);
        checkParameterCount(arg0, 1, 2);
        //将参数值存入变量中
        this.values = arg0.toArray();

    }
    public String getImgBase64(String filePath) {
        InputStream in = null;
        byte[] data = null;
        String result = null;
        try {
            in = new FileInputStream(filePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();

            BASE64Encoder encoder = new BASE64Encoder();
            result = encoder.encode(data);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
        
        return result;
    }

}

3. 由于我写的类没有依赖第三方jar包,引入的jmeter核心包都是jmeter自带的,所以直接导出上面的类为一个jar包,并把这个jar放在jmeter安装目录的apache-jmeter-3.2\lib\ext下面

4. 重启jmeter,打开函数助手,可看见如下图:

5. 下面我们测试一下这个函数是否能使用,新建一个http请求,在post请求里分别添加${__MyBase64(D:\\aa.jpg,imgresult)}和${imgresult}如下图,注意${__MyBase64(D:\\aa.jpg,imgresult)}一定要在上面

6. 运行后可以看到已经成功

相关文章:

  • Vue 2.3、2.4 知识点小结
  • 颜色模式
  • 自作聪明的开发
  • 记录一次MySQL进程崩溃,无法重启故障排查
  • 我的IntelliJ IDEA 设置
  • shell自定义函数
  • SVN:show log问题
  • monaco editor + vue的配置
  • Centos7配置ubuntu本地仓库
  • jQuery 选择器
  • IP unnumbered interface,某个接口不编号,某个接口不分配IP地址
  • struts2中怎样处理404?
  • 一张图让你学会LVM
  • WiFi替代者LiFi首次实测结果出炉:1Gbps
  • 解读:四点让云计算引发全球变革
  • JavaScript-如何实现克隆(clone)函数
  • [Vue CLI 3] 配置解析之 css.extract
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • IDEA常用插件整理
  • Intervention/image 图片处理扩展包的安装和使用
  • Java 23种设计模式 之单例模式 7种实现方式
  • Java教程_软件开发基础
  • Odoo domain写法及运用
  • React Native移动开发实战-3-实现页面间的数据传递
  • spring-boot List转Page
  • Swoft 源码剖析 - 代码自动更新机制
  • windows下如何用phpstorm同步测试服务器
  • 电商搜索引擎的架构设计和性能优化
  • 前端性能优化——回流与重绘
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 小而合理的前端理论:rscss和rsjs
  • 浅谈sql中的in与not in,exists与not exists的区别
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • 说说我为什么看好Spring Cloud Alibaba
  • 昨天1024程序员节,我故意写了个死循环~
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • !!Dom4j 学习笔记
  • #include<初见C语言之指针(5)>
  • $ git push -u origin master 推送到远程库出错
  • (7)STL算法之交换赋值
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (待修改)PyG安装步骤
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (转)c++ std::pair 与 std::make
  • (转)ORM
  • .FileZilla的使用和主动模式被动模式介绍
  • .net6使用Sejil可视化日志
  • .NET成年了,然后呢?
  • .NET导入Excel数据
  • .net反编译工具
  • .net流程开发平台的一些难点(1)