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

网友投稿 814 2022-11-02

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

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

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

org.apache.jmeter

ApacheJMeter_core

3.2

org.apache.jmeter

ApacheJMeter_functions

3.2

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 desc = new LinkedList();

static {

desc.add("图片路径");

}

desc.add("图片base64后存放变量");

private static final String KEY = "__MyBase64";

//存放传入参数的值的变量

private Object[] values;

//描述参数

public List getArgumentDesc() {

// TODO Auto-generated method stub

return desc;

@Override

//函数的执行

public synchronizDVWaYnPed String execute(SampleResult arg0, Sampler arg1) throws InvalidVariableException {

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;

public String getRefereDVWaYnPnceKey() {

//提供jmeter函数助手显示的名称

return KEY;

public synchronized void setParameters(Collection arg0) throws InvalidVariableException {

//检查参数的个数,支持的方法有2个,具体用法参加api:

/**

* protected void checkParameterCount(Collection parameters,

int count)

throws InvalidVariableException

Utility method to check parameter counts.

Parameters:

parameters - collection of parameters

count - number of parameters expected

* */

//-----------------

*

int min,

int max)

min - minimum number of parameters allowed

max - maximum number of parameters allowed

//checkParameterCount(arg0, 1);

checkParameterCount(arg0, 1, 2);

http:// //将参数值存入变量中

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. 运行后可以看到已经成功

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:40 - xml文档与字典之间的互相转换
下一篇:46 - 将xml文档保存在MongoDB数据库中
相关文章

 发表评论

暂时没有评论,来抢沙发吧~