Eli Lyonhart
Eli Lyonhart

Reputation: 11

Creating dynamic POST /users calls with Gatling in Scala

I am using Gatling to generate a large number of users to test performance issues on my product. I need to be able to dynamically create users with unique fields (like 'email'). So, I'm generating a random number and using it, but it isn't being re-instantiated each time, so the email is only unique on the first pass.

object Users {

  def r = new scala.util.Random;
  def randNumb() = r.nextInt(Integer.MAX_VALUE)
  val numb = randNumb()

  val createUser = {
    exec(http("Create User")
    .post("/users")
    .body(StringBody(raw"""{"email": "[email protected]" }""")))
  }
}

val runCreateUsers = scenario("Create Users").exec(Users.createUser)

setUp(
  runCreateUsers.inject(constantUsersPerSec(10) during(1 seconds))
).protocols(httpConf)

Where should I be defining my random numbers? How can I pass it into createUser?

Upvotes: 0

Views: 838

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19527

Use a feeder:

object Users {
  val createUser = exec(http("Create User")
    .post("/users")
    .body(StringBody("""{"email": "qa_user_${numb}@Marqeta.com" }""")))
}

val numbers = Iterator.continually(Map("numb" -> scala.util.Random.nextInt(Int.MaxValue)))

val runCreateUsers = scenario("Create Users")
  .feed(numbers)
  .exec(Users.createUser)

...

Upvotes: 1

Related Questions