Jmeter扩展---自定义取样器

时间:2024-04-14 16:06:02

简介

Jmeter已经内置了各种协议的取样器,已经能满足常用的性能压测需求。且在前面一章Jmeter扩展开发--自定义java取样器-****博客中也有关于Java取样器的扩展开发,不过有时候我们期望能定制自己的取样器和界面。为此,需要对Jmeter做扩展,自定义取样器开发

依赖&打包

基本的依赖和打包可以查看前一章Jmeter扩展开发--自定义java取样器-****博客

效果

自定义取样器的使用效果如下图所示:

如上图,成功编写自定义取样器之后,打包,将相关jar包放置在:Jmeter安装目录/lib/ext目录,重启Jmeter即可,当选择某个自定义插件之后,实例界面如下:

核心代码简介

在自定义取样器开发中有两个类需要重点介绍

org.apache.jmeter.samplers.gui.AbstractSamplerGui  

该类是自定义Jmeter取样器的ui显示类,可以直接像操作Swing一样编辑Jmeter取样器的ui界面,如上图2所示。该类包含如下重要方法介绍如下:

getStaticLabel

自定义取样器在Jmeter界面上显示的名字如下图:

示例代码如下:

public String getStaticLabel() {
        return "YC http请求";
    }
getLabelResource

同上,取样器名称的本地化方法,返回一样即可(其实上层组件调用的是getStaticLabel方法获取取样器名称,如果没有国际化的处理,直接放回相同即可)

public String getLabelResource() {
        return "YC http请求";
    }
createTestElement

该方法会创建一个AbstractSampler对象返回即可,必须对该对象中的字段做赋值的动作一般的即是一个ui字段对应一个AbstractSampler字段即可,示例代码如下:

public TestElement createTestElement() {
        YcSample ycSample = new YcSample();
        ycSample.setUserName(this.userNameField.getText());
        ycSample.setAddress(this.addressField.getText());
        ycSample.setAge(this.ageField.getText());
        ycSample.setScore(this.scoreField.getText());
        return ycSample;
    }
modifyTestElement

该方法是将UI界面上配置的信息,传递给执行测试Samper对象完成测试,其实就是将界面上传递的参数传递给sampler,完成数据传递的过程,一般的,通过ui界面的字段取值,然后设置到sampler对应的字段值上去即可,实例代码如下:

public void modifyTestElement(TestElement element) {
        YcSample ycSample = (YcSample)element;
        ycSample.setUserName(this.userNameField.getText());
        ycSample.setAddress(this.addressField.getText());
        ycSample.setAge(this.ageField.getText());
        ycSample.setScore(this.scoreField.getText());
    }
构造器

ui的核心方法是构造器的,在构造器中可以直接使用Swing的方法构建ui界面即可,示例方法如下:

public YcHttpGui() {
        super(); // 此处一定要先调用父类构造器
        setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);

        JPanel mainPanel = new VerticalPanel();
        add(mainPanel, BorderLayout.CENTER);


        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(2,3));

        jPanel.add(userNameField);
        jPanel.add(addressField);
        jPanel.add(ageField);
        jPanel.add(scoreField);

        VerticalPanel verticalPanel = new VerticalPanel();
        verticalPanel.add(jPanel);

        TitledBorder border = BorderFactory.createTitledBorder("用户信息");
        verticalPanel.setBorder(border);

        mainPanel.add(verticalPanel);
    }

以上基本是一些ui定式,如果还需要其他的ui元素,添加即可

org.apache.jmeter.samplers.AbstractSampler

该类是自定义Jmeter取样器的逻辑运行类,核心方法之后一个sample,说明如下
sample

该方法是自定义取样器的核心逻辑运行方法,只需要返回一个SampleResult对象即可(此处和扩展java取样器逻辑一样),示例代码如下:

public SampleResult sample(Entry e) {
        SampleResult result = new SampleResult();
        result.sampleStart();
        log.info("userName is:{}",this.userName);
        System.out.println("运行时名称是:" + this.userName);
        result.sampleEnd();
        result.setSuccessful(true);
        result.setSamplerData("this is test!");
        result.setResponseCode("200");
        return result;
    }

完整代码

自定义Jmeter取样器UI完整代码如下:

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class YcHttpGui extends AbstractSamplerGui {

    private JLabeledTextField userNameField = new JLabeledTextField("用户名");
    private JLabeledTextField addressField = new JLabeledTextField("地址");
    private JLabeledTextField ageField = new JLabeledTextField("年龄");
    private JLabeledTextField scoreField = new JLabeledTextField("score");

    public YcHttpGui() {
        super(); // 此处一定要先调用父类构造器
        setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);

        JPanel mainPanel = new VerticalPanel();
        add(mainPanel, BorderLayout.CENTER);


        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(2,3));

        jPanel.add(userNameField);
        jPanel.add(addressField);
        jPanel.add(ageField);
        jPanel.add(scoreField);

        VerticalPanel verticalPanel = new VerticalPanel();
        verticalPanel.add(jPanel);

        TitledBorder border = BorderFactory.createTitledBorder("用户信息");
        verticalPanel.setBorder(border);

        mainPanel.add(verticalPanel);
    }

    @Override
    public String getStaticLabel() {
        return "YC http请求";
    }

    @Override
    public String getLabelResource() {
        return "YC http请求";
    }

    @Override
    public TestElement createTestElement() {
        YcSample ycSample = new YcSample();
        ycSample.setUserName(this.userNameField.getText());
        ycSample.setAddress(this.addressField.getText());
        ycSample.setAge(this.ageField.getText());
        ycSample.setScore(this.scoreField.getText());
        return ycSample;
    }

    @Override
    public void modifyTestElement(TestElement element) {
        YcSample ycSample = (YcSample)element;
        ycSample.setUserName(this.userNameField.getText());
        ycSample.setAddress(this.addressField.getText());
        ycSample.setAge(this.ageField.getText());
        ycSample.setScore(this.scoreField.getText());
    }

}

自定义Jmeter取样器sampler完整代码如下:

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

@Data
@Slf4j
public class YcSample extends AbstractSampler {

    private String userName;
    private String address;
    private String age;
    private String score;

    @Override
    public SampleResult sample(Entry e) {
        SampleResult result = new SampleResult();
        result.sampleStart();
        log.info("userName is:{}",this.userName);
        System.out.println("运行时名称是:" + this.userName);
        result.sampleEnd();
        result.setSuccessful(true);
        result.setSamplerData("this is test!");
        result.setResponseCode("200");
        return result;
    }
}

后续会继续分享关于Jmeter自定义取样器的源码分析,届时会把相关逻辑梳理清楚