ngrinder groovy 参数化--从数据库获取数据(以oracle数据库为例)
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")); } } }
相关文章
- Oracle数据库提权(低权限提升至dba)
- Oracle授权允许远程访问--Oracle配置允许远程连接
- oracle SQL Develop导出数据库中的表格数据到excel
- 【性能优化】ORACLE数据库性能优化概述
- .NET(C#) Dapper Oracle(ODP.NET)或SQL Server 执行多条查询(select)语句的方法代码
- PostgreSQL Oracle 兼容性之 - 字符编码转换 CONVERT
- cx_Oracle连接oracle数据库
- Oracle数据库 External component has thrown an exception
- Oracle :一次数据库连接,返回多个结果集
- Oracle ORA-12899
- Atitit 为什么oracle这类大型数据库比mysql的性能机制目录1. 分区机制差别 11.1. Join算
- jdbc应用程序连接Oracle rac的URL写法:
- 数据库MySQL、Oracle、SQLServer、db2分页查询语句
- 【项目实战】如何在关系型数据库(Oracle、MySQL)中创建定时任务
- Oracle Bills of Material and Engineering Application Program Interface (APIs)
- Oracle入门捷径之--结合docker快速搭建学习或者测试数据库环境
- Linux下的Oracle的数据库备份与恢复(emp和imp命令)
- Oracle数据库用户管理之二---权限授权去权(用户和角色)
- Oracle 19c big_table的创建
- 【数据库管理】①② Oracle逻辑存储架构(上)
- Oracle的学习心得和知识总结(七)|Oracle数据库Literals技术详解