Jmeter(7)----开发自定义函数

注意:包名不能为Jmeter,天坑!!!

1、POM文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Plguin.Jmeter</groupId>
	<artifactId>ImageBase64</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core -->
	<dependencies>
		<dependency>
			<groupId>org.apache.jmeter</groupId>
			<artifactId>ApacheJMeter_core</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.jmeter</groupId>
			<artifactId>ApacheJMeter_java</artifactId>
			<version>3.0</version>
		</dependency>
	</dependencies>
</project>
2、自定义函数:引入Image图片,进行BASE64加密

package org.apache.functions;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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 ImageBase64 extends AbstractFunction {
	//显示的参数名字
	private static final List<String> desc = new LinkedList();
	//显示的函数名字
	private static final String KEY = "__ImageBase64";
	//参数值
	private Object[] values;
	
	static {
		desc.add("String to calculate Base64 hash");
		desc.add("Name of variable in which to store the result (optional)");
	}
	
	public  String execute(SampleResult paramSampleResult, Sampler paramSampler)
			throws InvalidVariableException {
		StringBuffer stBuffer = new StringBuffer();
		try {
			String url = ((CompoundVariable)this.values[0]).execute();
<span style="white-space:pre">			</span>url = new String(url.getBytes("UTF-8"),"UTF-8");
<span style="white-space:pre">			</span>InputStream input = new FileInputStream(url);
			byte[] by = new byte[3072];
			int status = -1;
			while((status = input.read(by))>-1)
			{
				stBuffer.append(new BASE64Encoder().encode(by));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stBuffer.toString();
	}

	//获取参数值
	public synchronized void setParameters(Collection<CompoundVariable> paramCollection)
			throws InvalidVariableException {
		//检查参数,我也搞不懂是毛线意思,NND
		checkMinParameterCount(paramCollection, 1);
		this.values = paramCollection.toArray();
	}

	//返回函数名字
	public String getReferenceKey() {
		return KEY;
	}
	//返回参数名字
	public List<String> getArgumentDesc() {
		return desc;
	}

}

3、自定义函数:加载image图片,保存byte 10进制数组

package org.apache.functions;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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;

public class ImageByte extends AbstractFunction {
	private static final List<String> desc = new LinkedList();
	private static final String KEY = "__ImageByte";
	private Object[] values;

	static {
		desc.add("String to calculate Base64 hash");
		desc.add("Name of variable in which to store the result (optional)");
	}

	public String execute(SampleResult paramSampleResult, Sampler paramSampler) throws InvalidVariableException {
		StringBuffer stBuffer = new StringBuffer();
		try {
			String url = ((CompoundVariable)this.values[0]).execute();
<span style="white-space:pre">			</span>url = new String(url.getBytes("UTF-8"),"UTF-8");
<span style="white-space:pre">			</span>InputStream input = new FileInputStream(url);
			byte[] by = new byte[3072];
			int num = -1;
			stBuffer.append("[");
			while ((num = input.read()) > -1) {
				stBuffer.append(num + ",");
			}
			stBuffer.deleteCharAt(stBuffer.lastIndexOf(","));
			stBuffer.append("]");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stBuffer.toString();
	}

	public synchronized void setParameters(Collection<CompoundVariable> paramCollection)
			throws InvalidVariableException {
		checkMinParameterCount(paramCollection, 1);
		this.values = paramCollection.toArray();
	}

	public String getReferenceKey() {
		return KEY;
	}

	public List<String> getArgumentDesc() {
		return desc;
	}

}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值