Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add a self-defined extension function to realize base64 coding of pictures in jmeter

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The knowledge of this article "jmeter how to add a custom extension function to achieve picture base64 coding" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "jmeter how to add a custom extension function to achieve picture base64 coding" article.

Open eclipse, create a new maven project, and introduce the jmeter core jar package into pom:

Org.apache.jmeter ApacheJMeter_core 3.2 org.apache.jmeter ApacheJMeter_functions 3.2

1. Create a new package com.mytest.functions, and the package name should contain functions, because jmeter.properties is configured for this block, so you can see the classfinder.functions.contain=.functions of the file.

two。 Create a new class under the package and inherit AbstractFunction, overriding all the methods of the class, as follows:

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 Description of public class MyBase64 extends AbstractFunction {/ / Custom function private static final List desc = new LinkedList (); static {desc.add ("picture path");} desc.add ("variable after picture base64"); private static final String KEY = "_ _ MyBase64"; / / variable private Object [] values that holds the value of the passed parameter / / description parameter public List getArgumentDesc () {/ / TODO Auto-generated method stub return desc; @ Override / / 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 () {/ / provide the name return KEY displayed by the jmeter function helper Public synchronized void setParameters (Collection arg0) throws InvalidVariableException {/ / check the number of parameters. There are two methods supported. For specific usage, please see 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); / / store parameter values in variables 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. Since the class I wrote does not rely on third-party jar packages, the introduced jmeter core packages are all included with jmeter, so directly export the above class to a jar package and put the jar under apache-jmeter-3.2\ lib\ ext in the jmeter installation directory.

4. Restart jmeter, open the function helper, and you can see the following figure:

5. Let's test whether this function can be used. Create a new http request and add ${_ MyBase64 (D:\\ aa.jpg,imgresult)} and ${imgresult} to the post request as shown below. Note that ${_ MyBase64 (D:\\ aa.jpg,imgresult)} must be on it.

6. After running it, you can see that it has been successful.

The above is about "jmeter how to add a custom extension function to achieve picture base64 coding" this article content, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report