ngrinder groovy 引數化--從資料庫獲取資料(以oracle資料庫為例)
阿新 • • 發佈:2022-05-18
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")); } } }