wisdom-1983

今天有个接口压测,确认使用jmeter进行,接口以HTTP协议GET方式请求,比较简单

唯一有问题的地方在于,接口的参数需要算出一个MD5的值,以校验码的形式跟在参数后面,这就需要对jmeter进行扩展

 

测试类:

  1 package com.ieostek.test.imusic;
  2 
  3 
  4 import java.io.BufferedReader;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileReader;
  8 import java.io.IOException;
  9 import java.io.InputStreamReader;
 10 import java.io.LineNumberReader;
 11 import java.net.HttpURLConnection;
 12 import java.net.URL;
 13 
 14 
 15 
 16 
 17 public class Test {
 18     
 19     public String strUrl(String type_value, int tp_value ){
 20          String url_patem = "";
 21          long time = System.currentTimeMillis() /1000;
 22          System.out.println(time);
 23          String pgmId_value= getPgmId_value();
 24          url_patem = pgmId_value + "ifid=Playlists&type=" + 
 25                              type_value + "&tp=2&pgmId=" 
 26                              + pgmId_value + "&oper=0&time=" 
 27                              +time + time + time; 
 28          
 29          String md5url = MD5Tool.calcMD5(url_patem);
 30         return "http://172.23.67.77:8014/eos/clientService.jsp?"+
 31                      "ifid=Playlists"
 32                      +"&type=" + type_value 
 33                      +"&tp=2"
 34                      +"&pgmId="+pgmId_value 
 35                      +"&oper=0"
 36                     +"&time=" + time 
 37                     + "&sn=" +md5url;
 38     }
 39     
 40     
 41         
 42          public String  getConn(String urlstr){
 43             String result="";
 44             BufferedReader in = null;
 45             URL url;
 46             try {
 47                 url = new URL(urlstr);
 48                 HttpURLConnection conn=(HttpURLConnection)url.openConnection();
 49                 conn.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
 50                 conn.addRequestProperty("Accept-Language","zh-cn");
 51                 conn.setRequestMethod("GET");//提交模式        
 52                 conn.setReadTimeout(26*1000);
 53                 conn.setConnectTimeout(26*1000);
 54                 conn.setDoOutput(true);
 55                 conn.connect();
 56                 in = new BufferedReader(new InputStreamReader(
 57                         conn.getInputStream()));
 58                 String line=null;
 59                 while ((line = in.readLine()) != null) {
 60                     result += line;
 61                 }
 62                 
 63             } catch (IOException e) {
 64                 System.out.println("GET 请求参数异常,请检查您的参数设置");
 65                 e.printStackTrace();
 66             }finally{
 67                 if(in != null){
 68                     try {
 69                         
 70                         in.close();
 71                     } catch (IOException e) {
 72                         // TODO Auto-generated catch block
 73                         e.printStackTrace();
 74                     }
 75                 }
 76             }
 77             return result;
 78         
 79     }
 80          
 81         public String  getPgmId_value(){
 82             ReadSelectedLine rl = new ReadSelectedLine();
 83             String temp = "";
 84             // 指定读取的行号    
 85             int lineNumber =  (int)(Math.random()*300);  
 86             // 读取文件    
 87             File sourceFile = new   
 88                     File("D:\\All.csv");  
 89             // 读取指定的行    
 90             try {
 91                 String s=rl.readAppointedLineNumber(sourceFile, lineNumber);
 92                 while(s ==null|| s.equals("")){
 93                      s=rl.readAppointedLineNumber(sourceFile, lineNumber);
 94                 }
 95                 System.out.println(lineNumber+":"+s);
 96                 if(s!=null)
 97                 temp = s.substring(0, 12);
 98             } catch (IOException e) {
 99                 e.printStackTrace();
100             }
101             
102         //    System.out.println("---------temp---------"+temp);
103                 return temp;
104     
105             
106         }
107         
108         public void getType_value(){
109             
110         }
111          
112 
113 }

 

MD5算法:

  1 package com.ieostek.test.imusic;
  2 
  3 
  4 
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.IOException;
  8 import java.security.MessageDigest;
  9 import java.security.NoSuchAlgorithmException;
 10 
 11 
 12 
 13 /**
 14  * MD5 tools
 15  */
 16 public class MD5Tool {
 17 
 18 
 19     public static String calcMD5(String input) {
 20         return calcMD5(input.getBytes());
 21     }
 22 
 23     /**
 24      * MD5 encode
 25      *
 26      * @param data
 27      * @return
 28      */
 29     public static String calcMD5(byte[] data) {
 30         MessageDigest md = null;
 31         try {
 32             md = MessageDigest.getInstance("MD5");
 33         } catch (NoSuchAlgorithmException e) {
 34             return "";
 35         }
 36 
 37         // generate MessageDigest
 38         md.update(data);
 39         byte[] hash = md.digest();
 40 
 41         // translate to string
 42         StringBuffer sbRet = new StringBuffer();
 43         for (int i = 0; i < hash.length; i++) {
 44             int v = hash[i] & 0xFF;
 45             if (v < 16) sbRet.append("0");
 46             sbRet.append(Integer.toString(v, 16));
 47         }
 48 
 49         return sbRet.toString();
 50     }
 51     /**
 52      * MD5 encode
 53      *
 54      * @param data
 55      * @return
 56      */
 57     public static String calcMD5LowerCase(byte[] data) {
 58         
 59         String md5 = calcMD5(data).toLowerCase();
 60     
 61         return md5;
 62     }
 63     
 64     public static String calcMD5Lim(String input) {
 65         String result=calcMD5(input.getBytes()).toUpperCase();
 66         
 67         return result.substring(0,20);
 68     }
 69     
 70     public static String calcMD5Lim2(String input) {
 71         String result=calcMD5(input.getBytes());
 72         
 73         return result.substring(0,20);
 74     }
 75     
 76     /**
 77      * 适用于上G大的文件
 78      * 
 79      * @param file
 80      * @return
 81      * @throws IOException
 82      */
 83     public static String getFileMD5String(File file){
 84             FileInputStream is = null;
 85             MessageDigest digest = null;
 86             byte buffer[] = new byte[1024];
 87             int len;
 88             try {
 89                 digest = MessageDigest.getInstance("MD5");
 90                 is = new FileInputStream(file);
 91                 while ((len = is.read(buffer, 0, 1024)) != -1) {
 92                     digest.update(buffer, 0, len);
 93                 }
 94                 byte[] hash = digest.digest();
 95                 StringBuffer sbRet = new StringBuffer();
 96                 for (int i = 0; i < hash.length; i++) {
 97                     int v = hash[i] & 0xFF;
 98                     if (v < 16)
 99                         sbRet.append("0");
100                     sbRet.append(Integer.toString(v, 16));
101                 }
102                 return sbRet.toString();
103             } catch (Exception e) {
104                 e.printStackTrace();
105                 return null;
106             } finally {
107                 if (is != null) {
108                     try {
109                         is.close();
110                     } catch (IOException e) {
111                     }
112                 }
113             }
114     }
115     
116     public static String calcMD5(File file){
117         return getFileMD5String(file);
118     }
119 }

 

继承jmeterAbstractJavaSamplerClient  对jmeter进行调用

 1 /**
 2  * 
 3  */
 4 package com.ieostek.test.imusic;
 5 
 6 import java.net.URL;
 7 import java.util.Random;
 8 
 9 import org.apache.jmeter.config.Arguments;
10 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
11 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
12 import org.apache.jmeter.samplers.SampleResult;
13 
14 /**
15  * @author Administrator
16  *
17  */
18 public class OneTest extends AbstractJavaSamplerClient{      
19     Test test = new Test();
20     URL url ;
21     String urlstr = "";
22     public void setupTest(JavaSamplerContext arg0) {   
23         
24         urlstr=test.strUrl("OTTHD",1);
25     }   
26     public void teardownTest(JavaSamplerContext arg0) {   
27     }   
28     
29     public Arguments getDefaultParameters() {   
30             Arguments args = new Arguments();   
31             return args;   
32     }   
33     
34     @SuppressWarnings("deprecation")
35     public SampleResult runTest(JavaSamplerContext arg0) {   
36             SampleResult sr = new SampleResult();   
37             System.out.println(Thread.currentThread().getName()+","+this);
38             try {   
39                     // Start   
40                     sr.sampleStart();     
41                     System.out.println(urlstr);
42                     
43                     String result = test.getConn(urlstr);
44                     
45                     //设置结果树上的lable
46                     sr.setSampleLabel("Imusic Test Lable");
47                     //设置结果数上的请求显示
48                     sr.setSamplerData(urlstr);
49                    //设置结果数上的响应显示
50                     sr.setResponseData("结果是:"+result, null);
51                     
52                     
53                     
54                     // TODO                              
55                     sr.setSuccessful(true);   
56                     
57                    
58             } catch (Exception e) {   
59                  sr.setSuccessful(false);
60                  
61                     e.printStackTrace();   
62             }finally{

63                  // End   
64                 sr.sampleEnd();   
65             }
66             return sr;   
67     }  
68 }

 

分类:

技术点:

相关文章:

  • 2021-12-13
  • 2021-12-13
  • 2021-10-26
  • 2021-12-09
  • 2021-11-06
  • 2021-12-28
猜你喜欢
  • 2021-06-15
  • 2021-06-06
  • 2021-04-16
  • 2021-11-29
  • 2021-12-07
  • 2021-07-07
  • 2021-07-26
相关资源
相似解决方案