1. 新建maven工程

2.pom文件引入jmeter的核心包

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>jmeterplugntest</groupId>
  6. <artifactId>jmeterplugntest</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>jmeterplugntest</name>
  11. <url>http://maven.apache.org</url>
  12.  
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. <jmeter-version>3.1</jmeter-version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.apache.jmeter</groupId>
  20. <artifactId>ApacheJMeter_core</artifactId>
  21. <version>${jmeter-version}</version>
  22. <scope>provided</scope>
  23. </dependency>
  24.  
  25. <dependency>
  26. <groupId>org.apache.jmeter</groupId>
  27. <artifactId>ApacheJMeter_java</artifactId>
  28. <version>${jmeter-version}</version>
  29. <scope>provided</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>junit</groupId>
  33. <artifactId>junit</artifactId>
  34. <version>3.8.1</version>
  35. <scope>test</scope>
  36. </dependency>
  37. </dependencies>
  38. <build>
  39. <finalName>ssmtest</finalName>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.apache.maven.plugins</groupId>
  43. <artifactId>maven-compiler-plugin</artifactId>
  44. <version>3.5.1</version>
  45. <configuration>
  46. <source>1.8</source>
  47. <target>1.8</target>
  48. <encoding>UTF-8</encoding>
  49. </configuration>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. </project>

3. 新建一个类继承AbstractSamplerGui

  1. package com.test.gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6.  
  7. import javax.swing.BorderFactory;
  8. import javax.swing.Box;
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JCheckBox;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14.  
  15. import org.apache.jmeter.gui.util.HorizontalPanel;
  16. import org.apache.jmeter.gui.util.JSyntaxTextArea;
  17. import org.apache.jmeter.gui.util.JTextScrollPane;
  18. import org.apache.jmeter.gui.util.VerticalPanel;
  19. import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
  20. import org.apache.jmeter.testelement.TestElement;
  21. import org.apache.jmeter.testelement.property.BooleanProperty;
  22. import org.apache.jmeter.util.JMeterUtils;
  23. import org.apache.jorphan.gui.JLabeledChoice;
  24. import com.test.sampler.MyPluginSampler;
  25.  
  26. public class MypluginGUI extends AbstractSamplerGui {
  27.  
  28. private static final long serialVersionUID = 240L;
  29.  
  30. private JTextField domain;
  31. private JTextField port;
  32. private JTextField contentEncoding;
  33. private JTextField path;
  34. private JCheckBox useKeepAlive;
  35. private JLabeledChoice method;
  36.  
  37. // area区域
  38. private JSyntaxTextArea postBodyContent = JSyntaxTextArea.getInstance(30, 50);
  39. // 滚动条
  40. private JTextScrollPane textPanel = JTextScrollPane.getInstance(postBodyContent);
  41. private JLabel textArea = new JLabel("Message");
  42.  
  43. private JPanel getDomainPanel() {
  44. domain = new JTextField(10);
  45. JLabel label = new JLabel("IP"); // $NON-NLS-1$
  46. label.setLabelFor(domain);
  47.  
  48. JPanel panel = new HorizontalPanel();
  49. panel.add(label, BorderLayout.WEST);
  50. panel.add(domain, BorderLayout.CENTER);
  51. return panel;
  52. }
  53.  
  54. private JPanel getPortPanel() {
  55. port = new JTextField(10);
  56.  
  57. JLabel label = new JLabel(JMeterUtils.getResString("web_server_port")); // $NON-NLS-1$
  58. label.setLabelFor(port);
  59.  
  60. JPanel panel = new HorizontalPanel();
  61. panel.add(label, BorderLayout.WEST);
  62. panel.add(port, BorderLayout.CENTER);
  63.  
  64. return panel;
  65. }
  66.  
  67. protected JPanel getContentEncoding() {
  68.  
  69. // CONTENT_ENCODING
  70. contentEncoding = new JTextField(10);
  71. JLabel contentEncodingLabel = new JLabel("contentEncoding"); // $NON-NLS-1$
  72. contentEncodingLabel.setLabelFor(contentEncoding);
  73.  
  74. JPanel panel = new HorizontalPanel();
  75. panel.setMinimumSize(panel.getPreferredSize());
  76. panel.add(Box.createHorizontalStrut(5));
  77.  
  78. panel.add(contentEncodingLabel,BorderLayout.WEST);
  79. panel.add(contentEncoding,BorderLayout.CENTER);
  80. panel.setMinimumSize(panel.getPreferredSize());
  81. return panel;
  82. }
  83.  
  84. protected Component getPath() {
  85. path = new JTextField(15);
  86.  
  87. JLabel label = new JLabel(JMeterUtils.getResString("path")); //$NON-NLS-1$
  88. label.setLabelFor(path);
  89.  
  90. JPanel pathPanel = new HorizontalPanel();
  91. pathPanel.add(label);
  92. pathPanel.add(path);
  93.  
  94. JPanel panel = new HorizontalPanel();
  95. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  96. panel.add(pathPanel);
  97.  
  98. return panel;
  99. }
  100.  
  101. protected Component getMethodAndUseKeepAlive() {
  102. useKeepAlive = new JCheckBox(JMeterUtils.getResString("use_keepalive")); // $NON-NLS-1$
  103. useKeepAlive.setFont(null);
  104. useKeepAlive.setSelected(true);
  105. JPanel optionPanel = new HorizontalPanel();
  106. optionPanel.setMinimumSize(optionPanel.getPreferredSize());
  107. optionPanel.add(useKeepAlive);
  108. String Marry[] = { "GET", "POST" };
  109. method = new JLabeledChoice(JMeterUtils.getResString("method"), // $NON-NLS-1$
  110. Marry, true, false);
  111. // method.addChangeListener(this);
  112. JPanel panel = new HorizontalPanel();
  113. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  114. panel.add(optionPanel,BorderLayout.WEST);
  115. panel.add(method,BorderLayout.WEST);
  116. return panel;
  117. }
  118.  
  119. protected Component getpostBodyContent() {
  120.  
  121. JPanel panel = new HorizontalPanel();
  122. JPanel ContentPanel = new VerticalPanel();
  123. JPanel messageContentPanel = new JPanel(new BorderLayout());
  124. messageContentPanel.add(this.textArea, BorderLayout.NORTH);
  125. messageContentPanel.add(this.textPanel, BorderLayout.CENTER);
  126. ContentPanel.add(messageContentPanel);
  127. ContentPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Content"));
  128. panel.add(ContentPanel);
  129. return panel;
  130. }
  131.  
  132. public MypluginGUI() {
  133. super();
  134. init();
  135. }
  136.  
  137. private void init() { // WARNING: called from ctor so must not be overridden (i.e. must be private or
  138. // final)
  139. creatPanel();
  140. }
  141.  
  142. public void creatPanel() {
  143. JPanel settingPanel = new VerticalPanel(5, 0);
  144. settingPanel.add(getDomainPanel());
  145. settingPanel.add(getPortPanel());
  146. settingPanel.add(getContentEncoding());
  147. settingPanel.add(getPath());
  148. settingPanel.add(getMethodAndUseKeepAlive());
  149. settingPanel.add(getpostBodyContent());
  150. JPanel dataPanel = new JPanel(new BorderLayout(5, 0));
  151.  
  152. dataPanel.add(settingPanel, BorderLayout.NORTH);
  153. setLayout(new BorderLayout(0, 5));
  154. setBorder(makeBorder());
  155. add(makeTitlePanel(), BorderLayout.NORTH); // Add the standard title
  156. add(dataPanel, BorderLayout.CENTER);
  157. }
  158.  
  159. /*
  160. * 创建一个新的Sampler,然后将界面中的数据设置到这个新的Sampler实例中
  161. * */
  162. @Override
  163. public TestElement createTestElement() {
  164. // TODO Auto-generated method stub
  165. MyPluginSampler sampler = new MyPluginSampler();
  166. modifyTestElement(sampler);
  167. return sampler;
  168. }
  169.  
  170. @Override
  171. public String getLabelResource() {
  172. // TODO Auto-generated method stub
  173. throw new IllegalStateException("This shouldn't be called");
  174. // return "example_title";
  175. // 从messages_zh_CN.properties读取
  176. }
  177.  
  178. @Override
  179. public String getStaticLabel() {
  180. return "Qiao jiafei";
  181. }
  182.  
  183. /*
  184. * 把界面的数据移到Sampler中,与configure方法相反
  185. * */
  186. @Override
  187. public void modifyTestElement(TestElement arg0) {
  188. // TODO Auto-generated method stub
  189. arg0.clear();
  190. configureTestElement(arg0);
  191.  
  192. arg0.setProperty(MyPluginSampler.domain, domain.getText());
  193. arg0.setProperty(MyPluginSampler.port, port.getText());
  194. arg0.setProperty(MyPluginSampler.contentEncoding, contentEncoding.getText());
  195. arg0.setProperty(MyPluginSampler.path, path.getText());
  196. arg0.setProperty(MyPluginSampler.method, method.getText());
  197. arg0.setProperty(MyPluginSampler.postBodyContent, postBodyContent.getText());
  198. arg0.setProperty(new BooleanProperty(MyPluginSampler.useKeepAlive, useKeepAlive.isSelected()));
  199.  
  200. }
  201.  
  202. /*
  203. * reset新界面的时候调用,这里可以填入界面控件中需要显示的一些缺省的值
  204. * */
  205. @Override
  206. public void clearGui() {
  207. super.clearGui();
  208.  
  209. domain.setText("");
  210. port.setText("");
  211. contentEncoding.setText("");
  212. path.setText("");
  213. method.setText("GET");
  214. postBodyContent.setText("");
  215. useKeepAlive.setSelected(true);
  216.  
  217. }
  218.  
  219. /*
  220. * 把Sampler中的数据加载到界面中
  221. * */
  222. @Override
  223. public void configure(TestElement element) {
  224.  
  225. super.configure(element);
  226. // jmeter运行后,保存参数,不然执行后,输入框会情况
  227.  
  228. domain.setText(element.getPropertyAsString(MyPluginSampler.domain));
  229. port.setText(element.getPropertyAsString(MyPluginSampler.port));
  230. contentEncoding.setText(element.getPropertyAsString(MyPluginSampler.contentEncoding));
  231. path.setText(element.getPropertyAsString(MyPluginSampler.path));
  232. method.setText("GET");
  233. postBodyContent.setText(element.getPropertyAsString(MyPluginSampler.postBodyContent));
  234. useKeepAlive.setSelected(true);
  235.  
  236. }
  237.  
  238. }

4. 接受界面配置参数的的类,处理业务逻辑,继承AbstractSampler

  1. package com.test.sampler;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. import org.apache.jmeter.samplers.AbstractSampler;
  6. import org.apache.jmeter.samplers.Entry;
  7. import org.apache.jmeter.samplers.SampleResult;
  8. import org.apache.jorphan.logging.LoggingManager;
  9. import org.apache.log.Logger;
  10.  
  11. public class MyPluginSampler extends AbstractSampler{
  12. private static final long serialVersionUID = 240L;
  13.  
  14. private static final Logger log = LoggingManager.getLoggerForClass();
  15.  
  16. // The name of the property used to hold our data
  17. public static final String domain = "domain.text";
  18. public static final String port = "port.text";
  19. public static final String contentEncoding = "contentEncoding.text";
  20. public static final String path = "path.text";
  21. public static final String method = "method.text";
  22. public static final String postBodyContent = "postBodyContent.text";
  23. public static final String useKeepAlive = "useKeepAlive.text";
  24.  
  25. private static AtomicInteger classCount = new AtomicInteger(0); // keep track of classes created
  26.  
  27. private String getTitle() {
  28. return this.getName();
  29. }
  30.  
  31. /**
  32. * @return the data for the sample
  33. */
  34. public String getdomain() {
  35. return getPropertyAsString(domain);
  36. //从gui获取domain输入的数据
  37. }
  38.  
  39. public String getport() {
  40. return getPropertyAsString(port);
  41. //从gui获取port输入的数据
  42. }
  43.  
  44. public String getcontentEncoding() {
  45. return getPropertyAsString(contentEncoding);
  46.  
  47. }
  48.  
  49. public String getpath() {
  50. return getPropertyAsString(path);
  51.  
  52. }
  53.  
  54. public String getmethod() {
  55. return getPropertyAsString(method);
  56.  
  57. }
  58.  
  59. public String getpostBodyContent() {
  60. return getPropertyAsString(postBodyContent);
  61.  
  62. }
  63.  
  64. public String getuseKeepAlive() {
  65. return getPropertyAsString(useKeepAlive);
  66.  
  67. }
  68.  
  69. public MyPluginSampler() {
  70. //getTitle方法会调用getName方法,setName不写会默认调用getStaticLabel返回的name值
  71. setName("qiaojiafei");
  72. classCount.incrementAndGet();
  73. trace("FirstPluginSampler()");
  74. }
  75. private void trace(String s) {
  76. String tl = getTitle();
  77. String tn = Thread.currentThread().getName();
  78. String th = this.toString();
  79. log.debug(tn + " (" + classCount.get() + ") " + tl + " " + s + " " + th);
  80. }
  81.  
  82. @Override
  83. public SampleResult sample(Entry arg0) {
  84. // TODO Auto-generated method stub
  85. trace("sample()");
  86. SampleResult res = new SampleResult();
  87. boolean isOK = false; // Did sample succeed?
  88.  
  89. String response = null;
  90. String sdomain = getdomain(); // Sampler data
  91. String sport = getport();
  92. String scontentEncoding = getcontentEncoding();
  93. String spath = getpath();
  94. String smethod = getmethod();
  95. String spostBodyContent = getpostBodyContent();
  96. String suseKeepAlive = getuseKeepAlive();
  97.  
  98. res.setSampleLabel(getTitle());
  99. /*
  100. * Perform the sampling
  101. */
  102. res.sampleStart(); // Start timing
  103. try {
  104.  
  105. // Do something here ...
  106.  
  107. response = Thread.currentThread().getName();
  108.  
  109. /*
  110. * Set up the sample result details
  111. */
  112. res.setSamplerData("setSamplerData!!!");
  113. res.setResponseData(response+sdomain+sport+scontentEncoding+spath+smethod+spostBodyContent+suseKeepAlive, null);
  114. res.setDataType(SampleResult.TEXT);
  115.  
  116. res.setResponseCodeOK();
  117. res.setResponseMessage("OK");// $NON-NLS-1$
  118. isOK = true;
  119. } catch (Exception ex) {
  120. log.debug("", ex);
  121. res.setResponseCode("500");// $NON-NLS-1$
  122. res.setResponseMessage(ex.toString());
  123. }
  124. res.sampleEnd(); // End timimg
  125.  
  126. res.setSuccessful(isOK);
  127.  
  128. return res;
  129. }
  130.  
  131. }

5.工程目录结构如下,到处jar包,存放在jmeter的lib/ext目录下

6.启动jmeter,添加自己增加的sample插件

7.运行后,查看执行结果



jmeter开发自己的sampler插件的更多相关文章

  1. 用阿里巴巴官方给Jmeter开发的Dubbo sampler取样器进行dubbo接口测试【图解剖析】

    自:https://blog.csdn.net/cyjs1988/article/details/84258046 [一]Dubbo sampler下载地址: 该插件支持jmeter 3.2及3.2以 ...

  2. JMeter开发插件——图片验证码识别

    我们在性能测试中总会时不时地遭遇到来自于应用系统的各种阻碍,图片验证码就是一类最常见的束缚,登录或交易时需要按照图片中的内容输入正确的验证信息后,数据才可以提交成功,这使得许多性能测试工具只能望而却步 ...

  3. 如何为Apache JMeter开发插件(二)—第一个JMeter插件

    文章内容转载于:http://lib.csdn.net/article/softwaretest/25700,并且加上个人一些截图 本篇将开启为JMeter开发插件之旅,我们选择以Function(函 ...

  4. (十)Jmeter中的Debug Sampler介绍

    一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...

  5. Notepad++进行php开发所必需的插件

    Notepad++进行php开发所必需的插件有那些呢? 1. Compare: 可以用来比较两个文件不同之处. 2. Explorer:文件浏览器插件,包含收藏夹.Session保存功能.可与NppE ...

  6. 我利用网上代码开发的JQuery图片插件

    我利用网上代码开发的JQuery图片插件 代码如下 (function($){ $.fn.FocusPic = function(options){ var defaults = { interval ...

  7. Cordova - 与iOS原生代码交互2(使用Swift开发Cordova的自定义插件)

    在前一篇文章中我介绍了如何通过 js 与原生代码进行交互(Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)),当时是直接对Cordova生成的iOS工程项目进行编辑操作的(添加 ...

  8. (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明

    (原)http://www.cnblogs.com/hxling/archive/2012/12/12/2814207.html jQuery Mobile 移动开发中的日期插件Mobiscroll ...

  9. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(三)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2340661.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

随机推荐

  1. 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)

    链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...

  2. grep参数说明及常用用法(转)

    转:https://www.cnblogs.com/leo-li-3046/p/5690613.html grep常用参数说明 grep [OPTIONS] PATTERN [FILE...] gre ...

  3. 15、SpringBoot-CRUD错误处理机制(2)

    二.如何定制错误响应 1).如何定义错误处理页面 1.1.有模板引擎的情况下:error/状态码;         [将错误页面命名为 错误状态码.html 放在模板引擎文件夹里面的error文件夹下 ...

  4. 6.spring:AOP(注解)

     spring Aop AOP面向切面编程,与OOP面向对象编程相辅相成 AOP中最基本的单元是切面 问题: 代码混乱:越来越多的业务需求(日志&验证)加入后,原有的业务方法急剧膨胀,每个方法 ...

  5. HDU 1249 三角形(三角形分割平面)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1249 三角形 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  6. Python开发工具之Sublime Text 3基于文件创建项目

    说明: 本地windows系统 本地已安装Sublime Text 3; 本地已创建python项目文件,如test,并在该文件夹下创建了虚拟环境venv(test/venv). 1.创建项目 依次鼠 ...

  7. 一文读懂类加载机制--ClassLoader

    一.什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程 ...

  8. Swift_继承

    Swift_继承 点击查看源码 func testInheritance() { //基类 class Base { var count = 0.0 var description: String { ...

  9. 为什么我们需要DTO?

    最近在写代码时突然产生了这个疑惑,我们为什么需要DTO进行数据传输呢? 要了解DTO首先我们要知道什么是DAO,DAO就是数据库的一个数据模型,是一个类文件里面存储着数据库的字段及其getter&am ...

  10. ubnutu 安装protocol buffer

    工作中需要使用protocol buffer,需要编译出protocol buffer的动态链接库,然后在别的makefile中链接它, 我的环境是ubnutu16.04,64bit,使用的proto ...