jmeter开发自己的sampler插件
1. 新建maven工程
2.pom文件引入jmeter的核心包
- <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>jmeterplugntest</groupId>
- <artifactId>jmeterplugntest</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>jmeterplugntest</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <jmeter-version>3.1</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>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <finalName>ssmtest</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.5.1</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
3. 新建一个类继承AbstractSamplerGui
- package com.test.gui;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Component;
- import javax.swing.BorderFactory;
- import javax.swing.Box;
- import javax.swing.BoxLayout;
- import javax.swing.JCheckBox;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import org.apache.jmeter.gui.util.HorizontalPanel;
- 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.samplers.gui.AbstractSamplerGui;
- import org.apache.jmeter.testelement.TestElement;
- import org.apache.jmeter.testelement.property.BooleanProperty;
- import org.apache.jmeter.util.JMeterUtils;
- import org.apache.jorphan.gui.JLabeledChoice;
- import com.test.sampler.MyPluginSampler;
- public class MypluginGUI extends AbstractSamplerGui {
- private static final long serialVersionUID = 240L;
- private JTextField domain;
- private JTextField port;
- private JTextField contentEncoding;
- private JTextField path;
- private JCheckBox useKeepAlive;
- private JLabeledChoice method;
- // area区域
- private JSyntaxTextArea postBodyContent = JSyntaxTextArea.getInstance(30, 50);
- // 滚动条
- private JTextScrollPane textPanel = JTextScrollPane.getInstance(postBodyContent);
- private JLabel textArea = new JLabel("Message");
- private JPanel getDomainPanel() {
- domain = new JTextField(10);
- JLabel label = new JLabel("IP"); // $NON-NLS-1$
- label.setLabelFor(domain);
- JPanel panel = new HorizontalPanel();
- panel.add(label, BorderLayout.WEST);
- panel.add(domain, BorderLayout.CENTER);
- return panel;
- }
- private JPanel getPortPanel() {
- port = new JTextField(10);
- JLabel label = new JLabel(JMeterUtils.getResString("web_server_port")); // $NON-NLS-1$
- label.setLabelFor(port);
- JPanel panel = new HorizontalPanel();
- panel.add(label, BorderLayout.WEST);
- panel.add(port, BorderLayout.CENTER);
- return panel;
- }
- protected JPanel getContentEncoding() {
- // CONTENT_ENCODING
- contentEncoding = new JTextField(10);
- JLabel contentEncodingLabel = new JLabel("contentEncoding"); // $NON-NLS-1$
- contentEncodingLabel.setLabelFor(contentEncoding);
- JPanel panel = new HorizontalPanel();
- panel.setMinimumSize(panel.getPreferredSize());
- panel.add(Box.createHorizontalStrut(5));
- panel.add(contentEncodingLabel,BorderLayout.WEST);
- panel.add(contentEncoding,BorderLayout.CENTER);
- panel.setMinimumSize(panel.getPreferredSize());
- return panel;
- }
- protected Component getPath() {
- path = new JTextField(15);
- JLabel label = new JLabel(JMeterUtils.getResString("path")); //$NON-NLS-1$
- label.setLabelFor(path);
- JPanel pathPanel = new HorizontalPanel();
- pathPanel.add(label);
- pathPanel.add(path);
- JPanel panel = new HorizontalPanel();
- panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
- panel.add(pathPanel);
- return panel;
- }
- protected Component getMethodAndUseKeepAlive() {
- useKeepAlive = new JCheckBox(JMeterUtils.getResString("use_keepalive")); // $NON-NLS-1$
- useKeepAlive.setFont(null);
- useKeepAlive.setSelected(true);
- JPanel optionPanel = new HorizontalPanel();
- optionPanel.setMinimumSize(optionPanel.getPreferredSize());
- optionPanel.add(useKeepAlive);
- String Marry[] = { "GET", "POST" };
- method = new JLabeledChoice(JMeterUtils.getResString("method"), // $NON-NLS-1$
- Marry, true, false);
- // method.addChangeListener(this);
- JPanel panel = new HorizontalPanel();
- panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
- panel.add(optionPanel,BorderLayout.WEST);
- panel.add(method,BorderLayout.WEST);
- return panel;
- }
- protected Component getpostBodyContent() {
- JPanel panel = new HorizontalPanel();
- 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"));
- panel.add(ContentPanel);
- return panel;
- }
- public MypluginGUI() {
- super();
- init();
- }
- private void init() { // WARNING: called from ctor so must not be overridden (i.e. must be private or
- // final)
- creatPanel();
- }
- public void creatPanel() {
- JPanel settingPanel = new VerticalPanel(5, 0);
- settingPanel.add(getDomainPanel());
- settingPanel.add(getPortPanel());
- settingPanel.add(getContentEncoding());
- settingPanel.add(getPath());
- settingPanel.add(getMethodAndUseKeepAlive());
- settingPanel.add(getpostBodyContent());
- JPanel dataPanel = new JPanel(new BorderLayout(5, 0));
- dataPanel.add(settingPanel, BorderLayout.NORTH);
- setLayout(new BorderLayout(0, 5));
- setBorder(makeBorder());
- add(makeTitlePanel(), BorderLayout.NORTH); // Add the standard title
- add(dataPanel, BorderLayout.CENTER);
- }
- /*
- * 创建一个新的Sampler,然后将界面中的数据设置到这个新的Sampler实例中
- * */
- @Override
- public TestElement createTestElement() {
- // TODO Auto-generated method stub
- MyPluginSampler sampler = new MyPluginSampler();
- modifyTestElement(sampler);
- return sampler;
- }
- @Override
- public String getLabelResource() {
- // TODO Auto-generated method stub
- throw new IllegalStateException("This shouldn't be called");
- // return "example_title";
- // 从messages_zh_CN.properties读取
- }
- @Override
- public String getStaticLabel() {
- return "Qiao jiafei";
- }
- /*
- * 把界面的数据移到Sampler中,与configure方法相反
- * */
- @Override
- public void modifyTestElement(TestElement arg0) {
- // TODO Auto-generated method stub
- arg0.clear();
- configureTestElement(arg0);
- arg0.setProperty(MyPluginSampler.domain, domain.getText());
- arg0.setProperty(MyPluginSampler.port, port.getText());
- arg0.setProperty(MyPluginSampler.contentEncoding, contentEncoding.getText());
- arg0.setProperty(MyPluginSampler.path, path.getText());
- arg0.setProperty(MyPluginSampler.method, method.getText());
- arg0.setProperty(MyPluginSampler.postBodyContent, postBodyContent.getText());
- arg0.setProperty(new BooleanProperty(MyPluginSampler.useKeepAlive, useKeepAlive.isSelected()));
- }
- /*
- * reset新界面的时候调用,这里可以填入界面控件中需要显示的一些缺省的值
- * */
- @Override
- public void clearGui() {
- super.clearGui();
- domain.setText("");
- port.setText("");
- contentEncoding.setText("");
- path.setText("");
- method.setText("GET");
- postBodyContent.setText("");
- useKeepAlive.setSelected(true);
- }
- /*
- * 把Sampler中的数据加载到界面中
- * */
- @Override
- public void configure(TestElement element) {
- super.configure(element);
- // jmeter运行后,保存参数,不然执行后,输入框会情况
- domain.setText(element.getPropertyAsString(MyPluginSampler.domain));
- port.setText(element.getPropertyAsString(MyPluginSampler.port));
- contentEncoding.setText(element.getPropertyAsString(MyPluginSampler.contentEncoding));
- path.setText(element.getPropertyAsString(MyPluginSampler.path));
- method.setText("GET");
- postBodyContent.setText(element.getPropertyAsString(MyPluginSampler.postBodyContent));
- useKeepAlive.setSelected(true);
- }
- }
4. 接受界面配置参数的的类,处理业务逻辑,继承AbstractSampler
- package com.test.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.apache.jorphan.logging.LoggingManager;
- import org.apache.log.Logger;
- public class MyPluginSampler extends AbstractSampler{
- private static final long serialVersionUID = 240L;
- private static final Logger log = LoggingManager.getLoggerForClass();
- // The name of the property used to hold our data
- public static final String domain = "domain.text";
- public static final String port = "port.text";
- public static final String contentEncoding = "contentEncoding.text";
- public static final String path = "path.text";
- public static final String method = "method.text";
- public static final String postBodyContent = "postBodyContent.text";
- public static final String useKeepAlive = "useKeepAlive.text";
- private static AtomicInteger classCount = new AtomicInteger(0); // keep track of classes created
- private String getTitle() {
- return this.getName();
- }
- /**
- * @return the data for the sample
- */
- public String getdomain() {
- return getPropertyAsString(domain);
- //从gui获取domain输入的数据
- }
- public String getport() {
- return getPropertyAsString(port);
- //从gui获取port输入的数据
- }
- public String getcontentEncoding() {
- return getPropertyAsString(contentEncoding);
- }
- public String getpath() {
- return getPropertyAsString(path);
- }
- public String getmethod() {
- return getPropertyAsString(method);
- }
- public String getpostBodyContent() {
- return getPropertyAsString(postBodyContent);
- }
- public String getuseKeepAlive() {
- return getPropertyAsString(useKeepAlive);
- }
- public MyPluginSampler() {
- //getTitle方法会调用getName方法,setName不写会默认调用getStaticLabel返回的name值
- setName("qiaojiafei");
- classCount.incrementAndGet();
- trace("FirstPluginSampler()");
- }
- 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);
- }
- @Override
- public SampleResult sample(Entry arg0) {
- // TODO Auto-generated method stub
- trace("sample()");
- SampleResult res = new SampleResult();
- boolean isOK = false; // Did sample succeed?
- String response = null;
- String sdomain = getdomain(); // Sampler data
- String sport = getport();
- String scontentEncoding = getcontentEncoding();
- String spath = getpath();
- String smethod = getmethod();
- String spostBodyContent = getpostBodyContent();
- String suseKeepAlive = getuseKeepAlive();
- 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("setSamplerData!!!");
- res.setResponseData(response+sdomain+sport+scontentEncoding+spath+smethod+spostBodyContent+suseKeepAlive, 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;
- }
- }
5.工程目录结构如下,到处jar包,存放在jmeter的lib/ext目录下
6.启动jmeter,添加自己增加的sample插件
7.运行后,查看执行结果
jmeter开发自己的sampler插件的更多相关文章
- 用阿里巴巴官方给Jmeter开发的Dubbo sampler取样器进行dubbo接口测试【图解剖析】
自:https://blog.csdn.net/cyjs1988/article/details/84258046 [一]Dubbo sampler下载地址: 该插件支持jmeter 3.2及3.2以 ...
- JMeter开发插件——图片验证码识别
我们在性能测试中总会时不时地遭遇到来自于应用系统的各种阻碍,图片验证码就是一类最常见的束缚,登录或交易时需要按照图片中的内容输入正确的验证信息后,数据才可以提交成功,这使得许多性能测试工具只能望而却步 ...
- 如何为Apache JMeter开发插件(二)—第一个JMeter插件
文章内容转载于:http://lib.csdn.net/article/softwaretest/25700,并且加上个人一些截图 本篇将开启为JMeter开发插件之旅,我们选择以Function(函 ...
- (十)Jmeter中的Debug Sampler介绍
一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...
- Notepad++进行php开发所必需的插件
Notepad++进行php开发所必需的插件有那些呢? 1. Compare: 可以用来比较两个文件不同之处. 2. Explorer:文件浏览器插件,包含收藏夹.Session保存功能.可与NppE ...
- 我利用网上代码开发的JQuery图片插件
我利用网上代码开发的JQuery图片插件 代码如下 (function($){ $.fn.FocusPic = function(options){ var defaults = { interval ...
- Cordova - 与iOS原生代码交互2(使用Swift开发Cordova的自定义插件)
在前一篇文章中我介绍了如何通过 js 与原生代码进行交互(Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)),当时是直接对Cordova生成的iOS工程项目进行编辑操作的(添加 ...
- (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明
(原)http://www.cnblogs.com/hxling/archive/2012/12/12/2814207.html jQuery Mobile 移动开发中的日期插件Mobiscroll ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(三)
http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2340661.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
随机推荐
- 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)
链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...
- grep参数说明及常用用法(转)
转:https://www.cnblogs.com/leo-li-3046/p/5690613.html grep常用参数说明 grep [OPTIONS] PATTERN [FILE...] gre ...
- 15、SpringBoot-CRUD错误处理机制(2)
二.如何定制错误响应 1).如何定义错误处理页面 1.1.有模板引擎的情况下:error/状态码; [将错误页面命名为 错误状态码.html 放在模板引擎文件夹里面的error文件夹下 ...
- 6.spring:AOP(注解)
spring Aop AOP面向切面编程,与OOP面向对象编程相辅相成 AOP中最基本的单元是切面 问题: 代码混乱:越来越多的业务需求(日志&验证)加入后,原有的业务方法急剧膨胀,每个方法 ...
- HDU 1249 三角形(三角形分割平面)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1249 三角形 Time Limit: 2000/1000 MS (Java/Others) Me ...
- Python开发工具之Sublime Text 3基于文件创建项目
说明: 本地windows系统 本地已安装Sublime Text 3; 本地已创建python项目文件,如test,并在该文件夹下创建了虚拟环境venv(test/venv). 1.创建项目 依次鼠 ...
- 一文读懂类加载机制--ClassLoader
一.什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程 ...
- Swift_继承
Swift_继承 点击查看源码 func testInheritance() { //基类 class Base { var count = 0.0 var description: String { ...
- 为什么我们需要DTO?
最近在写代码时突然产生了这个疑惑,我们为什么需要DTO进行数据传输呢? 要了解DTO首先我们要知道什么是DAO,DAO就是数据库的一个数据模型,是一个类文件里面存储着数据库的字段及其getter&am ...
- ubnutu 安装protocol buffer
工作中需要使用protocol buffer,需要编译出protocol buffer的动态链接库,然后在别的makefile中链接它, 我的环境是ubnutu16.04,64bit,使用的proto ...