jmeter添加自定義擴展函數(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. 新建一個包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("圖片路徑");
}
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ù)的個數(shù),支持的方法有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
* */
//-----------------
*
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. 由于我寫的類沒有依賴第三方jar包,引入的jmeter核心包都是jmeter自帶的,所以直接導出上面的類為一個jar包,并把這個jar放在jmeter安裝目錄的apache-jmeter-3.2\lib\ext下面

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

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

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

到此這篇關于jmeter添加自定義擴展函數(shù)之圖片base64編碼示例詳解的文章就介紹到這了,更多相關jmeter 圖片base64編碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot結合JWT登錄權限控制的實現(xiàn)
本文主要介紹了SpringBoot結合JWT登錄權限控制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
Springboot hibernate envers使用過程詳解
這篇文章主要介紹了Springboot hibernate envers使用過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
RestTemplate實現(xiàn)發(fā)送帶headers的GET請求
這篇文章主要介紹了RestTemplate實現(xiàn)發(fā)送帶headers的GET請求,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
hibernate 配置數(shù)據(jù)庫方言的實現(xiàn)方法
這篇文章主要介紹了hibernate 配置數(shù)據(jù)庫方言的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05

