業務描述:
在業務系統里進行查詢操作,查詢的結果是通過請求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,如下代碼

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; } }

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) { } } }
2.將測試類導出為jar包 : carlevel.jar
3.Loadrunner創建java測試類
創建java協議腳本
添加代碼后:
4.設置環境變量
5.設置安裝的jdk位置目錄
6.導入jar包
7.試運行,查看結果
說明:至此,完成了java腳本的編寫和基本測試,接下來我們就可以使用loadrunner的進行壓力測試了。
四.設置場景、運行場景
略
五.分析
略
小結:
一直都只會用jmeter對http接口進行測試,今天剛好有時間,就順便學習下用loadrunner如何進行http接口壓力測試,並嘗試成功,就留下筆記,以便后續工作中使用。