【转】性能测试分享---java协议(1)------jemter篇

转自:http://blog.csdn.net/weiweicao0429/article/details/8941081

主要用到了两个性能测试工具,一个是jmeter,一个是LoadRunner。

使用jmeter的测试方法:

1、下载jemeter的linux版本的,下载的文件里包含了两个jar包,一个是ApacheJMeter_core.jar,ApacheJMter_java.jar。分别把这两个jar放到eclipse的Java Build Path的libraries下面。

2、创建编写脚本的类,使他继承AbstractJavaSamplerClient。初步结构是

[java] view plaincopyprint?

  1. private SampleResult results;
  2. public void setupTest(JavaSamplerContext arg0) {
  3. results = new SampleResult();
  4. }
  5. public Arguments getDefaultParameters() {
  6. Arguments params = new Arguments();
  7. return params;
  8. }
  9. //测试执行的循环体,根据线程数和循环次数的不同可执行多次,类似于LoadRunner中的Action方法
  10. public SampleResult runTest(JavaSamplerContext arg0) {
  11. results.sampleStart();
  12. results.sampleEnd();
  13. results.setSuccessful(true);
  14. return results;
  15. }
  16. //结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法
  17. public void teardownTest(JavaSamplerContext arg0) {
  18. }
private SampleResult results;

public void setupTest(JavaSamplerContext arg0) {
 results = new SampleResult();
 }

public Arguments getDefaultParameters() {
 Arguments params = new Arguments();
 return params;
 }

//测试执行的循环体,根据线程数和循环次数的不同可执行多次,类似于LoadRunner中的Action方法
 public SampleResult runTest(JavaSamplerContext arg0) {

results.sampleStart();

results.sampleEnd();
 results.setSuccessful(true);
 return results;
 }

//结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法
 public void teardownTest(JavaSamplerContext arg0) {
 }

将需要循环的业务流程放到runTest方法里。编写好的代码如下:

[java] view plaincopyprint?

  1. package app;
  2. import http.HttpClient4;
  3. import http.HttpParameter;
  4. import http.HttpResp;
  5. import http.JsonUtil;
  6. import java.io.IOException;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.Map;
  12. import java.util.Random;
  13. import org.apache.commons.httpclient.HttpClient;
  14. import org.apache.commons.httpclient.HttpException;
  15. import org.apache.commons.httpclient.methods.PostMethod;
  16. import org.apache.jmeter.config.Arguments;
  17. import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
  18. import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
  19. import org.apache.jmeter.samplers.SampleResult;
  20. import com.yeepay.g3.utils.common.encrypt.AES;
  21. import com.yibao.utils.des3.RSA_Encrypt;
  22. public class TheardTest extends AbstractJavaSamplerClient{
  23. private SampleResult results;
  24. public void setupTest(JavaSamplerContext arg0) {
  25. results = new SampleResult();
  26. }
  27. public Arguments getDefaultParameters() {
  28. Arguments params = new Arguments();
  29. return params;
  30. }
  31. public static String ramdonNum(int num){
  32. Random rd = new Random();
  33. StringBuilder sb = new StringBuilder("");
  34. if (num <1){
  35. return null;
  36. }
  37. else{
  38. for(int i=1;i<=num;i++){
  39. int sr=rd.nextInt(9);
  40. sb.append(String.valueOf(sr));
  41. }
  42. return sb.toString();
  43. }}
  44. public static void testCreditPay() throws ParseException{
  45. String PUBLIC_KEY=TheardTest.testConsult();
  46. HttpClient client = new HttpClient();
  47. PostMethod method = new PostMethod("http://xxxxxxxx/xxxxxx/api/bankcard/credit/pay");
  48. String key=TheardTest.ramdonNum(6);
  49. String encryptkey = "0123456789"+key;
  50. String merchantAccount = "YB01000000078";
  51. //民生
  52. String cardNo ="XXXXXXXXX";
  53. String validthru="xxx7";
  54. String cvv2 = "6xx";
  55. String phone = "13xxxxxx5431";
  56. String orderId = "33hhkssseef3utyt2"+key;
  57. System.out.println(orderId);
  58. Integer transtime = (int)(System.currentTimeMillis()/1000);
  59. Integer currency = 156;
  60. String  amount = "50";
  61. String productcatalog = "1";
  62. String productName = "123";
  63. String productDesc = "小丸子";
  64. String userIp = "123.45.45.45";
  65. String identityId = "a";
  66. Integer identityType = 6;
  67. String other = "eeee";
  68. String data = "{\"merchantaccount\":\"" + merchantAccount
  69. +"\",\"cardno\":\"" + cardNo
  70. + "\",\"validthru\":\"" + validthru
  71. + "\",\"cvv2\":\"" + cvv2
  72. + "\",\"phone\":\"" + phone
  73. + "\",\"orderid\":\"" + orderId
  74. + "\",\"transtime\":" + transtime
  75. + ",\"currency\":" + currency
  76. + ",\"amount\":" + amount
  77. + ",\"productcatalog\":\"" + productcatalog
  78. + "\",\"productname\":\"" + productName
  79. + "\",\"productdesc\":\"" + productDesc
  80. + "\",\"userip\":\"" + userIp
  81. + "\",\"identityid\":\"" + identityId
  82. + "\",\"identitytype\":" + identityType
  83. + ",\"other\":\"" + other + "\"}";
  84. data = AES.encryptToBase64(data, encryptkey);
  85. try {
  86. method.setParameter("merchantaccount", merchantAccount);
  87. method.setParameter("data", data);
  88. method.setParameter("encryptkey", RSA_Encrypt.encrypt(encryptkey, PUBLIC_KEY));
  89. client.executeMethod(method);
  90. System.out.println(method.getStatusLine());
  91. String respStr = method.getResponseBodyAsString();
  92. System.out.println(respStr);
  93. String result = AES.decryptFromBase64(respStr, encryptkey);
  94. System.out.println(result);
  95. //                    自动退款
  96. Map<String, Object> mapjson=JsonUtil.parse(result);
  97. Object obj = mapjson.get("yborderid");
  98. String yborderid=obj.toString();
  99. System.out.println("流水号:"+yborderid);
  100. TheardTest.refundTeseCase(yborderid,merchantAccount,amount);
  101. method.releaseConnection();
  102. } catch (HttpException e) {
  103. // TODO Auto-generated catch block
  104. e.printStackTrace();
  105. } catch (IOException e) {
  106. // TODO Auto-generated catch block
  107. e.printStackTrace();
  108. } catch (Exception e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. }
  113. /**
  114. * 测试协商接口
  115. * @author shangwei
  116. * @return
  117. * @throws ParseException
  118. *
  119. */
  120. public static String testConsult() throws ParseException{
  121. String url = "http://xxxxxxx/xxxxxx/consult";
  122. HttpClient4 client =HttpClient4.createDefault();
  123. String data = "";
  124. Date d = new Date();
  125. Calendar now = Calendar.getInstance();
  126. now.setTime(d);
  127. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  128. String dateline=format.format(now.getTime());
  129. System.out.println(dateline);
  130. Date date = format.parse(dateline);
  131. String dates=date.getTime()/1000+"";
  132. System.out.println(dates);
  133. try {
  134. data = AES.encryptToBase64(dates, "12345678901234HH");
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. HttpParameter parameter = new HttpParameter();
  139. parameter.add("data", data);
  140. parameter.add("merchantaccount", "YB01000000078");
  141. HttpResp resp = new HttpResp();
  142. try{
  143. resp=client.doPost(url, parameter, "utf-8");
  144. String respStr= resp.getText("utf-8");
  145. System.out.println(respStr);
  146. String aes=AES.decryptFromBase64(respStr, "12345678901234HH");
  147. System.out.println(aes);
  148. return aes;
  149. }catch (HttpException e) {
  150. // TODO Auto-generated catch block
  151. e.printStackTrace();
  152. } catch (IOException e) {
  153. // TODO Auto-generated catch block
  154. e.printStackTrace();
  155. } catch (Exception e) {
  156. // TODO Auto-generated catch block
  157. e.printStackTrace();
  158. }
  159. client.shutdown();
  160. return "出错啦";
  161. }
  162. public static void refundTeseCase(String yborderid,String merchantAccount,String amount) throws IOException{
  163. String url ="http://xxxxxxxx/xxxxxxx/api/refund/direct";
  164. HttpClient4 client =HttpClient4.createDefault();
  165. HttpParameter parameter = new HttpParameter();
  166. parameter.add("merchantaccount", merchantAccount);
  167. parameter.add("merchantrefundid", "093032323");
  168. parameter.add("transorderid", yborderid);
  169. parameter.add("amount", amount);
  170. HttpResp rep;
  171. try {
  172. rep = client.doPost(url, parameter,"utf-8");
  173. System.out.println(rep.getStatusCode());
  174. System.out.println(rep.getText("utf-8"));
  175. }catch (Exception e) {
  176. // TODO Auto-generated catch block
  177. e.printStackTrace();
  178. }
  179. }
  180. //测试执行的循环体,根据线程数和循环次数的不同可执行多次,类似于LoadRunner中的Action方法
  181. public SampleResult runTest(JavaSamplerContext arg0) {
  182. results.sampleStart();
  183. try {
  184. testCreditPay();
  185. } catch (ParseException e) {
  186. // TODO Auto-generated catch block
  187. e.printStackTrace();
  188. }
  189. results.sampleEnd();
  190. results.setSuccessful(true);
  191. return results;
  192. }
  193. //结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法
  194. public void teardownTest(JavaSamplerContext arg0) {
  195. }
  196. }
package app;

import http.HttpClient4;
 import http.HttpParameter;
 import http.HttpResp;
 import http.JsonUtil;

import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Map;
 import java.util.Random;

import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.jmeter.config.Arguments;
 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
 import org.apache.jmeter.samplers.SampleResult;

import com.yeepay.g3.utils.common.encrypt.AES;
 import com.yibao.utils.des3.RSA_Encrypt;

public class TheardTest extends AbstractJavaSamplerClient{
 private SampleResult results;
 public void setupTest(JavaSamplerContext arg0) {
 results = new SampleResult();
 }
 public Arguments getDefaultParameters() {
 Arguments params = new Arguments();
 return params;
 }
 public static String ramdonNum(int num){
 Random rd = new Random();

StringBuilder sb = new StringBuilder("");
 if (num <1){
 return null;
 }
 else{
 for(int i=1;i<=num;i++){
 int sr=rd.nextInt(9);
 sb.append(String.valueOf(sr));

}
 return sb.toString();

}}

public static void testCreditPay() throws ParseException{
 String PUBLIC_KEY=TheardTest.testConsult();
 HttpClient client = new HttpClient();
 PostMethod method = new PostMethod("http://xxxxxxxx/xxxxxx/api/bankcard/credit/pay");
 String key=TheardTest.ramdonNum(6);
 String encryptkey = "0123456789"+key;
 String merchantAccount = "YB01000000078";
 //民生
 String cardNo ="XXXXXXXXX";
 String validthru="xxx7";
 String cvv2 = "6xx";
 String phone = "13xxxxxx5431";
 String orderId = "33hhkssseef3utyt2"+key;
 System.out.println(orderId);

Integer transtime = (int)(System.currentTimeMillis()/1000);
 Integer currency = 156;
 String  amount = "50";
 String productcatalog = "1";
 String productName = "123";
 String productDesc = "小丸子";
 String userIp = "123.45.45.45";
 String identityId = "a";
 Integer identityType = 6;
 String other = "eeee";

String data = "{\"merchantaccount\":\"" + merchantAccount
 +"\",\"cardno\":\"" + cardNo
 + "\",\"validthru\":\"" + validthru
 + "\",\"cvv2\":\"" + cvv2
 + "\",\"phone\":\"" + phone
 + "\",\"orderid\":\"" + orderId
 + "\",\"transtime\":" + transtime
 + ",\"currency\":" + currency
 + ",\"amount\":" + amount
 + ",\"productcatalog\":\"" + productcatalog
 + "\",\"productname\":\"" + productName
 + "\",\"productdesc\":\"" + productDesc
 + "\",\"userip\":\"" + userIp
 + "\",\"identityid\":\"" + identityId
 + "\",\"identitytype\":" + identityType
 + ",\"other\":\"" + other + "\"}";
 data = AES.encryptToBase64(data, encryptkey);
 try {
 method.setParameter("merchantaccount", merchantAccount);
 method.setParameter("data", data);
 method.setParameter("encryptkey", RSA_Encrypt.encrypt(encryptkey, PUBLIC_KEY));
 client.executeMethod(method);
 System.out.println(method.getStatusLine());
 String respStr = method.getResponseBodyAsString();
 System.out.println(respStr);
 String result = AES.decryptFromBase64(respStr, encryptkey);
 System.out.println(result);
 //                    自动退款
 Map<String, Object> mapjson=JsonUtil.parse(result);
 Object obj = mapjson.get("yborderid");
 String yborderid=obj.toString();
 System.out.println("流水号:"+yborderid);
 TheardTest.refundTeseCase(yborderid,merchantAccount,amount);
 method.releaseConnection();
 } catch (HttpException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (Exception e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

/**
 * 测试协商接口
 * @author shangwei
 * @return
 * @throws ParseException
 *
 */

public static String testConsult() throws ParseException{

String url = "http://xxxxxxx/xxxxxx/consult";
 HttpClient4 client =HttpClient4.createDefault();
 String data = "";
 Date d = new Date();
 Calendar now = Calendar.getInstance();
 now.setTime(d);
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String dateline=format.format(now.getTime());
 System.out.println(dateline);
 Date date = format.parse(dateline);
 String dates=date.getTime()/1000+"";
 System.out.println(dates);
 try {
 data = AES.encryptToBase64(dates, "12345678901234HH");
 } catch (Exception e) {
 e.printStackTrace();
 }
 HttpParameter parameter = new HttpParameter();
 parameter.add("data", data);
 parameter.add("merchantaccount", "YB01000000078");
 HttpResp resp = new HttpResp();
 try{
 resp=client.doPost(url, parameter, "utf-8");
 String respStr= resp.getText("utf-8");
 System.out.println(respStr);
 String aes=AES.decryptFromBase64(respStr, "12345678901234HH");
 System.out.println(aes);
 return aes;
 }catch (HttpException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (Exception e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 client.shutdown();
 return "出错啦";

}

public static void refundTeseCase(String yborderid,String merchantAccount,String amount) throws IOException{

String url ="http://xxxxxxxx/xxxxxxx/api/refund/direct";
 HttpClient4 client =HttpClient4.createDefault();
 HttpParameter parameter = new HttpParameter();
 parameter.add("merchantaccount", merchantAccount);
 parameter.add("merchantrefundid", "093032323");
 parameter.add("transorderid", yborderid);
 parameter.add("amount", amount);

HttpResp rep;
 try {
 rep = client.doPost(url, parameter,"utf-8");
 System.out.println(rep.getStatusCode());
 System.out.println(rep.getText("utf-8"));
 }catch (Exception e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

//测试执行的循环体,根据线程数和循环次数的不同可执行多次,类似于LoadRunner中的Action方法
 public SampleResult runTest(JavaSamplerContext arg0) {

results.sampleStart();
 try {
 testCreditPay();
 } catch (ParseException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 results.sampleEnd();
 results.setSuccessful(true);
 return results;
 }

//结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法
 public void teardownTest(JavaSamplerContext arg0) {
 }

}

3、将写好的class类和该类引入到的类都打成jar包,放到jemter文件的lib/ext文件下。将用到的其他的jar包放到lib文件夹下面。

4、启动jemter后,创建先添加线程组,在添加sampler的java请求,如果之前步骤成功,在类名称下拉列表里会出现测试代码打好jar包的类。之后操作jemter进行测试就OK啦。

时间: 2024-11-09 09:40:37

【转】性能测试分享---java协议(1)------jemter篇的相关文章

【转】性能测试分享---java vuser协议(2)---LoadRunner篇

转自:http://blog.csdn.net/weiweicao0429/article/details/8941137 LoadRunner性能测试工具使用: 1.新建脚本,选择java vuser协议.初步结构是 [java] view plaincopyprint? import lrapi.lr; public class Actions { //在init方法里面编写一个虚拟用户循环只执行一次的方法,如可以把协商写在里面,就可以达到每个用户协商一次之后,就不再协商的效果 public

分享Java web 开发必游之路

一.开始,万事开头难啊! Java的学习很难,因为它太庞杂了,有很多令人恐惧的(并非语言本身的)细节. 让我们先看看,我们将碰到哪些难关: 1. 表示 脸面的部分,这里将用到的技术和知识:JSP,Servlet,XML,HTML,JavaScript,CSS等,最好能了解网络基础(TCP/IP协议等). 2. 业务逻辑 核心的部分,这里将用到的技术和知识:Java Bean,XML等. 3. 数据库 存储的部分,这里将用到的技术和知识:数据库的知识,JDBC,XML等. 4. 部署 必备的部分,

Java RMI之HelloWorld篇

Java RMI之HelloWorld篇 Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方法调用的任何对象必须实现该远程接口. Java RMI不是什么新技术(在Java1.1的时代都有了),但却是是非常重要的底层技术. 大名鼎鼎的EJB都是建立在rmi基础之上的,现在还有一些开源的远程调用组件,其底层技术也是rmi. 在大力鼓吹Web Serv

java学习笔记 第二篇 核心技术(二)

第十四章 集合类 集合类用来存放对象的引用.继承关系如下图: 14.1 Collection 接口 是层次结构中的根接口,构成Collection的单位称为元素.Collection接口不能直接使用,但该接口提供了添加元素.删除元素.管理数据的方法. Collection接口常用方法: 14.2 List 集合 包括List接口以及List集合的所有实现类.List集合中的元素允许重复,各元素循序就是对象插入的顺序 1.List接口,两个重要方法: get(int index): 获取指定索引位

每天五个java相关面试题(10)--java基础详解篇2

好勒好勒.一起加油 一.HashMap和Hashtable的区别. 答: HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable. HashMap允许将null作为一个entry的key或者value,而Hashtable不允许. HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey.因

[Java 05 OO] (基础篇) 《Java开发实战经典》

p5OO 第五章 面向对象 (基础篇) Notes (1), Constructor / this / String   String str1 = "hello"; 解释 : 是把一个在堆内存空间的使用权给了 str1 对象.   String str2 = "hello"; str1 == str2 是 true   String 字符串的内容不可改变 (2), Java 常用的内存区域    1), 栈内存空间    2), 堆内存空间    3), 全局数据

学习java随笔第八篇:封装、继承、多态

java和c#一样都是面向对象的语言. 面向对象的语言有三大特征:封装.继承.多态 封装 封装:隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别. class Person2 { private String name; public void setName(String name) { this.name=name; } public String getName() { return name; } private String sex; public voi

Java学习之Servlet篇

<JAVA遇见HTML——Servlet篇> Servlet 生命周期:Servlet 加载--->实例化--->服务--->销毁. init():在Servlet的生命周期中,仅执行一次init()方法.它是在服务器装入Servlet时执行的,负责初始化Servlet对象.可以配置服务器,以在启动服务器或客户机首次访问Servlet时装入Servlet.无论有多少客户机访问Servlet,都不会重复执行init(). service():它是Servlet的核心,负责响应客

基于Netty的聊天系统(三)协议定制----消息篇

今天我们继续来讨论协议,今天基本就把一对一聊天的协议定制完毕了,上一篇我们讲述了登录的过程,那么登录完毕就是聊天了,首先我们还是以A和B为例子,A发送消息给B,那么这条消息的的协议如下 发送消息协议: {"id":"xxxx","#":"msg","text":"内容","to":"接收用户ID","type":0,"