使用Loadrunner进行http接口压力测试

时间:2020-12-19 23:19:39

业务描述

  在业务系统里进行查询操作,查询的结果是通过请求http接口,从系统中处理并将结果以json字符串返回。

 

 本文就讲述使用Loadrunner对此类接口进行压力测试并记录相关的性能指标数据:

一.安装Loadrunner

本次测试过程使用Loadrunner 11.0版本。

二.部署环境

1.接口服务器一台;

2.用于运行Loadrunner的压力测试机1台或N台 ,在条件允许下,尽可能提供高配置的CPU 和内存。

3.接口服务器和压力测试机建议应部署于同一个局域网内,否则测试过程和结果将受到网络带宽因素的影响无法顺利进行。

三.编写测试脚本

方法一. 通过java编写测试类,以jar包的方式引入Loadrunner进行测试。

优点:便于解析接口响应结果,同时避免由于LR脚本编写不规范或配置问题,导致测试过程引发的未知错误。

条件:运行loadrunner的机器需要安装jdk1.6的版本。

1.编写java测试类: CTLPTest.java,如下代码

使用Loadrunner进行http接口压力测试使用Loadrunner进行http接口压力测试
package Demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;

public class HttpRequestor {
    
    private String charset = "utf-8";
    private Integer connectTimeout = null;
    private Integer socketTimeout = null;
    private String proxyHost = null;
    private Integer proxyPort = null;
    
    /**
     * Do GET request
     * @param url
     * @return
     * @throws Exception
     * @throws IOException
     */
    public int doGet(String url) throws Exception {
        
        int returnCount = -1;
        
        URL localURL = new URL(url);
        
        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
        
        httpURLConnection.setRequestProperty("Authorization", "Bearer 902c001a-a4d5-4f4c-8b44-661e7242787b");
        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        
        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }
        
        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            
            //将响应流转换成字符串
            String res = new String(resultBuffer.toString());
            //根据实际情况,判断响应结果,并设置返回值
            boolean of = res.contains("\"code\":1");
            if (of) {
                returnCount = 1;
            } else {
                returnCount = -1;
            }

        }
        catch(Exception e)
        {
             returnCount = -1;            
        }
        finally {
            
            if (reader != null) {
                reader.close();
            }
            
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            
            if (inputStream != null) {
                inputStream.close();
            }
            
        }
        System.out.println(resultBuffer.toString());
        
        return returnCount;
    }
    
    /**
     * Do POST request
     * @param url
     * @param parameterMap
     * @return
     * @throws Exception 
     */
    public int doPost(String url, Map parameterMap) throws Exception {
        
        int returnCount = -1;
        
        /* Translate parameter map to parameter date string */
        StringBuffer parameterBuffer = new StringBuffer();
        if (parameterMap != null) {
            Iterator iterator = parameterMap.keySet().iterator();
            String key = null;
            String value = null;
            while (iterator.hasNext()) {
                key = (String)iterator.next();
                if (parameterMap.get(key) != null) {
                    value = (String)parameterMap.get(key);
                } else {
                    value = "";
                }
                
                parameterBuffer.append(key).append("=").append(value);
                if (iterator.hasNext()) {
                    parameterBuffer.append("&");
                }
            }
        }
        
        System.out.println("POST parameter : " + parameterBuffer.toString());
        
        URL localURL = new URL(url);
        
        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
        
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept-Charset", charset);
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterBuffer.length()));
        httpURLConnection.setRequestProperty("Authorization", "Bearer 902c001a-a4d5-4f4c-8b44-661e7242787b");
        
        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        
        try {
            outputStream = httpURLConnection.getOutputStream();
            outputStreamWriter = new OutputStreamWriter(outputStream);
            
            outputStreamWriter.write(parameterBuffer.toString());
            outputStreamWriter.flush();
            
            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }
            
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
            
            //将响应流转换成字符串
            String res = new String(resultBuffer.toString());
            //根据实际情况,判断响应结果,并设置返回值
            boolean of = res.contains("\"code\":1");
            if (of) {
                returnCount = 1;
            } else {
                returnCount = -1;
            }
            
        } finally {
            
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            
            if (outputStream != null) {
                outputStream.close();
            }
            
            if (reader != null) {
                reader.close();
            }
            
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            
            if (inputStream != null) {
                inputStream.close();
            }
            
        }

        System.out.println(resultBuffer.toString());
        
        return returnCount;
    }

    private URLConnection openConnection(URL localURL) throws IOException {
        URLConnection connection;
        if (proxyHost != null && proxyPort != null) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            connection = localURL.openConnection(proxy);
        } else {
            connection = localURL.openConnection();
        }
        return connection;
    }
    
    /**
     * Render request according setting
     * @param request
     */
    private void renderRequest(URLConnection connection) {
        
        if (connectTimeout != null) {
            connection.setConnectTimeout(connectTimeout);
        }
        
        if (socketTimeout != null) {
            connection.setReadTimeout(socketTimeout);
        }
        
    }

    /*
     * Getter & Setter
     */
    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public Integer getSocketTimeout() {
        return socketTimeout;
    }

    public void setSocketTimeout(Integer socketTimeout) {
        this.socketTimeout = socketTimeout;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public Integer getProxyPort() {
        return proxyPort;
    }

    public void setProxyPort(Integer proxyPort) {
        this.proxyPort = proxyPort;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }
    
}
HttpRequestor类
使用Loadrunner进行http接口压力测试使用Loadrunner进行http接口压力测试
package Demo;


import java.util.HashMap;
import java.util.Map;


public class CTLPTest {

    
    /**
     * @param args
     */
    public static void main(String[] args) {
         
        try
        {
        /* Get Request */
        System.out.println(new HttpRequestor().doGet("http://10.201.76.152:8080/order/carlevel/list"));
        
        
        /* 登录 */
        Map dataMap = new HashMap();
        dataMap.put("client_type", "2");
        dataMap.put("username", "13200010001");
        dataMap.put("password", "174696");
        System.out.println(new HttpRequestor().doPost("http://10.201.76.152:8080/oauth/oauthlogin/verify/login", dataMap));
        
        
        
        }catch(Exception ex)
        {
            
        }
    }
    
}
CTLPTest类

 

2.将测试类导出为jar包 : carlevel.jar
使用Loadrunner进行http接口压力测试

 

3.Loadrunner创建java测试类

使用Loadrunner进行http接口压力测试

创建java协议脚本

使用Loadrunner进行http接口压力测试

使用Loadrunner进行http接口压力测试

使用Loadrunner进行http接口压力测试

添加代码后:

使用Loadrunner进行http接口压力测试

4.设置环境变量

使用Loadrunner进行http接口压力测试

5.设置安装的jdk位置目录

使用Loadrunner进行http接口压力测试

6.导入jar包

使用Loadrunner进行http接口压力测试

7.试运行,查看结果

使用Loadrunner进行http接口压力测试

使用Loadrunner进行http接口压力测试

 

说明:至此,完成了java脚本的编写和基本测试,接下来我们就可以使用loadrunner的进行压力测试了。

 

四.设置场景、运行场景

 

五.分析

 

小结

  一直都只会用jmeter对http接口进行测试,今天刚好有时间,就顺便学习下用loadrunner如何进行http接口压力测试,并尝试成功,就留下笔记,以便后续工作中使用。

 

java工程附件下载