当前位置:AIGC资讯 > 数据采集 > 正文

 jmeter 自定义函数和Sampler(采集器)

第一个插件自定义函数(Function)

代码具体如下,生成 jar 放到 lib\ext 目录下即可。


/*代码路径中,一定要放在 functions 目录中,否则 jmeter 会识别自定义函数失败*/
/*http://blog.csdn.net/xreztento/article/details/48622019*/
package interfacePerformance.functions;
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;
public class double2 extends AbstractFunction {
// private static final Logger log = LoggingManager.getLoggerForClass();
private static final List<String> desc = new LinkedList<String>();
/* KEY 要和类名一致,否则无法识别自定义函数 */
private static final String KEY = "__double2";
private Object[] values = null;
static {
desc.add("double2_value");
} 
//描述参数
@Override
public List<String> getArgumentDesc() {
// TODO Auto-generated method stub
return desc;
} 
//函数执行,返回结果
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws
InvalidVariableException {
// TODO Auto-generated method stub
String numberString = ((CompoundVariable) values[0]).execute().trim();
int num;
try {num = Integer.valueOf(numberString);
} catch (Exception e) {
return null;
} 
/* 默认返回 String 类型 */
/* num*2 为业务代码 */
return String.valueOf(num * 2);
} 
//获取函数引用关键字
@Override
public String getReferenceKey() {
// TODO Auto-generated method stub
return KEY;
} 
//设置参数
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws
InvalidVariableException {
// TODO Auto-generated method stub
// 可以检查参数数量,主要包括以下两种方法
checkMinParameterCount(parameters, 1);
checkParameterCount(parameters, 1, 1);
values = parameters.toArray();
}
}

第二个插件采集器(Sampler)

TestSampler.java

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

/**
 * @author nikey
 * @date 2020/3/30
 */
public class TestSampler extends AbstractSampler {
    public final static String FUNCTION = "function";

    @Override
    public SampleResult sample(Entry entry) {
        SampleResult result = new SampleResult();
        result.sampleStart();
        // 输出GUI界面所输入的函数方法返回结果
        System.out.println(this.getProperty(FUNCTION));
        result.sampleEnd();
        result.setSuccessful(true);
        return result;
    }
}

TestSamplerGUI.java

import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;

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

/**
 * @author nikey
 * @date 2020/3/30
 */
public class TestSamplerGUI extends AbstractSamplerGui {
    private JTextField functionTextField = null;
    public TestSamplerGUI(){
        init();
    }

    @Override
    public void configure(TestElement element) {
        super.configure(element);
        functionTextField.setText(element.getPropertyAsString(TestSampler.FUNCTION));
    }

    private void init() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        functionTextField = new JTextField(20);
        mainPanel.add(functionTextField);
        add(mainPanel);
    }

    @Override
    public TestElement createTestElement() {
        // 创建所对应的Sampler
        TestElement sampler = new TestSampler();
        modifyTestElement(sampler);
        return sampler;
    }

    @Override
    public String getLabelResource() {
        return this.getClass().getSimpleName();
    }

    @Override
    public void modifyTestElement(TestElement sampler) {
        super.configureTestElement(sampler);
        if(sampler instanceof TestSampler){
            TestSampler testSampler = (TestSampler) sampler;
            testSampler.setProperty(TestSampler.FUNCTION,functionTextField.getText());
        }
    }

    @Override
    public String getStaticLabel() {
        // 设置显示名称
        return "TestSampler";
    }

    private void initFields(){
        functionTextField.setText("");
    }

    @Override
    public void clearGui() {
        super.clearGui();
        initFields();
    }
}

更新时间 2023-11-08