0
点赞
收藏
分享

微信扫一扫

nGrinder实现多接口性能测试

蓝哆啦呀 2022-04-05 阅读 26
压力测试

       编写两个场景的发送请求,在同一个test中运行。

       下文的示例是在Cookie指定的情况下,完成外勤签到和签退操作。外勤签到生成了外勤ID,根据外勤ID去签退。一些敏感信息使用了XXXXX来代替,可以自行修改。

       发送签到和签退是使用xml格式,里面包了一层json,所以脚本看起来稍麻烦些,看看思路即可。

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 groovy.json.JsonSlurper
import groovy.json.JsonOutput

/**
  外勤签到
  @author anfs2020
 */
@RunWith(GrinderRunner)
class TestRunner {
	public static GTest test1
	public static GTest test2
	public static HTTPRequest request
	public static NVPair[] headers = []
	public static NVPair[] params = []
	public static Cookie[] cookies = []
	public static String fsToken
	def parser_json,parser_xml,postID,fieldworkID,url,jString,json,jsonString,requestString,response,responseJson

	@BeforeProcess
	public static void beforeProcess() {
		HTTPPluginControl.getConnectionDefaults().timeout = 6000
		test1 = new GTest(1, "createFieldwork")
		test2 = new GTest(2, "fieldworkCheckout")
		request = new HTTPRequest()
		// Set header datas
		List<NVPair> headerList = new ArrayList<>()
		headerList.add(new NVPair("Content-Type", "application/xml; charset=UTF-8"))
		headers = headerList.toArray()
		
		// Set cookie datas
		List<Cookie> cookieList = new ArrayList<>()
		def cookieDate = new Date(32503647599000L)
		def cookieDomain = '.XXXXXX.com'
		def fsAuth = '0G60zbqOTu400005JxQsscuMqiM8FWvDeCMZ2OTzE2S1rnBVz2uxAh8mrU5gnoGER9vygRpsITFpWHzFQ8R8JKKBCCC7nICPREGelkLqaesvv1RqFHyFgO2eF8OEh5OSauiuEtKeNJIPOmhyvBS4Rzxec4Rz6eKpBmuFGxld4Rl8cdwfkJMZKuEOVhdSAupkgZmFy3J8t0U7MBSyMz0wyWypTVPWSzvpQ2Rnvy166BjU7bECxwwffkeY5xW8'
		cookieList.add(new Cookie("FSAuthX", fsAuth, cookieDomain, "", cookieDate, false))
		cookieList.add(new Cookie("FSAuthXC", fsAuth, cookieDomain, "", cookieDate, false))
		cookies = cookieList.toArray()
		grinder.logger.info("before process.");
	}

	@BeforeThread 
	public void beforeThread() {
		test1.record(this, "createFieldwork")
		test2.record(this, "fieldworkCheckout")
		grinder.statistics.delayReports=true;
		grinder.logger.info("before thread.");
	}
	
	//get random number
	public static String getRandomNumber(int length) {
		String val = "";
		Random random = new Random();
		for(int i = 0; i < length; i++) {
			val += String.valueOf(random.nextInt(10));
		}
		return val;
	}
	
	//get current time
	public static String getCurrentTime() {
		def time = new Date().time
		return time;
	}
	
	@Before
	public void before() {
		request.setHeaders(headers)
		cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
	}

	@Test
	public void test(){

		//外勤签到
		createFieldwork()
		
		//外勤签退
		fieldworkCheckout()
	}
	
	//外勤签到
	public void createFieldwork(){
		postID = getRandomNumber(10)
		url = 'https://www.XXXXX.com/FHE/XXXXXX/XXXXXX/createCheckin/iOS.795001?_vn=795001'
		jString = '{ "M12" : "中国", "M41" : 0, "M17" : "温泉花园XX号楼", "M4" : "", "M38" : "", "M21" : "", "M13" : "北京市", "M42" : 0, "M34" : 1649157390000, "M18" : 0, "M39" : "", "M30" : "2f8d3049651fa5502d97adb65de08d72", "M22" : "5db69107122c60523d350c5b", "M14" : "北京市", "M43" : 0, "M27" : 0, "M19" : "", "M10" : 116.41649627685547, "M23" : "", "M15" : "昌平区", "M8" : "iPhone",  "M11" : 40.129558563232422, "M40" : 0, "M24" : 0, "M16" : "温泉花园XX号楼", "M37" : "", "M20" : "王XX" }'
		//把String转为json,用来赋值
		parser_json = new JsonSlurper()
		json = parser_json.parseText(jString)
		//json串赋值
		json.M34 = getCurrentTime()
		json.M30 = getRandomNumber(32)
		//把json转回String
		jsonString = JsonOutput.toJson(json)
		requestString = '<FHE><Tickets /><PostId>' + postID + '</PostId><Data DataType="Json/P">' + jsonString + '</Data></FHE>'
		byte[] body = requestString.getBytes()
		HTTPResponse result = request.POST(url, body, params)
		grinder.logger.info("Request Body is:{}", requestString)
		//获取Response,先解析xml再把Data里的数据转为String
		parser_xml = new XmlSlurper()
		response = parser_xml.parseText(result.getText()).toString()
		grinder.logger.info("Response is:{}", response)
		//把String转json,用来解析外勤ID
		responseJson = parser_json.parseText(response)
		fieldworkID = responseJson.M10
		grinder.logger.info("FieldworkID is:{}", fieldworkID)
	}
	
	//外勤签退
	public void fieldworkCheckout(){
		postID = getRandomNumber(10)
		url = 'https://www.XXXXXX.com/FHE/XXXXXX/XXXXXX/createCheckOut/iOS.795001?_vn=795001'
		jString = '{"M10":"624b0b52504cdb632ab8ac05","M13":"116.3323170","M14":"39.97707","M15":"中国","M16":"北京市","M17":"北京市","M18":"海淀区","M19":"知春路XXX号","M20":"XXX大厦","M8":"QAPhone-API"}'
		//把String转为json,用来赋值
		parser_json = new JsonSlurper()
		json = parser_json.parseText(jString)
		//json串赋值
		json.M10 = fieldworkID
		//把json转回String
		jsonString = JsonOutput.toJson(json)
		requestString = '<FHE><Tickets /><PostId>' + postID + '</PostId><Data DataType="Json/P">' + jsonString + '</Data></FHE>'
		byte[] body = requestString.getBytes()
		HTTPResponse result = request.POST(url, body, params)
		grinder.logger.info("Request Body is:{}", requestString)
		//获取Response,先解析xml再把Data里的数据转为String
		parser_xml = new XmlSlurper()
		response = parser_xml.parseText(result.getText()).toString()
		grinder.logger.info("Response is:{}", response)
	}
}

    最终运行后的界面如下图:

 

举报

相关推荐

0 条评论