JMeter自定义采样器插件开发
目录
- JMeter自定义采样器插件开发
- 1. 简介
- 2. 需求简介
- 3.成品展示
- 成功展示
- 失败展示
- 4. 准备开发环境
- 4.1 准备pom文件
- 4.2 新建Java的GUI类
- 4.3 准备Java的采样器
- 5. 打包&部署
- 6. 参考文章
1. 简介
JMeter支持插件机制,只需要将打包好的jar包放到lib/ext/
下面,JMeter就会动态的加载符合要求的插件。
- 要扩展UI的话,扩展的Java类的包名必须是
.gui.
- 同样的扩展函数的Java类的包名必须是
.function.
2. 需求简介
本文演示的是如何自定义一个https
的采样器。
ssl的配置以参数的形式传给JMeter。
3.成品展示
成功展示
失败展示
4. 准备开发环境
新建Maven项目。
4.1 准备pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>httpsSampler</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jmeter-version>3.0</jmeter-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>${jmeter-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>${jmeter-version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4.2 新建Java的GUI类
新建 org.apache.jmeter.protocol.https.control.gui
类,并继承org.apache.jmeter.samplers.gui.AbstractSamplerGui
需要重写几个函数。
/**gui显示的sample的名称**/
public String getStaticLabel()
public String getLabelResource()
//这个方法用于把界面的数据移到Sampler中。
public void modifyTestElement(TestElement testElement)
//界面与Sampler之间的数据交换
public void configure(TestElement el)
//该方法会在reset新界面的时候调用,这里可以填入界面控件中需要显示的一些缺省的值(就是默认显示值)
public void clearGui()
//该方法创建一个新的Sampler,然后将界面中的数据设置到这个新的Sampler实例中。
public TestElement createTestElement()
全代码
package org.apache.jmeter.protocol.https.control.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.jmeter.gui.util.JSyntaxTextArea;
import org.apache.jmeter.gui.util.JTextScrollPane;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.protocol.https.sampler.HttpsSampler;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
//这个注解必须要有
@SuppressWarnings("deprecation")
public class HttpsSamplerUI extends AbstractSamplerGui {
private static final long serialVersionUID = 1L;
private static Logger log = LoggingManager.getLoggerForClass();
private final JLabeledTextField sslVersionField = new JLabeledTextField("SSL版本");
private final JLabeledTextField cipherField = new JLabeledTextField("密码套件");
private final JLabeledTextField twoWayField = new JLabeledTextField("双向");
private final JLabeledTextField caCertField = new JLabeledTextField("CA证书");
private final JLabeledTextField clientCertField = new JLabeledTextField("客户端证书");
private final JLabeledTextField clientP12Field = new JLabeledTextField("客户端私钥");
private final JLabeledTextField requestsStringField = new JLabeledTextField("请求信息");
private final JSyntaxTextArea textMessage = new JSyntaxTextArea(10, 50);
// private final JLabel textArea = new JLabel(JMeterUtils.getResString("kafka.message", "Message"));
private final JLabel textArea = new JLabel("Message");
private final JTextScrollPane textPanel = new JTextScrollPane(textMessage);
public HttpsSamplerUI(){
super();
this.init();
}
/**
这里使用的是Java的CUI代码,可以自行百度,类似于设计网页布局内容。就是添加面板,然后往面板中添加输入框和文字提示,文本框下拉框等等。
*/
private void init(){
log.info("Initializing the UI.");
setLayout(new BorderLayout());
setBorder(makeBorder());
add(makeTitlePanel(), BorderLayout.NORTH);
JPanel mainPanel = new VerticalPanel();
add(mainPanel, BorderLayout.CENTER);
JPanel DPanel = new JPanel();
DPanel.setLayout(new GridLayout(4, 2));
DPanel.add(sslVersionField);
DPanel.add(cipherField);
DPanel.add(twoWayField);
DPanel.add(caCertField);
DPanel.add(clientCertField);
DPanel.add(clientP12Field);
DPanel.add(requestsStringField);
JPanel ControlPanel = new VerticalPanel();
ControlPanel.add(DPanel);
ControlPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "参数"));
mainPanel.add(ControlPanel);
/**这是是输出**/
JPanel ContentPanel = new VerticalPanel();
JPanel messageContentPanel = new JPanel(new BorderLayout());
messageContentPanel.add(this.textArea, BorderLayout.NORTH);
messageContentPanel.add(this.textPanel, BorderLayout.CENTER);
ContentPanel.add(messageContentPanel);
ContentPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Content"));
mainPanel.add(ContentPanel);
}
/**
* 该方法创建一个新的Sampler,然后将界面中的数据设置到这个新的Sampler实例中。
* **/
@Override
public TestElement createTestElement() {
HttpsSampler sampler = new HttpsSampler();
this.setupSamplerProperties(sampler);
return sampler;
}
/**
* 该方法会在reset新界面的时候调用,这里可以填入界面控件中需要显示的一些缺省的值。
* **/
@Override
public void clearGui(){
super.clearGui();
this.sslVersionField.setText("TLSv1.2");
this.cipherField.setText("ECDHE-AES256-SHA384");
this.twoWayField.setText("y");
this.caCertField.setText("xxx/xxx/ca.cert");
this.clientCertField.setText("xxx/xxx/client.cert");
this.clientP12Field.setText("xxx/xxx/client.key");
this.requestsStringField.setText("GET /1k.html HTTP1.0\r\n");
}
/**
* 界面与Sampler之间的数据交换
* 该方法用于把Sampler中的数据加载到界面中。
* 在实现自己的逻辑之前,先调用一下父类的方法super.configure(el),这样可以确保框架自动为你加载一些缺省数据,比如Sampler的名字。
* **/
@Override
public void configure(TestElement el){
super.configure(el);
HttpsSampler sampler = (HttpsSampler) el;
this.sslVersionField.setText(sampler.getHttpsSslVersion());
this.cipherField.setText(sampler.getHttpsCipher());
this.twoWayField.setText(sampler.getHttpsTwoWay());
this.caCertField.setText(sampler.getHttpsCa());
this.clientCertField.setText(sampler.getHttpsClientCert());
this.clientP12Field.setText(sampler.getHttpsClientP12());
this.requestsStringField.setText(sampler.getHttpsRequest());
}
private void setupSamplerProperties(HttpsSampler sampler) {
this.configureTestElement(sampler);
sampler.setSslVersion(this.sslVersionField.getText());
sampler.setCipher(this.cipherField.getText());
sampler.setHttpsTwoWay(this.twoWayField.getText());
sampler.setHttpsCa(this.caCertField.getText());
sampler.setHttpsClientCert(this.clientCertField.getText());
sampler.setHttpsClientP12(this.clientP12Field.getText());
sampler.setHttpsRequest(this.requestsStringField.getText());
}
/**gui显示sample的名称**/
@Override
public String getStaticLabel() {
return "Https Sampler";
}
@Override
public String getLabelResource() {
throw new IllegalStateException("This shouldn't be called");
}
/**
* 这个方法用于把界面的数据移到Sampler中,刚好与上面的方法相反。
* 在调用自己的实现方法之前,请先调用一下super.configureTestElement(e),这个会帮助移到一些缺省的数据。
* **/
@Override
public void modifyTestElement(TestElement testElement) {
HttpsSampler sampler = (HttpsSampler) testElement;
this.setupSamplerProperties(sampler);
}
}
4.3 准备Java的采样器
新建org.apache.jmeter.protocol.https.sampler
继承 org.apache.jmeter.samplers.AbstractSampler
实现 org.apache.jmeter.testelement.TestStateListener
接口
重写函数
//该方法是JMeter实现对目标系统发起请求实际工作的地方
public SampleResult sample(Entry entry)
这个4个方法必须覆写,否则无法识别
@Override
public void testStarted() {
}
@Override
public void testStarted(String s) {
}
@Override
public void testEnded() {
this.testEnded("local");
}
@Override
public void testEnded(String s) {
}
全代码
package org.apache.jmeter.protocol.https.sampler;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.TestStateListener;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
public class HttpsSampler extends AbstractSampler implements TestStateListener {
private static final long serialVersionUID = 1L;
private static final String HTTPS_SSL_VERSION = "https.sslVersion";
private static final String HTTPS_CIPHER = "https.cipher";
private static final String HTTPS_TWO_WAY= "https.twoWay";
private static final String HTTPS_CA = "https.caCert";
private static final String HTTPS_CLIENT_CERT = "https.clientCert";
private static final String HTTPS_CLIENT_P12 = "https.clientP12";
private static final String HTTPS_REQUEST = "https.requestsString";
private static final Logger log = LoggingManager.getLoggerForClass();
// 构造函数要为pubilc
public HttpsSampler(){
setName("Https Sampler");
}
/**
* 该方法是JMeter实现对目标系统发起请求实际工作的地方
* **/
@Override
public SampleResult sample(Entry entry) {
SampleResult result = new SampleResult();
result.setSampleLabel(getName());
result.sampleStart();
try {
/**
* 采样器执行code处
* **/
result.setRequestHeaders("请求头---请求头展示数据");
result.setSamplerData("设置采样器数据");
if (!getHttpsSslVersion().startsWith("TLS")) {
// stop stopwatch
result.sampleEnd();
result.setSuccessful(false);
// 设置取样器结果里面响应信息
result.setResponseMessage("ssl不为TLS开头,采样失败(示例)");
result.setDataType(org.apache.jmeter.samplers.SampleResult.TEXT);
result.setResponseCode("FAILED");
// 设置响应数据的响应body
result.setResponseData("响应信息---响应体展示数据", null);
// 设置响应数据的响应header
result.setResponseHeaders("响应头信息---响应头展示数据");
}else {
result.sampleEnd();
result.setSuccessful(true);
result.setResponseCodeOK();
}
} catch (Exception e) {
result.sampleEnd(); // stop stopwatch
result.setSuccessful(false);
result.setResponseMessage("Exception: " + e);
// get stack trace as a String to return as document data
java.io.StringWriter stringWriter = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(stringWriter));
result.setResponseData(stringWriter.toString(), null);
result.setDataType(org.apache.jmeter.samplers.SampleResult.TEXT);
result.setResponseCode("FAILED");
}
return result;
}
public void setSslVersion(String sslVersion){
setProperty(HTTPS_SSL_VERSION, sslVersion);
}
public void setCipher(String cipher){
setProperty(HTTPS_CIPHER, cipher);
}
public void setHttpsTwoWay(String twoWay){
setProperty(HTTPS_TWO_WAY, twoWay);
}
public void setHttpsCa(String ca){
setProperty(HTTPS_CA, ca);
}
public void setHttpsClientCert(String clientCert){
setProperty(HTTPS_CLIENT_CERT, clientCert);
}
public void setHttpsClientP12(String clientP12){
setProperty(HTTPS_CLIENT_P12, clientP12);
}
public void setHttpsRequest(String requestsString){
setProperty(HTTPS_REQUEST, requestsString);
}
public String getHttpsSslVersion(){
return getPropertyAsString(HTTPS_SSL_VERSION);
}
public String getHttpsCipher(){
return getPropertyAsString(HTTPS_CIPHER);
}
public String getHttpsTwoWay(){
return getPropertyAsString(HTTPS_TWO_WAY);
}
public String getHttpsCa(){
return getPropertyAsString(HTTPS_CA);
}
public String getHttpsClientCert(){
return getPropertyAsString(HTTPS_CLIENT_CERT);
}
public String getHttpsClientP12(){
return getPropertyAsString(HTTPS_CLIENT_P12);
}
public String getHttpsRequest(){
return getPropertyAsString(HTTPS_REQUEST);
}
@Override
public void testStarted() {
}
@Override
public void testStarted(String s) {
}
@Override
public void testEnded() {
this.testEnded("local");
}
@Override
public void testEnded(String s) {
}
}
5. 打包&部署
maven install
后会在target/
下会有打包好的jar包。
然后将jar包拷贝到lib/ext/
下,启动JMeter。选取新增的取样器即可。
6. 参考文章
JMeter扩展插件实现对自定义协议进行支持