gatlingscala-gatling

gatling: wrong usage of counter in repeat-block


i need some help with the usage of the counter in a repeat-block in gatling.

I have the following:

private val personSheet= "qpaPersonSheet"
private val accountingSheet= "qpaAccountingSheet"
private val createGroup =
group("create group") {
  repeat(10, "counter") {
    exec(Login.loginWithRoleFromJson(role, realm))
      .exec(shortPause)
      .exec(Base.createSheet(personSheet, "#{counter}"))
      .exec(shortPause)
      .exec(Base.createSheet(accountingSheet, "#{counter}"))
      .exec(shortPause)
      .exec(Logout.logout(realm))
  }
}

This is very similar to the example from https://gatling.io/docs/gatling/reference/current/core/scenario/#repeat

I would expect that I can use the value of counter in the Base.createSheet-Function. But insteat of that I only get the string "#counter". What am I missing here?

Edit: The Code from Base.createSheet

def createSheet(name: String, counter: String): ChainBuilder = {
    val uuidIdentifier = s"${name}Uuid"
    val requestName = s"${name}Create"
    val requestUrl = s"/plattform/produkt/#{produktObjectId}/produktbasis"
    val requestBodyUrl = s"request-data/${name}CreateRequestBody.json"
    val responseIdentifier = s"${name}CreateResponse"
    val objectIdName = s"${name}ObjectId" + s"${counter}"
    logger.debug(s"Produktbasis#sheetCreate: uuidIdentifier=$uuidIdentifier")
    logger.debug(s"Produktbasis#sheetCreate: requestName=$requestName")
    logger.debug(s"Produktbasis#sheetCreate: requestUrl=$requestUrl")
    logger.debug(s"Produktbasis#sheetCreate: requestBodyUrl=$requestBodyUrl")
    logger.debug(s"Produktbasis#sheetCreate: responseIdentifier=$responseIdentifier")
    logger.debug(s"Produktbasis#sheetCreate: objectIdName=$objectIdName")
    exec(session => {
      session.set(uuidIdentifier, randomUUID())
    }).exec(http(requestName)
      .put(requestUrl)
      .headers(authHeader)
      .body(StringBody(loadJson(requestBodyUrl))).asJson
      .check(bodyString.saveAs(responseIdentifier))
      .check(jsonPath("$.objectId").saveAs(objectIdName))
    ).exec(session => {
      FileUtil.writeResponseBody(session(responseIdentifier)
        .asOption[String]
        .getOrElse(s"$responseIdentifier has no response body"), responseIdentifier)
      session
    }).exec(checkExitMode)
  }

In the qpaPersonSheetCreateRequestBody.json the "#{counter}" will be replaced by it's value. The value of objectIdName sadly is always qpaPersonSheetObjectId#counter instead of qpaPersonSheetObjectId1 for example. In the end i want to have the ObjectId's from every object in the session to use them in the next group. At the moment there is only the last ObjectId available in the session.

I hope you guys can understand what I want to achive. I'm not a native engslish speaker. So sorry for any spelling mistakes.


Solution

  • First, the #{} Gatling Expression Language syntax was introduced in Gatling 3.8.0. You're using an outdated version and should upgrade.

    Then, Gatling Expression Language doesn't magically work. It's only interpreted by Gatling components, not your own code.

    Moreover, saveAs only accepts a static value, so you can't directly save under a key that would depend on the counter.

    The only solution for you is to use the Session API and move data around.