本篇将开启为JMeter开发插件之旅,我们选择以Function(函数)组件作为插件开发的入手对象,在前面的章节我们将其划分为非GUI组件,选择它的理由不仅仅是因为Function插件在开发方面是极简的,而且在实际运用JMeter执行测试时,对于Function组件的应用会极大的为你的测试带来便利,有些甚至是必不可少的。
什么是Function组件?
我们还是有必要对Function组件的功能进行一下简单的回顾:
通过打开函数助手,我们可以通过下拉菜单,查看JMeter为我们默认提供的一系列实用的函数功能,使用函数非常简单,比如"__UUID"函数,功能是生成一个uuid,在后面的输入框中只需输入"${__UUID}"便可以引入调用该函数方法所返回的uuid值,所有的组件都可以对函数组件进行引入。
一个用于测试的Sampler插件
在此处我们首先可以开发一个专门用于测试函数或自定义变量输出的Sampler(原来它才是我们所开发的第一个插件),我们叫它TestSampler,开发方式是分离法,分为逻辑控制部分TestSampler和GUI部分TestSamplerGUI:
-
首先在Java工程中引入主要的JMeter插件开发jar包:
- TestSampler类代码参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class TestSampler extends AbstractSampler { public final static String FUNCTION = "function"; @Override public SampleResult sample(Entry entry) { // TODO Auto-generated method stub SampleResult res = new SampleResult(); res.sampleStart(); System.out.println(this.getProperty(FUNCTION));//输出GUI界面所输入的函数方法返回结果 res.sampleEnd(); res.setSuccessful(true); return res; } } |
我们在之前的章节简单介绍了Sampler的主要实现方法,通过重写sample方法,实现执行一次抽样方法,我们在此处输出"function"属性所对应的值,即输出GUI界面所引入的函数方法返回结果。
- TestSamplerGUI类代码参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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 // TODO Auto-generated method stub TestElement sampler = new TestSampler(); modifyTestElement(sampler); return sampler; } @Override public String getLabelResource() { // TODO Auto-generated method stub return this.getClass().getSimpleName(); } @Override public void modifyTestElement(TestElement sampler) { // TODO Auto-generated method stub super.configureTestElement(sampler); if (sampler instanceof TestSampler) { TestSampler testSmpler = (TestSampler) sampler; testSmpler.setProperty(TestSampler.FUNCTION, functionTextField.getText()); } } @Override public String getStaticLabel() {//设置显示名称 // TODO Auto-generated method stub return "TestSampler"; } private void initFields(){ functionTextField.setText(""); } @Override public void clearGui() { super.clearGui(); initFields(); } } |
我们主要的目的是通过modifyTestElement方法将functionTextField输入框中所输入的值赋给TestSampler对象的function属性。
好了,这样我们就通过上面的简单代码完成了测试用的Sampler,把工程打包为jar包,插入JMeter框架,我们就可以看到该Sampler出现在Sampler列表中:
在线程组下添加该Sampler,并输入被测试函数,运行该测试计划,可以看到控制台输出:
OK,和我们的意图一致,输出了__UUID函数运行后所返回的结果。
有了测试用的Sampler后,我们就可以开发拥有自己功能的Function插件了。
Function插件开发案例
我们可以写一个计算阶乘的Function,将其命名为Factorial,主要代码参考如下(打包在如*.jmeter.functions下):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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.util.JMeterUtils; import org.apache.jorphan.logging.LoggingManager; import org.apache.log.Logger; public class Factorial extends AbstractFunction { private static final Logger log = LoggingManager.getLoggerForClass(); private static final List<String> desc = new LinkedList<String>(); private static final String KEY = "__factorial"; private Object[] values = null; static { desc.add("factorial_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; } return String.valueOf(factorial(num)); } //获取函数引用关键字 @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(); } private int factorial(int num){ int result = 1; if(num < 0){ return -1; } if(num == 0){ result = 1; } else { for(int i = num; i > 0; i--){ result *= i; } } return result; } } |
通过继承AbstractFunction抽象类,重写getArgumentDesc方法实现对函数参数的描述,重写setParameters方法来对函数的参数进行检查和设置,重写getReferenceKey方法告诉JMeter该函数在框架中的引用名称,重写execute方法,实现对该函数的执行并返回结果。通过上述代码我们完成了对Factorial函数组件的编写。
将插件打包插入JMeter框架,可以在函数助手中查看到该函数组件内容如下:
框架已经引入了我们所开发的函数组件,我们根据参数设置计算10的阶乘,设置如下:
通过TestSampler对其进行测试,测试结果如下:
控制台输出了10的阶乘返回结果。就此,我们便通过此章节基本掌握了Function(函数)插件式组件的主要开发过程了。