简体   繁体   中英

How to pass a list of maps to pebble template in Gatling?

There is an official documentation of Pebble templating in Gatling: https://gatling.io/2018/11/06/gatling-3-pebble-templating/

In the end of this you can see the code:

{% for child in children %}
  {
    "id": {{child.id}},
    "name": "{{child.name}}"
  }{% if loop.last %}{% else %},{% endif %}
{% endfor %}

It means that the children variable is a list that contains a map with keys id and name But how to create such list and let Pebble know about it?

Currently I have a method for creation a list of maps:

  def getArray(count: Integer): mutable.MutableList[Map[String, String]] = {
    var array = mutable.MutableList[Map[String, String]]()
    for (i <- 0 until count) {
      array += Map(
        "UniqueString" -> UUID.randomUUID.toString,
        "RandomNumber" -> (Random.nextInt(10000) + 10000).toString,
        "contentId" -> UUID.randomUUID.toString
      )
    }
    array
  }

And I have a feeder for it:

  val elementsFeeder = Iterator.continually(Map(
    "elements" -> getArray(documentsMultiple)
  ))

I feed it as usual:

  def archive = repeat(1){
    feed(elementsFeeder)
    .exec(
    .......

My pebble template looks like this:

[
{% for element in elements %}
  Debugging info <element>: {{element}}
  {"name": "{{element.UniqueString}}",
  "value": "{{element.RandomNumber}}"}{% if loop.last %}{% else %},{% endif %}
{% endfor %}
]

But instead of name and value I see only empty space between quotes, when debugging line looks like this:

Debugging info <element>: Map(UniqueString -> 81a92aeb-3966-43ff-8c42-1e0e93ace3a4, RandomNumber0To100000 -> 11775, contentId -> 436701db-d66b-4819-b33e-3e455dbe19fc)

Even if I change the maps generation to convert them to Java's maps by adding .asJava after a new Map declaration, I still see empty spaces and output like this:

Debugging info <element>: {UniqueString=119593c6-e526-4cca-a2d5-34d57bbfb22d, RandomNumber0To100000=16945, contentId=57385850-670b-484b-96e6-a1ec05c568ac}

Pebble is a java library and works with Java collections, not Scala ones.

There are some shortcomings as of Gatling 3.4.0:

  • we don't wrap Scala Maps into Java ones
  • we don't deep wrap/convert Session content, only root elements

Try using Java collections for now.

To get a value from a map using pebble you should access it like the regular java map:

{% for element in elements %}
  {"name": "{{element.get("UniqueString"}}",
  "value": "{{element.get("RandomNumber")}}"}{% if loop.last %}{% else %},{% endif %}
{% endfor %}

To create feeder I use 2 methods. One of them returns a new instance of java.util.HashMap and the second creates an java.util.ArrayList of these maps. And after it I can create feeder:

val elementsFeeder = Iterator.continually(Map(
  "elements" -> getJavaArray(10)
))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM