564543 发表于 2016-7-22 10:31:24

Loadrunner进行http接口压力测试

附件来自百度。



脚本正文:
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*                     
*/

import lrapi.lr;

public class Actions
{

        public int init() throws Throwable {
                return 0;
        }//end of init


        public int action() throws Throwable {
          com.CTLPTest ct1 = new com.CTLPTest();
          ct1.main(null);
                return 0;
        }//end of action


        public int end() throws Throwable {
                return 0;
        }//end of end
}


jar包内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Properties;
import java.util.Random;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

public class CTLPTest
{
    public static void main(String[] args)
    {
      CTLPTest lbs = new CTLPTest();
      String ltpUrl = lbs.ltpRequestUrl();
      System.out.println("ltpUrl:"+ltpUrl);
      System.out.println("lbs.ltpRequestUrl(ltpUrl):"+lbs.ltpRequestUrl(ltpUrl));
    }
   

    public int ltpRequestUrl(String ltpRequestUrl)
    {
         
      int returnCount = -1;
      try
      {
            URL url = new URL(ltpRequestUrl);
            Properties prop = System.getProperties();
            System.setProperty("http.proxyHost", "proxy.com");
            System.setProperty("http.proxyPort", "80");
             //http
            HttpURLConnection http = (HttpURLConnection)url.openConnection();
            http.setUseCaches(false);
            http.connect();
             //http
            InputStream in = http.getInputStream();
            //
            byte[] b = new byte;
            in.read(b);
            //
            String res = new String(b);
            System.out.println("res:");
            System.out.println(res);

            //ж,÷
            int of = res.indexOf("resultCode");
            if (of < 0) {
                System.out.println("***************failure***********************");
                returnCount = -1;
            } else {
                returnCount = 1;
            }
      }catch(Exception e){
            System.out.println("Exception");
            System.out.println(e);
            returnCount = -1;
      }   
      return returnCount;   
      }   
         
      public String ltpRequestUrl() {
      StringBuilder param = new StringBuilder("http://xxx.xxx.com");
//      param.append("userid-1");
//      param.append("&appName=LBS&Apikey=D39hr1FgplZSjV2eNVW71wvbYbl8Mip4");
      return param.toString();
    }
}






页: [1]
查看完整版本: Loadrunner进行http接口压力测试