使用TestBean框架开发一个简单的sampler插件

来源:互联网 发布:linux修改用户家目录 编辑:程序博客网 时间:2024/05/19 03:46

使用TestBean框架开发组件的步骤

创建三个文件

[Component].java :必须实现TestBean接口,可以通过拓展不能抽象类(为了更加容易实现)或者对应接口来指定此组件元素的类型;比如:AbstractSampler,AbstractSampler, AbstractVisualizer, GenericController,ConfigTestElement,etc;
[Component]BeanInfo.java 指定配置元件需要输入属性.这些都会显示在jmeter的窗口;必须继承BeanInfoSupport,必须创建一个Public无参构造函数并在此处调用super([Component].class)

[Component]Resources.properties:定义GUI中使用字符串资源.


在[Component].java 类中实现逻辑代码

注意类中的属性必须拥public权限的getter和setter方法,属性是可以通过GUI中的配置的,这个类在每个线程开始之前都会克隆一份副本

在[Component]BeanInfo.java创建属性组

如:createPropertyGroup("csv_data",new String[]{"filename","variableNames"});然后定义属性

Key Value

Description of Value

NOT_UNDEFINED

The property will not be left null if true.

DEFAULT

A default values must be given if NOT_UNDEFINED is true.

NOT_EXPRESSION

The value will not be parsed for functions if this is true.

NOT_OTHER

This is not a free form entry field – a list of values has to be provided

TAGS

With a String[] as the value, this sets up a predefined list of acceptable values, and JMeter will create a dropdown select.

TableEditor.CLASSNAME

The name of the Class that will represent each row of a GUI Table (only valid if you have set the property's editor class to TableEditor via p.setPropertyEditorClass(TableEditor.class);

TableEditor.HEADERS

A String[] array that holds the header labels for the table, if using TableEditor

TableEditor.OBJECT_PROPERTIES

A String[] array that holds the names of the properties you wish to display (and make editable) for each object in the table (ie these will correspond to the columns of the table). If the CLASSNAME is "java.lang.String" then this should be skipped

MULTILINE

If true, then the component for the property will be allowed to grow with the screen space in the window - ideal for TestArea editors, scroll panels, and other more complicated gui elements


createPropertyGroup("MyFuction",new String[] { FuctionName });PropertyDescriptor p = property(FuctionName);p.setValue(NOT_UNDEFINED, true);p.setValue(DEFAULT, "请输入函数");


在[Component]Resources.properties定义资源属性

  • displayName - This will provide a name for the element that will appear in menus.

  • csv_data.displayName - we create a property grouping called "csv_data", so we have to provide a label for the grouping

  • filename.displayName - a label for the filename input element.

  • filename.shortDescription - a tool-tip-like help text blurb.

  • variableNames.displayName - a label for the variable name input element.

  • variableNames.shortDescription - tool tip for the variableNames input element.

displayName=gzy@GetFuctionRestultfuction.displayName=函数名称fuction.shortDescription=请输入需要获取返回值的函数

具体代码实现

目标:开发一个sampler组件,该组件功能是可以在输入框中输入对应函数在日志中打印该函数的返回值

1.创建三个文件

GetFuctionRestult:逻辑实现部分,继承AbstractSampler实现TestBean接口

GetFuctionRestultBeanInfo:UI定义部分,继承GetFuctionRestultBeanInfo

GetFuctionRestultResources.properties:UI定义部分

GetFuctionRestult参考代码:

package com.gzy.GetFuctionResult;import org.apache.jmeter.samplers.AbstractSampler;import org.apache.jmeter.samplers.Entry;import org.apache.jmeter.samplers.SampleResult;import org.apache.jmeter.testbeans.TestBean;import org.apache.jorphan.logging.LoggingManager;import org.apache.log.Logger;/*** * 逻辑实现类,继承AbstractSampler * */public class GetFuctionRestult extends AbstractSampler implements TestBean {//定义UIDprivate static final long serialVersionUID = 240L;//属性,必须有public的get,set方法private String fuction;//获取log对象private static final Logger log = LoggingManager.getLoggerForClass();public String getFuction() {return fuction;}public void setFuction(String fuction) {this.fuction = fuction;}@Overridepublic SampleResult sample(Entry arg0) {//逻辑实现SampleResult res = new SampleResult();res.setSampleLabel(getName());res.sampleStart();log.info("获取函数返回值--->>开始");log.info(fuction);res.setSamplerData(fuction);res.setResponseData(fuction, null);log.info("获取函数返回值--->>结束");res.sampleEnd();res.setSuccessful(true);return res;}}


GetFuctionRestultBeanInfo参考代码

package com.gzy.GetFuctionResult;import java.beans.PropertyDescriptor;import org.apache.jmeter.testbeans.BeanInfoSupport;import org.apache.jmeter.testbeans.TestBean;import org.apache.jmeter.testbeans.gui.FileEditor;/**定义UI展示,继承BeanInfoSupport */public class GetFuctionRestultBeanInfo extends BeanInfoSupport {//定义UID    private static final long serialVersionUID = 240L;    //关联fuction属性private static final String FuctionName = "fuction"; //$NON-NLS-1$//必须提供无参构造方法public GetFuctionRestultBeanInfo() {//关联 GetFuctionRestultsuper(GetFuctionRestult.class);// 创建属性,FuctionName指的是fuction输入框前的描述字符createPropertyGroup("MyFuction", //$NON-NLS-1$new String[] { FuctionName });//设置FuctionName属性PropertyDescriptor p = property(FuctionName);p.setValue(NOT_UNDEFINED, true);p.setValue(DEFAULT, "请输入函数");}}


GetFuctionRestultResources.properties参考代码

displayName=gzy@GetFuctionRestultfuction.displayName=函数名称fuction.shortDescription=请输入需要获取返回值的函数



0 0
原创粉丝点击