您的位置:首页 > 理论基础 > 计算机网络

LoadRunner做基于http协议的接口测试

2016-02-01 12:15 513 查看
用LoadRunner实现http协议数据的发送,支持Java的语法,需要32位1.6版本的JDK才能访问,否则会出现一些莫名的错误,本人已经经历过

依赖包:

httpclient-4.3.3.jar、httpcore-4.3.2.jar、commons-logging-1.1.1.jar

1.需要在LoadRunner中进行配置JDK的运行环境及包的需要的包导入

2.编写需要测试的接口的java代码:

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import lrapi.lr;

public class Actions {
CloseableHttpClient httpclient=null;
CloseableHttpResponse response=null;

public int init() throws Throwable {

return 0;
}

public int action() throws Throwable {

String host="192.168.1.41";
int port=20220;

RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).setCookieSpec(CookieSpecs.BEST_MATCH).setExpectContinueEnabled(true).build();
httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
lr.start_transaction("schedule");

String url="http://211.144.118.118:29008/solr/NewHouseProject/select?q=*:*&wt=json&indent=true";

String response=Http_Get(httpclient,url);
System.out.println("response"+response);
if(response.contains("status\":0")){
lr.end_transaction("schedule", lr.PASS);
}else{
lr.end_transaction("schedule",lr.FAIL);
}

lr.think_time(1);
return 0;
}// end of action

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

public  String Http_Get(CloseableHttpClient client,String url){

try {
//创建 httpGet 的实例
HttpGet httpGet = new HttpGet(url);
//设置报文头以及格式
httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36");
httpGet.setHeader("Accept", "*/*");
httpGet.setHeader("Accept-Language", "zh-CN");
httpGet.setHeader("Accept-Encoding", "gzip,utf-8,deflate");

//执行get请求 ,返回Response,包含响应头和响度应体
response=client.execute(httpGet);
HttpEntity entity = response.getEntity();
String ss = EntityUtils.toString(entity,"utf-8");
return ss;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(response != null){
response.close();
}
if(httpclient != null){
httpclient.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}

return null;

}
}
执行后的结果:

如果需要测试单个接口的性能时候,可以把服务器的返回及日志输出及System.out.println进行关闭,具体对性能进行压测的时候,单个接口就是一个场景,场景的直接到达最大的人数,然后持续5分钟进行查看运行的指标:(服务端:CPU/内存、java资源监控(jdk自带的工具)、ResponseTime、Transaction per second)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: