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對這塊有配置,可見該文件的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. 下面我們測試一下這個(gè)函數(shù)是否能使用,新建一個(gè)http請求,在post請求里分別添加${__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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
@Scheduled fixedDelayString 加載properties配置方式
這篇文章主要介紹了@Scheduled fixedDelayString 加載properties配置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10SVN出現(xiàn)提示org.apache.subversion.javahl.ClientException: Attempt
這篇文章主要介紹了SVN出現(xiàn)提示org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12jedis獲取redis中二進(jìn)制圖片轉(zhuǎn)Base64方式
這篇文章主要介紹了jedis獲取redis中二進(jìn)制圖片轉(zhuǎn)Base64方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07java實(shí)現(xiàn)分段讀取文件并通過HTTP上傳的方法
這篇文章主要介紹了java實(shí)現(xiàn)分段讀取文件并通過HTTP上傳的方法,實(shí)例分析了java分段讀取文件及使用http實(shí)現(xiàn)文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解
這篇文章主要介紹了spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09spring+srpingmvc+hibernate實(shí)現(xiàn)動態(tài)ztree生成樹狀圖效果
這篇文章主要介紹了spring+srpingmvc+hibernate動態(tài)ztree生成樹狀圖效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11詳解Java中ExceptionInInitializer錯誤的解決方法
ExceptionInInitializerError 是 Java 中的未經(jīng)檢查的異常,它是 Error 類的子類, 它屬于運(yùn)行時(shí)異常的類別,下面我們就來看看它的具體解決方法吧2024-02-02@PathVariable、@RequestParam和@RequestBody的區(qū)別
本文主要介紹了@PathVariable、@RequestParam和@RequestBody的區(qū)別和使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Java實(shí)現(xiàn)橋接方法isBridge()和合成方法isSynthetic()
本文主要介紹了Java實(shí)現(xiàn)橋接方法isBridge()和合成方法isSynthetic(),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06