Gatling Simulation from Scratch – Feeders and HTTP Requests

If you arrived at this page directly, please go through this post first. It explains some basics, how to define HTTP protocol configurations and headers which will be used while building the test scenario along with the feeders and HTTP requests described in this post.

Step 3: Define Feeders

Feeders resemble parameter files in Loadrunner and Jmeter. As the name suggests, these are used to feed data to your simulation. Feeder is a type alias for Iterator[Map[String, T]], meaning that the component created by the feed method will poll  Map[String, T] records and inject its content. Gatling provides various built-in feeders like csv feeder, json feeder, JDBC feeder, sitemap feeder, redis feeder etc. There some good examples of built-in feeders in Gatling documentation, so here I’ll provide examples of custom feeders

import java.util.{Properties, UUID} //1
import java.util.Base64 //2

val requestIdFeeder = Iterator.continually(Map("requestId" -> UUID.randomUUID())) //3
val someIdFeeder = Iterator.continually(Map("someId" -> { //4
	val sId = s"${(Random.alphanumeric take 16).mkString("")}"
	val base64sId = Base64.getEncoder.encodeToString(sId.getBytes(StandardCharsets.UTF_8))
	base64sId;
}))
  1. Required import for UUID
  2. Required import for Base64
  3. Creates a requestId feeder of random UUID – returns a randomUUID each time a call is made to this feeder
  4. Creates someId feeder which returns a base64 encoded random alphanumeric string of length 16

 

Step 4: Define HTTP Requests

Now let’s define HTTP requests which will be simulated in the test scenario. Consider following sample request defined as a Scala variable. It takes two feeders as input, executes requestOne1 following by requestOne2 REST API, passing requestOnePayload1.json and requestOnePayload2.json respectively as input body. Once the request is executed, its status is checked on request level as well as application level.

val requestOne =
	group("RequestGroupName"){ //1
		feed(requestIdFeeder) //2
		.feed(taskIdFeeder) //3
		.exec(http("httpRequestName1") //4
			.post("${url}/path/to/resource/requestOne1") //5
			.headers(jsonHeader) //6
			.body(ElFileBody("requestOnePayload1.json")).asJSON //7
			.check(status.is(200)) //8
			.check(jsonPath("$.statusCode").is("200")) //9
		.exec(http("httpRequestName2")
			.post("${url}/path/to/resource/requestOne2")
			.headers(jsonHeader)
			.body(ElFileBody("requestOnePayload1.json")).asJSON
			.check(status.is(200)) //http respnose status
			.check(jsonPath("$.responseHeader.statusCode").is("200"))
		)
	}
//defined similar to requestOne above
val requestTwo = ...
val requestThree = ...
  1. The response time of a group is the cumulated response times of each individual request in that group. Useful to model requests on the same page (e.g. group of CSS, image, HTML requests on a page).
  2. Usage of feeders defined in step 3
  3. Usage of feeders defined in step 3
  4. Used to execute an action, in this case, HTTP request
  5. API POST request
  6. Usage of headers defined in step 2
  7. Send contents of the json file as the request body. ,asJSON ensures the content is valid json.
  8. Check on HTTP 200 status code of the request
  9. check on the status code in json response received from the server. It traverses the jsonPath, extracts its value and compares it with the one in is the clause. See sample response payload below
{
	"responseHeader" : {
		"responseId" : "917fb901-161c-47d0-b5ee-475dbf31cd69",
		"conversationId" : "9bc872f9-5575-49d6-8a6c-32944c7aec61",
		"statusCode" : "200",
	},
	"registrationResponse" : {
		//....
	}
}

Next, we will use these feeders and HTTP request to define a scenario. See here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s