Pebbles

A template engine is a way to include some logic in your document/JSON, by putting keywords which will be interpreted and replaced when the simulation is launched.

Expression Language templating

Before Gatling 3, there was only one way to use dynamic variables inside a request body, the Expression Language (also named EL). Expression Language supports basic operations, like fetching an attribute in the session (memory case of a user) and some functions. In the following example, ${id} and ${name} will be replaced by the “id” and “name” attributes in the session.

{
  "id": ${id},
  "name": "${name}",
  "numberOfChildren": 2,
  "children": [{
    "id": 1002,
    "name": "Paul Smith"
  }, {
    "id": 1003,
    "name": "Jessica Smith"
  }]
}

Pebble templating

We introduced in Gatling 3 a new way to craft request bodies, Pebble templating. Pebble is a logic-full templating engine, with several interesting features missing in EL: filters, if, loop, import…

No specific import is needed to use this feature.

Let’s see how we can modify the following JSON file to use the attributes defined in the session: the id/name of the user, and an array of children.

{
  "id": 1000,
  "name": "John Smith",
  "numberOfChildren": 2,
  "children": [{
    "id": 1002,
    "name": "Paul Smith"
  }, {
    "id": 1003,
    "name": "Jessica Smith"
  }]
}

The first step is to replace the id and name by the ones in the session. Gatling will replace {{id}} and {{name}} by the attributes named “id” and “name” in the session .

{
  "id": {{id}},
  "name": "{{name}}",
  "numberOfChildren": 2,
  "children": [{
    "id": 1002,
    "name": "Paul Smith"
  }, {
    "id": 1003,
    "name": "Jessica Smith"
  }]
}

The second step is to compute the length of our array of children. We can use the filter “length” available in Pebble.

The final step is to loop over our children to query their id and name. Our loop begins with “{% for %}” and ends at “{% endfor % }”, displaying the id and name of each current child.  We’re not displaying the comma for the last element of the array thanks to an if condition.

{
  "id": {{id}},
  "name": "{{name}}",
  "numberOfChildren": {{children|length}},
  "children": [
  {% for child in children %}
      {
        "id": {{child.id}},
        "name": "{{child.name}}"
      }{% if loop.last %}{% else %},{% endif %}
  {% endfor %}
  ]
}

Once you’ve created your Pebble template, you can use it in Gatling:

.body(PebbleStringBody(""" file body as String """)).asJson
// or
// myFileBody.json is a file that contains our JSON
.body(PebbleFileBody("myFileBody.json")).asJson

Expression Language vs Pebble

Expression Language is faster and simpler than Pebble, however Pebble has more advanced features. I recommend using EL if your needs are simple. If your needs are more complicated, you can consider using Pebble or EL with a bit of Scala.

We’ll continue to maintain both Pebble and the Expression Language, so be sure to use the option which fits your needs!

Thanks for reading this article about Pebble templating in Gatling, check Pebble documentation or Gatling documentation for more examples.

Cédric from the Gatling team

Related resources

You might also be interested in…