jmeter函数扩展--图片转Base64码

------------恢复内容开始------------

致谢参考博客:

https://www.cnblogs.com/qiaoyeye/p/7218770.html

https://www.cnblogs.com/lasdaybg/p/9803988.html

https://www.cnblogs.com/jyiqing/p/10256178.html

http://www.manongjc.com/article/59838.html

一、环境

jmeter5.2.1

jdk1.8

eclipse

二、具体步骤

1、打开eclipse建立maven工程

2、修改  pom.xml  文件

修改完  pom.xml  文件后,等一会儿(正在下载  jmeter  的相关东西)

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

3、新建  com.mytest.functions  包

4、新建  MyBase64  类(jmeter 函数名为  __Base64Img)

我这里和原作者不同,用的是  java.util.Base64  的包(具体原因看第二第三条博客)

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 java.util.Base64;

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 = "__Base64Img";
   
    //存放传入参数的值的变量
    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);
            result = Base64.getEncoder().encodeToString(data);
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
        
        return result;
    }

}

5、把  MyBase64  类导出为  jar  包至  apache-jmeter-5.2.1\lib\ext  目录下

 

6、打开 jmeter5.2.1  去试一试 函数能不能用

6、注意事项

有些接口可能需要  data:img/jpg;base64,  把部分添加上去才能正确读取  Base64  码

 那就在  jmeter  参数填写的时候

 

好,完成。

猜你喜欢

转载自www.cnblogs.com/mua9012/p/11992334.html
今日推荐