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

用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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值