arch.ruaniz.com

Scott.Lee, Application Architect

nGrinder : Groovy Script

Create a folder

library/

Create a script

check ‘Create Lib and Resource folders’ library/lib library/resources

librarylibrary/loadProductByNo.inc.groovy

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 net.grinder.scriptengine.groovy.junit.annotation.AfterProcess
// 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 org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;

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 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 admin
 */
@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 Sql sql

	public static int totalProcessCount;
	public static int totalThreadCount;

	public static BigInteger lastProductNo = 0

	public static String userID = "developer";

	@BeforeProcess
	public static void beforeProcess() {
		HTTPPluginControl.getConnectionDefaults().timeout = 60000
		test = new GTest(1, "product.load.test")
		request = new HTTPRequest()
		
		sql = Sql.newInstance('jdbc:mysql://172.20.0.106:3306/dreamsearch?autoReconnect=true', 'dreamsearch', 'dnjvld$%^', 'org.mariadb.jdbc.Driver' )
		//sql = Sql.newInstance('jdbc:mysql://172.20.0.104:13306/esdb?autoReconnect=true', 'es', 'es', 'org.mariadb.jdbc.Driver' )
		
		totalProcessCount = grinder.getProperties().getInt("grinder.processes", 1);
		totalThreadCount = grinder.getProperties().getInt("grinder.threads", 1);

		grinder.logger.info("before process.");
	}
	@AfterProcess
	public static void afterProcess() {
		grinder.logger.warn("lastProductNo = {}", lastProductNo);
		
		grinder.logger.info("after process.");
	}

	@BeforeThread 
	public void beforeThread() {
		// Adjust logger to error level
        LoggerFactory.getLogger("worker").setLevel(Level.WARN)

		test.record(this, "test")
		grinder.statistics.delayReports=true;
		grinder.logger.info("before thread.");
	}
	
	@Before
	public void before() {
		// Set header to json
		List<NVPair> headerList = new ArrayList<NVPair>()
		headerList.add(new NVPair("Content-Type", "application/json"))
		headers = headerList.toArray()
		request.setHeaders(headers)

		/*
		cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
		grinder.logger.info("before thread. init headers and cookies");
		*/
	}

	@Test
	public void test(){
		String requestUrl = "http://tk.mediacategory.com/aggregate/mssg/product"
		//String requestUrl = "http://192.168.100.12:8080/default/dspt/product"
		//String requestUrl = "http://192.168.100.50:8080/default/dspt/product"

		int pageCount = 1000000;

		int baseProductNo = lastProductNo + pageCount * grinder.getRunNumber();
		int fromProductNo = baseProductNo + pageCount * grinder.getThreadNumber() / totalThreadCount + 1;
		int toProductNo   = baseProductNo + pageCount * (grinder.getThreadNumber() + 1) / totalThreadCount;

		grinder.logger.warn("pageCount/baseProductNo/fromProductNo/toProductNo = {}/{}/{}/{}", pageCount, baseProductNo, fromProductNo, toProductNo);
		
		String query = """SELECT NO FROM SHOP_DATA WHERE NO BETWEEN $fromProductNo AND $toProductNo""";
		sql.eachRow(query) { //$it.USERID
			lastProductNo = it.NO;
			String productDataStr = """{
				"adverId" : "$userID",
				"productCode": "$it.PCODE",
				"productName" : "$it.PNM",
				"price" : "$it.PRICE",
				"productUrl" : "$it.purl",
				"imageUrl" : "$it.IMGPATH",
				"nation" : "KR",
				"device" : "B",
				"domain": "$it.url",
				"topCategory" : "$it.CATE1",
				"firstSubCategory" : "$it.CATE2",
				"secondSubCategory" : "$it.CATE3",
				"thirdSubCategory" : "$it.CATE4"
				}""";
			try{
				HTTPResponse result = null;
				
				//product insert
				result = request.POST(requestUrl, productDataStr.getBytes())
				
				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}
/*
				//광고주 상품조회
				result = request.GET(requestUrl+"/$userID")
				
				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}

				//상품조회
				result = request.GET(requestUrl+"/$userID/$it.PCODE/KR/W")
				
				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}

				//광고주상품카운트
				result = request.GET(requestUrl+"count?adverId=$userID&status=Y&liveStatus=Y")

				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}
*/
			}catch(Exception e) {
				grinder.logger.error("Error occured : " + e.toString());
				fail();
			}

		}

	}
}
librarylibrary/loadProductByNo.groovy
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 net.grinder.scriptengine.groovy.junit.annotation.AfterProcess
// 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 org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;

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 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 admin
 */
@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 Sql sql

	public static int requestIdx = 0
	public static BigInteger firstProductNo = 4870237955
	public static BigInteger lastProductNo = 0

	public static String userID = "developer";

	@BeforeProcess
	public static void beforeProcess() {
		HTTPPluginControl.getConnectionDefaults().timeout = 60000
		test = new GTest(1, "product.load.test")
		request = new HTTPRequest()
		
		sql = Sql.newInstance('jdbc:mysql://172.20.0.106:3306/dreamsearch?autoReconnect=true', 'dreamsearch', 'dnjvld$%^', 'org.mariadb.jdbc.Driver' )
		//sql = Sql.newInstance('jdbc:mysql://172.20.0.104:13306/esdb?autoReconnect=true', 'es', 'es', 'org.mariadb.jdbc.Driver' )

		grinder.logger.info("before process.");
	}
	@AfterProcess
	public static void afterProcess() {
		grinder.logger.warn("lastProductNo = {}", lastProductNo);
		
		grinder.logger.info("after process.");
	}

	@BeforeThread 
	public void beforeThread() {
		// Adjust logger to error level
        LoggerFactory.getLogger("worker").setLevel(Level.WARN)

		test.record(this, "test")
		grinder.statistics.delayReports=true;
		grinder.logger.info("before thread.");
	}
	
	@Before
	public void before() {
		// Set header to json
		List<NVPair> headerList = new ArrayList<NVPair>()
		headerList.add(new NVPair("Content-Type", "application/json"))
		headers = headerList.toArray()
		request.setHeaders(headers)

		/*
		cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
		grinder.logger.info("before thread. init headers and cookies");
		*/
	}

	@Test
	public void test(){
		String requestUrl = "http://product.mobon.net:99/dspt/product"
		//String requestUrl = "http://192.168.100.12:8080/default/dspt/product"
		//String requestUrl = "http://192.168.100.50:8080/default/dspt/product"

		int pageCount = 100;
		int pageOffset = pageCount * requestIdx++;

		grinder.logger.warn("requestIdx/lastProductNo/firstProductNo/pageCount/pageOffset/userID = {}/{}/{}/{}/{}/{}",requestIdx, lastProductNo, firstProductNo, pageCount, pageOffset, userID);
		
		String query = """
			SELECT * FROM SHOP_DATA SD INNER JOIN (
						 SELECT NO FROM SHOP_DATA WHERE NO > $firstProductNo ORDER BY NO LIMIT $pageOffset, $pageCount
					 ) IX ON SD.NO = IX.NO""";
		sql.eachRow(query) { //$it.USERID
			lastProductNo = it.NO;
			String productDataStr = """{
				"productCode": "$it.PCODE",
				"adverId" : "$userID",
				"productName" : "$it.PNM",
				"price" : "$it.PRICE",
				"category" : "$it.CATE1",
				"productUrl" : "$it.purl",
				"imageUrl" : "$it.IMGPATH",
				"nation" : "KR",
				"device" : "W",
				"domain": "$it.url",
				"status": "$it.STATUS",
				"liveStatus" : "$it.liveChk",
				"kakaoStatus" : "$it.kakao_status"
				}""";

			try{
				HTTPResponse result = null;
				
				//product insert
				result = request.POST(requestUrl, productDataStr.getBytes())
				
				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}
/*
				//광고주 상품조회
				result = request.GET(requestUrl+"/$userID")
				
				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}

				//상품조회
				result = request.GET(requestUrl+"/$userID/$it.PCODE/KR/W")
				
				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}

				//광고주상품카운트
				result = request.GET(requestUrl+"count?adverId=$userID&status=Y&liveStatus=Y")

				if (result.statusCode == 301 || result.statusCode == 302) {
					grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
				} else {
					//assertThat(result.statusCode, is(200));
				}
*/
			}catch(Exception e) {
				grinder.logger.error("Error occured : " + e.toString());
				fail();
			}

		}

	}
}

Upload script or resources

library/lib/mariadb-java-client-2.4.3.jar