Hi All,
I am creating a gatling script which takes names in StringBody. Name should be unique everytime so I have to generate different random names. Here below script generates different names.
For single user it works fine. For multiple users it generates multiple random names but it request with SAME NAME everytime instead of using different name on every requests.
Like if I use userCount = 5, it will generates 5 different strings but unfortunately it request with one same string everytime in stringBody. I want 5 requests with different names. Can anyone please help me? Thanks.
Here is the code:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import scala.util.Random
class myTerm extends Simulation {
val scenarioRepeatCount = Integer.getInteger("scenarioRepeatCount", 1).toInt
val userCount = Integer.getInteger("userCount", 5).toInt
val TID = System.getProperty("TID", "13203462112")
// Methods for random char generator
def randomAlpha(length: Int): String = {
val chars = ('a' to 'z') ++ ('a' to 'z')
randomStringFromCharList(length, chars)
}
def randomStringFromCharList(length: Int, chars: Seq[Char]): String = {
val sb = new StringBuilder
for (i <- 1 to length) {
val randomNum = util.Random.nextInt(chars.length)
sb.append(chars(randomNum))
}
sb.toString
}
val httpProtocol = http
.connection("""keep-alive""")
.contentTypeHeader("""application/json""")
val scn = scenario("Create")
.repeat (scenarioRepeatCount) {
exec(http("Create with random names")
.post(s"""http://someurl/api/thri/$TID/terms""")
.body(StringBody("""{"term": """" + randomAlpha(7) + """"}""")) // Here randomAlpha(7) creates a string with 7 alphabates
)
}
setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
}