扩展jmeter Sampler
2017-09-30 本文已影响0人
递归思念
一,什么是jmeter Sampler
jmeterSampler二,准备工作
需要一些java基础及java开发环境(推荐IDEA)
三,实施阶段
以jemeter自带的示例ExampleSampler为范:
1.编写Sampler
package org.apache.jmeter.examples.sampler;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExampleSampler extends AbstractSampler {
//每个Sampler都是AbstractSampler 的子类
private static final long serialVersionUID = 240L;
private static final Logger log = LoggerFactory.getLogger(ExampleSampler.class);
//以常量方式定义需要输入的参数
public static final String DATA = "ExampleSampler.data";
// keep track of classes created
private static AtomicInteger classCount = new AtomicInteger(0);
// 计数与日志跟踪,可以忽略
public ExampleSampler() {
classCount.incrementAndGet();
trace("ExampleSampler()");
}
//Sampler的测试过程,测试结果设置
@Override
public SampleResult sample(Entry e) {
trace("sample()");
SampleResult res = new SampleResult();
boolean isOK = false; // Did sample succeed?
String data = getData(); // Sampler data
String response = null;
res.setSampleLabel(getTitle());
/*
* Perform the sampling
*/
res.sampleStart(); // Start timing
try {
// Do something here ...
response = Thread.currentThread().getName();
/*
* Set up the sample result details
*/
res.setSamplerData(data);
res.setResponseData(response, null);
res.setDataType(SampleResult.TEXT);
res.setResponseCodeOK();
res.setResponseMessage("OK");// $NON-NLS-1$
isOK = true;
} catch (Exception ex) {
log.debug("", ex);
res.setResponseCode("500");// $NON-NLS-1$
res.setResponseMessage(ex.toString());
}
res.sampleEnd(); // End timimg
res.setSuccessful(isOK);
return res;
}
//测试结果标题
private String getTitle() {
return this.getName();
}
//从JMeterProperty中获取GUI中得到的参数值
public String getData() {
return getPropertyAsString(DATA);
}
//跟踪日志,可以忽略
private void trace(String s) {
String tl = getTitle();
String tn = Thread.currentThread().getName();
String th = this.toString();
log.debug(tn + " (" + classCount.get() + ") " + tl + " " + s + " " + th);
}
}
2,编写SamplerGUI
package org.apache.jmeter.examples.sampler.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import org.apache.jmeter.examples.sampler.ExampleSampler;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
public class ExampleSamplerGui extends AbstractSamplerGui {
private static final long serialVersionUID = 240L;
//定义界面元素,用于获取参数输入
private JTextArea data;
public ExampleSamplerGui() {
init();
}
//设置Sampler标题,但由于国际化,需要在core的message.propertis文件中定义,所以getStaticLabel()方法直接设置标题
@Override
public String getLabelResource() {
return "example_title"; // $NON-NLS-1$
}
//从界面获取参数
@Override
public void configure(TestElement element) {
data.setText(element.getPropertyAsString(ExampleSampler.DATA));
super.configure(element);
}
//调用ExampleSampler
@Override
public TestElement createTestElement() {
ExampleSampler sampler = new ExampleSampler();
modifyTestElement(sampler);
return sampler;
}
//将从页面获取到的参数值加入JMeterProperty中,供Sampler中获取
@Override
public void modifyTestElement(TestElement te) {
te.clear();
configureTestElement(te);
te.setProperty(ExampleSampler.DATA, data.getText());
}
//设置GUI界面
private void init() {
// Standard setup
setLayout(new BorderLayout(0, 5));
setBorder(makeBorder());
add(makeTitlePanel(), BorderLayout.NORTH); // Add the standard title
// Specific setup
add(createDataPanel(), BorderLayout.CENTER);
}
//自定义的参数输入单元
private Component createDataPanel() {
JLabel label = new JLabel(JMeterUtils.getResString("example_data")); //$NON-NLS-1$
data = new JTextArea();
data.setName(ExampleSampler.DATA);
label.setLabelFor(data);
JPanel dataPanel = new JPanel(new BorderLayout(5, 0));
dataPanel.add(label, BorderLayout.WEST);
dataPanel.add(data, BorderLayout.CENTER);
return dataPanel;
}
//设置参数的默认值
@Override
public void clearGui() {
super.clearGui();
data.setText(""); // $NON-NLS-1$
}
}