Jmeter (15) - From entry to mastery - JMeter imports custom Jar package (detailed tutorial)

content

1 Introduction

2. Environmental preparation

3. Specific ideas

4. Realization of ideas

5.JMeter introduces custom Jar package

6. Summary


1 Introduction

  The original plan was to introduce the basic knowledge of the pre-processor. As a result, many friends or children’s shoes left messages in WeChat and blog gardens to ask how to introduce their own defined Jar packages? ? ? I replied to them one by one and told them the same reason as the Jar package that introduced the plug-in, everything was perfect. But they feel that they are still confused and confused, so here is an article about importing a custom Jar package. There is another reason that the preprocessor will use this custom Jar package.

2. Environmental preparation

(1)Eclipse

  We want to introduce a custom Jar package, so you need a tool that can write a script to generate Jar, of course, you can choose other development tools, Hongge choose Eclipse here.

(2) JMeter

  Not to mention JMeter, today's pig's feet is her.

3. Specific ideas

1. Develop the script

2. Export the script to a Jar package

3. JMeter introduces Jar package

4. Realization of ideas

1. The development script is of course on Eclipse. First, we develop a simple interface.

4.1 Code Implementation

 4.2 Reference code

package com.bjhg.test;

/**
 * @author 北京-宏哥
 *
 * 2020年7月2日
 */
public interface BeanJMeterUtil {
     //获取名字方法
     public String getUserName();
     //获取地址方法
     public String getAddress();
     
}

2. Develop a class that implements the interface

4.3 Code Implementation

 4.4 Reference code

package com.bjhg.test;

/**
 * @author 北京-宏哥
 *
 * 2020年7月2日
 */
public class BeanShellJMeter implements BeanJMeterUtil{
    //定义变量名字
    private String userName;
    //定义变量地址
    private String address;
 
     
    public BeanShellJMeter(String name)
    {
        this.userName = name;
    }
     
    public BeanShellJMeter(String name,String address){
        this.userName = name;
        this.address = address;
    }
     
    public String getUserName()
    {
        return userName;
    }
     
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
     
    public String getAddress()
    {
        return address;
    }
     
    public void setAddress(String address)
    {
        this.address = address;
    }
}

3. Test whether the interface is implemented. Create a test class test, instantiate the implementation class BeanShellJMeter of the interface, call the method in this class and output the result.

4.5 Code Implementation

 4.6 Reference code

package com.bjhg.test;

/**
 * @author 北京-宏哥
 *
 * 2020年7月2日
 */
public class test {
    public static void main(String[] args) {
        // 创建实现类的对象
        BeanShellJMeter bjhg = new BeanShellJMeter("宏哥","北京");

        System.out.println("Hello!我是:"+ bjhg.getAddress()+bjhg.getUserName());
    }
}

4.7 Running results

 4. Export the script to a Jar package

(1) Click File, and then continue to click "Export", as shown below:

 (2) Select "JAR file", and then click "next" as shown below:

 (3) Select the project for which the script was just written, and select the location to export the Jar package (Brother Hong directly imports the location of the Jmeter jar package here), as shown in the following figure:

 (4) Click "Finish", as shown below:

 (5) View the exported Jar package, as shown in the following figure:

 

5.JMeter introduces custom Jar package

1. Create a new test plan and import the custom Jar package, as shown in the following figure:

 

2. Add the BeanShell preprocessor under the thread group ( Parameters put 2 parameters in Hongge Beijing ), as shown in the following figure:

 

 

3. Script reference code:

import com.bjhg.test.BeanShellJMeter;


BeanShellJMeter bs = new BeanShellJMeter(bsh.args[0],bsh.args[1]);

vars.put("address",bs.getAddress());
vars.put("username",bs.getUserName());

copy

4. Then add a debug sampler, as shown below:

 

5. After the configuration is complete, click "Save", run JMeter, and view the table results (put the read data into username and adress), as shown in the following figure:

6. Summary

  Well, today I will share and explain the import of custom Jar packages in JMeter, and I hope it will be helpful to everyone.

Guess you like

Origin blog.csdn.net/ZangKang1/article/details/124295298