jmeter添加自定義擴(kuò)展函數(shù)之圖片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. 新建一個(gè)包c(diǎn)om.mytest.functions,包名要包含functions,因?yàn)閖meter.properties對(duì)這塊有配置,可見該文件的classfinder.functions.contain=.functions.
2. 在該包下新建一個(gè)類并繼承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("圖片路徑");
}
desc.add("圖片base64后存放變量");
private static final String KEY = "__MyBase64";
//存放傳入?yún)?shù)的值的變量
private Object[] values;
//描述參數(shù)
public List<String> getArgumentDesc() {
// TODO Auto-generated method stub
return desc;
@Override
//函數(shù)的執(zhí)行
public synchronized 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 getReferenceKey() {
//提供jmeter函數(shù)助手顯示的名稱
return KEY;
public synchronized void setParameters(Collection<CompoundVariable> arg0) throws InvalidVariableException {
//檢查參數(shù)的個(gè)數(shù),支持的方法有2個(gè),具體用法參加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
* */
//-----------------
*
int min,
int max)
min - minimum number of parameters allowed
max - maximum number of parameters allowed
//checkParameterCount(arg0, 1);
checkParameterCount(arg0, 1, 2);
//將參數(shù)值存入變量中
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. 由于我寫的類沒有依賴第三方j(luò)ar包,引入的jmeter核心包都是jmeter自帶的,所以直接導(dǎo)出上面的類為一個(gè)jar包,并把這個(gè)jar放在jmeter安裝目錄的apache-jmeter-3.2\lib\ext下面

4. 重啟jmeter,打開函數(shù)助手,可看見如下圖:

5. 下面我們測(cè)試一下這個(gè)函數(shù)是否能使用,新建一個(gè)http請(qǐng)求,在post請(qǐng)求里分別添加${__MyBase64(D:\\aa.jpg,imgresult)}和${imgresult}如下圖,注意${__MyBase64(D:\\aa.jpg,imgresult)}一定要在上面

6. 運(yùn)行后可以看到已經(jīng)成功

到此這篇關(guān)于jmeter添加自定義擴(kuò)展函數(shù)之圖片base64編碼示例詳解的文章就介紹到這了,更多相關(guān)jmeter 圖片base64編碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringAOP中的切點(diǎn)表達(dá)式Pointcut詳解
這篇文章主要介紹了SpringAOP中的切點(diǎn)表達(dá)式Pointcut詳解,Spring?的?AOP?中的一個(gè)核心概念是切點(diǎn)(Pointcut),切點(diǎn)表達(dá)式定義通知(Advice)執(zhí)行的范圍,需要的朋友可以參考下2023-08-08
SpringBoot結(jié)合JWT登錄權(quán)限控制的實(shí)現(xiàn)
本文主要介紹了SpringBoot結(jié)合JWT登錄權(quán)限控制的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Springboot hibernate envers使用過程詳解
這篇文章主要介紹了Springboot hibernate envers使用過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
RestTemplate實(shí)現(xiàn)發(fā)送帶headers的GET請(qǐng)求
這篇文章主要介紹了RestTemplate實(shí)現(xiàn)發(fā)送帶headers的GET請(qǐng)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
hibernate 配置數(shù)據(jù)庫方言的實(shí)現(xiàn)方法
這篇文章主要介紹了hibernate 配置數(shù)據(jù)庫方言的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
java實(shí)現(xiàn)斐波那契數(shù)列的3種方法
這篇文章主要介紹了java實(shí)現(xiàn)斐波那契數(shù)列的3種方法,有需要的朋友可以參考一下2014-01-01

