Search code examples
scalagatling

Gatling Scripts- how can I feed the eventdatetime in the below request with different epoch time


Gatling Scripts- How can I feed the eventDateTime in the below request with different epoch time as a feeder too. The Id is feed through the feeder in a circular way. the Below request is in a file which is fed using a ELFileBody. The event-new.json looks like

{
  "events": [
    {
      "eventDataModel": {
        "eventStatus": "ON",
        "eventDateTime": 1598271042436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    },
    {
      "eventDataModel": {
        "eventStatus": "ON",
        "eventDateTime": 1598271043436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    },
    {
      "eventDataModel": {
        "eventStatus": "ON",
        "eventDateTime": 1598271044436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    },
    {
      "eventDataModel": {
        "eventStatus": "ON",
        "eventDateTime": 1598271045436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    },
    {
      "eventDataModel": {
        "eventStatus": "ON",
        "eventDateTime": 1598271046436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    },
    {
      "eventDataModel": {
        "eventStatus": "ON",
        "eventDateTime": 1598271047436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    },
    {
      "eventDataModel": {
        "eventStatus": "ON",
        "eventDateTime": 1598271048436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    },
    {
      "eventDataModel": {
        "eventStatus": "OFF",
        "eventDateTime": 1598271049436
      },
      "transactionModel": {
        "id": "${ID}"
      }
    }
  ]
}

above is the event-new.json which is been posted using the gatling script snippet from gatling script

val idFeeder = csv("Id.csv").circular
val trip_dte2 = scenario("Event")
                    .feed(idFeeder)
                    .exec(http("event")
                    .post( event_url)
                    .headers(headers)
                    .body(ElFileBody("event-new.json")).asJSON
                    .check(status.is(201)))

Solution

  • As of Gatling 3.3:

    .exec { session =>
      session.set("timestamp", System.currentTimeMillis())
    }
    

    and then replace 1598271049436 with ${timestamp}.

    Or, coming in Gatling 3.4: directly replace 1598271049436 with ${currentTimeMillis()} if it's fine for you to have different values for each occurrence in your JSON payload.