zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

ngrinder groovy 参数化--从数据库获取数据(以oracle数据库为例)

Oracle数据库 -- 参数 为例 Groovy 获取数据
2023-09-14 08:59:26 时间
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith

import java.util.Date
import java.util.List
import java.util.ArrayList

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.apache.commons.lang3.RandomStringUtils; //导入方法依赖的package包/类
import groovy.sql.Sql

/**
 * A simple example using the HTTP plugin that shows the retrieval of a
 * single page via HTTP. 
 * 
 * This script is automatically generated by ngrinder.
 * 
 * @author xiaochanchan
 */
@RunWith(GrinderRunner)
class TestRunner {

	public static GTest test
	public static HTTPRequest request
	public static NVPair[] headers = []
	public static NVPair[] params = []
	public static Cookie[] cookies = []
	
	public static List<String> LineList //存放参数化数据

	@BeforeProcess
	public static void beforeProcess() {
		HTTPPluginControl.getConnectionDefaults().timeout = 6000
		test = new GTest(1, "10.248.64.58")
		request = new HTTPRequest()
		//连接数据库
		Sql db= Sql.newInstance( 
			"jdbc:oracle:thin:@IP:port:dbname", //IP、port、dbname修改为真实的数据库IP、端口和数据库名称
			"username", //用户名
			"password", //密码
			"oracle.jdbc.driver.OracleDriver");
		
		//获取查询数据
		LineList= db.rows("select CUSTOMER_ID from customer.cc_customer where create_time >= to_date('20220518','yyyy/mm/dd') order by create_time desc").CUSTOMER_ID;
		
		//LineList = new File("./resources/get_customerID.csv").readLines("UTF8")
		//grinder.logger.info("LineList={}",LineList);
		grinder.logger.info("before process.");
	}

	@BeforeThread 
	public void beforeThread() {
		test.record(this, "test")
		grinder.statistics.delayReports=true;
		grinder.logger.info("before thread.");
	}
	
	@Before
	public void before() {
		request.setHeaders(headers)
		cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
		grinder.logger.info("before thread. init headers and cookies");
	}

	@Test
	public void test(){
		def gtime=new Date().format('yyyyMMddHHmmssSSS')//生成17位时间戳
		//String getStr=RandomStringUtils.randomAlphanumeric(24)//获取24位长的字母和数字随机字符串
		int testcount=grinder.getProperties().getInt("grinder.runs",1);  //测试总数
		int total_processes=grinder.getProperties().getInt("grinder.processes",1);  //总进程数
		int total_threads=grinder.getProperties().getInt("grinder.threads",1);  //总线程数
		grinder.logger.info("total_runs={},total_processes={},total_threads={}",testcount,total_processes,total_threads)
		
		String code=grinder.agentNumber.toString()+grinder.processNumber.toString()+grinder.threadNumber.toString()+grinder.runNumber.toString()
		int Listindex=grinder.processNumber*total_threads+grinder.threadNumber+total_processes*total_threads*grinder.runNumber;// 取在数组中的序号
		String customerID = LineList.get(Listindex).toString()
		grinder.logger.info("code={},customerID={}",code,customerID)
		
		//定义请求报文
		String req_xml="<CustomerNumber>${customerID}</CustomerNumber><OrderNumber>${code}JCKL${gtime}${RandomStringUtils.randomAlphanumeric(20)}</OrderNumber>"
		grinder.logger.info("请求报文={}",req_xml)
		
		HttpClient client = new DefaultHttpClient();
		HttpPost post_request = new HttpPost("http://IP:post/targets"); //URL修改为请求的真实IP和端口
		StringEntity entityParams = new StringEntity(req_xml,"utf-8");
		entityParams.setContentType("text/xml");//设置请求头数据传输格式
		post_request.setEntity(entityParams); //设置post请求实体
		HttpResponse response = client.execute(post_request);  //发送http请求
		HttpEntity resEntity = response.getEntity();  
		String responseXML = EntityUtils.toString(resEntity, "utf-8");
		grinder.logger.info("响应报文={}",responseXML);//打印响应报文
		
		//获取响应状态码和描述
		def rootNode = new XmlParser().parseText(responseXML);
		String RspDesc=rootNode.order_content.order_resp.InterBOSS.Response.RspDesc.text();
		String RspCode=rootNode.order_content.order_resp.InterBOSS.Response.RspCode.text();
		String resp_code=rootNode.order_content.resp_code.text();
		String resp_desc=rootNode.order_content.resp_desc.text();
		//断言
		if(RspCode != '0000'){
			grinder.logger.error("接口响应失败>>>resp_code:{},resp_desc:{},RspCode:{},RspDesc:{}",resp_code,resp_desc,RspCode,RspDesc);
			assertThat("判断响应结果:",RspCode, is("0000"));
		}		
		
	}
}