zoukankan      html  css  js  c++  java
  • 【转】性能测试分享---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。初步结构是

    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方法里。编写好的代码如下:

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

  • 相关阅读:
    js,jQuery 排序的实现,网页标签排序的实现,标签排序
    SQL不规则排序,ORDER BY 不规则排序,case的使用
    XML methods are not allowed in a GROUP BY clause.XML不允许使用排序方法的处理
    DropDownList设置选定项,设置选择项,最安全的方法
    js 字符串转成货币格式, js转货币
    排名进入20000纪念
    jQuery对象和Dom对象互相转换
    屏蔽页面JS错误,拦截页面JS错误的代码,js禁止跳出错误提示
    枚举,以及中文对应解释
    insert into插入表,插入多条数据,sql插入表,插入多条数据,数据库插入表,插入多条数据
  • 原文地址:https://www.cnblogs.com/xubc/p/4561179.html
Copyright © 2011-2022 走看看