Gatling Inject Dynamic Values into a Json Template

In this example, we will show how to use Gatling to send a POST request with a body using a JSON file template with dynamic values.

This sample project is built using Gatling with Scala and Gradle build tool.

Pre-requisite:

To setup your Gatling project follow the steps defined in Performance Testing Framework with Gatling and Maven

Create the JSON Template File

First create a JSON template file, give it a name and place it under the src/gatling/resources folder:

{
  "orderId": "#{orderId}",
  "customerId": "#{customerId}",
  "amount": 10.00
}

Send Requests with Dynamic Data

Next, in our request object we will create random UUIDs and set a new session, so each request will use a different UUID each time.

  var customerId = java.util.UUID.randomUUID()
  var orderId = java.util.UUID.randomUUID()

  val setNewSession =
    exec(setSessionAttributesBulkAdd => {
      setSessionAttributesBulkAdd.setAll(
        "customerId" -> s"${customerId}",
        "orderId" -> s"${orderId}"
      )
    })

Then we send a request with the JSON file template using the ElFileBody and Gatling will automatically inject the values into the file:

  val sendRequestWithDynamicData =
      http("Request With Dynamic Values")
        .post("/some-endpoint")
        .body(ElFileBody("jsonTemplate.json"))
        .check(status.is(200))
        .check(jsonPath("$.someParam").saveAs("someParamValue"))

Full request object file looks like this:

import io.gatling.core.Predef._
import io.gatling.http.Predef._

object InjectDynamicValues {

  var customerId = java.util.UUID.randomUUID()
  var orderId = java.util.UUID.randomUUID()

  val setNewSession =
    exec(setSessionAttributesBulkAdd => {
      setSessionAttributesBulkAdd.setAll(
        "customerId" -> s"${customerId}",
        "orderId" -> s"${orderId}"
      )
    })

  val sendRequestWithDynamicData =
      http("Request With Dynamic Values")
        .post("/some-endpoint")
        .body(ElFileBody("jsonTemplate.json"))
        .check(status.is(200))
        .check(jsonPath("$.someParam").saveAs("someParamValue"))
}

Scenario

In the Scenario object, we first have to execute the setNewSession request to initialize the values and then execute the sendRequestWithDynamicData request

import io.devqa.requests.InjectDynamicValues
import io.gatling.core.Predef._

object DynamicValuesScenario {

  val dynamicValues = scenario("Scenario that injects dynamic values")
    .exec(InjectDynamicValues.setNewSession)
    .exec(InjectDynamicValues.sendRequestWithDynamicData)
}

Simulation

And finally, the simulation file looks like:

import io.devqa.scenarios.DynamicValuesScenario
import io.gatling.core.Predef._

import scala.concurrent.duration.DurationInt
import scala.language.postfixOps

class DynamicValuesSimulation extends Simulation {

  setUp(
    DynamicValuesScenario.dynamicValues.inject(
      nothingFor(3),
      constantUsersPerSec(1) during (60 second))
  )
}

You can then play around with different numbers for users and duration to simulate desired load.