NGrinder Post request script of performance tool

Article Directory

I. Introduction

In the actual pressure test work, Post requests are divided into the two most common parameter transmission situations:

The first is by key-valueway of the parameters passed

  • The head parameters are:Content-Type:application/x-www-form-urlencoded

The second is to pass parameters through Json

  • The head parameters are:Content-Type:application/json

The prerequisite is that the source code is successfully deployed. In this respect, let's directly add the script we debugged in the script position of the source code. Let's use two methods as examples to introduce separately.

Two, write test services

Add the following code to the Controller layer of the SpringBoot project on the server side of the simulation request:

@PostMapping("/findinfo")
@ResponseBody
public List<UserTable> findUserPost(UserTable userInfo) {
    List<UserTable> UserInfo = userService.findinfo(userInfo);
    return UserInfo;
}

If you don’t understand the above code, please refer to:

Three, write test scripts

1. Key-value method

Reference Code:

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.
* // You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat

/**
 * @Title: PostDemo
 * @Description: post请求
 * @author 7DGroup
 * @date 2019/10/24 / 12:22
 */

@RunWith(GrinderRunner)
class PostDemo {
	public static GTest test
	public static HTTPRequest request
	public static NVPair[] headers = []
	public static NVPair[] params = []
	public static Cookie[] cookies = []


	@BeforeProcess
	public static void beforeProcess() {
		HTTPPluginControl.getConnectionDefaults().timeout = 6000
		test = new GTest(1, "localhost:8888")
		request = new HTTPRequest()
		
		// Set header datas
		List<NVPair> headerList = new ArrayList<NVPair>()
		
		//POST key/value格式的params
		headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
		headers = headerList.toArray()

		// Set param datas
		List<NVPair> paramList = new ArrayList<NVPair>()
		paramList.add(new NVPair("username", "600128"))
		params = paramList.toArray()
		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() {
		HTTPResponse result = request.GET("http://localhost:8888/findinfo", params)
		def text = result.getText()
		grinder.logger.info(text)
		assertThat(result.statusCode, is(200))
	}
}

Be careful not to forget to modify the startup parameters:

-javaagent:D:\maven\repository\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar
Insert picture description here

Click to run:

Insert picture description here

operation result:

Insert picture description here

2. Json request method

Before testing, simulate the server that can send Json requests, add a method to the Controler layer and use the annotation that can parse the Json method: @RequestBody

The specific code is:

**
 * json请求
 * @param userInfo
 * @return
 */
@PostMapping("/findinfoJson")
@ResponseBody
public List<UserTable> findUserPostJson(@RequestBody UserTable userInfo) {
    List<UserTable> UserInfo = userService.findinfo(userInfo);
    return UserInfo;
}

Json format requests can be accepted through the above code adjustment, and the following scripts are written through the source code;

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.*
// You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat

/**
 * @Title: PostDemo
 * @Description: json请求
 * @author 7DGroup
 * @date 2019/10/24 / 12:22
 */

@RunWith(GrinderRunner)
class PostDemo {
	public static GTest test
	public static HTTPRequest request
	public static NVPair[] headers = []
	public static NVPair[] params = []
	public static Cookie[] cookies = []

	@BeforeProcess
	public static void beforeProcess() {
		HTTPPluginControl.getConnectionDefaults().timeout = 6000
		test = new GTest(1, "localhost:8888")
		request = new HTTPRequest()
		// Set header datas
		List<NVPair> headerList = new ArrayList<NVPair>()
		headerList.add(new NVPair("Content-Type", "application/json"))
		headers = headerList.toArray()
		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() {
		String url = "http://localhost:8888/findinfoJson"
		String parambody = "{\"username\":\"600128\"}"
		HTTPResponse result = request.POST(url, parambody.getBytes())

		//显示结果
		def text = result.getText()
		grinder.logger.info(text)
		assertThat(result.statusCode, is(200))
	}
}

operation result:

Insert picture description here

Fourth, source code analysis

By clicking on the post request of the method, you can see which methods are supported in the source code to pass parameters:

HTTPResponse result = request.POST(url, parambody.getBytes())
Insert picture description here

Five, summary

Note that key-valuethe Content-Type Json note value at the time of transmission parameters on the line.

Extended knowledge: