Unique POST param value for every call

Hi there

I’m a noob to Scala and Gatling so please bear with - I have read as much of the relevant documentation as I can but still not sure how to do this.

So, I’m writing a simulation to load test a website that requires one field (a request id) to be completely unique every call.

So I generate the value like this, but any method would be ok, I’m sure:

val rqId = “MYLOADTEST_” + Verification.randomAlpha(20)

(where my randomAlpha function calls util.Random.nextInt but that works, so I’ll not post that bit)

Then:

val scn = scenario(“My test”)
.exec(
http(“Payment123JS”)
.post("")
.formParam(“requestId”, rqId)
.formParam(“language”, “en”)

.check(status.is(200))

)

.pause(1)

and finally

setUp(scn.inject(atOnceUsers(10)).protocols(httpProtocol))

So clearly each of these 10 calls is identical, same requestId, so not what I want. But within the simulation there doesn’t seem any way to vary this at all. If I pass in a value, it’s no different. I could use several injectors running independently and change

setUp(scn.inject(atOnceUsers(10)).protocols(httpProtocol))

to

setUp(scn.inject(atOnceUsers(1)).protocols(httpProtocol))

but there must be a better way?

val rqId = “MYLOADTEST_” + Verification.randomAlpha(20)

val is a value, so it’s only computed once => you want a def (method)

def rqId = “MYLOADTEST_” + Verification.randomAlpha(20)

Fantastic, that is sort of what I need. There are two places I need this value though - one is to seed a verification hash function, the output of which goes into another form parameter, and the other is the actual form parameter itself above. Is there a clever way to use a def but to ensure for the same invocation somehow it returns a single value. Does that make sense? I can work round all of this but would rather not need to.

Thanks for the help so far though, always good to learn something new!

If I understand, what you are seeking is to save the generated rqId for one virtual user.

So, you can save it in the session.

def generateRqId = "MYLOADTEST_" + Verification.randomAlpha(20) // Note I renamed this function to better show what happens =)

scenario("My test")
  .exec(session => session.set("rqId", generateRqId)) // save in session

  .exec(
    http("Payment123JS")
      .post("<snip my URL>")
      .formParam("requestId", "${rqId}") // simple usage here
      .formParam("language", "en")
<snip>
      .check([status.is](http://status.is)(200))
    )
    .pause(1)

If needed, you can save a new rqId whenever you want.

Many thanks. I think this will do what I need. Scala is great - never used it before, I’ve done most of my development in Java, C++, C and JS to date.

Hmm - not quite right yet. My code is :

def generateRqId = “MYLOADTEST_” + Verification.randomAlpha(20)

val scn = scenario(“RSL”)
.exec(session => session.set(“rqId”, generateRqId))
.exec(
http(“MYLOAD”)
.post(“myurl”)
.formParam(“requestId”, “${rqId}”)
.formParam(“verify”, Verification.processIntoValue(“DATASTARTrequestid=${rqId}|otherstuff=xyz”))
.check(status.is(200))
.check(substring(“seqNumber=”).exists)
)
.pause(1)

Unfortunately, although it passes the right value as the form param requestId, it passes the literal ${rqId} into the method Verification.processIntoValue rather than the session value. If I separate it into “DATASTARTrequestid=” + “${rqId}” + “|otherstuff=xyz”, the same. So the form param verify ends up incorrect. That’s what I was meaning by using the same value in two places.

Apologies if this gets posted twice, I posted and it seems to have vanished :slight_smile:

Verification.processInfoValue is your own method, I guess.
And this is not aware of the session.

The code I provided use some scala magic to change formParam("requestId", "${rqId}") into formParam("requestId", session => session.get("rqId")) (in reality, there is something more complicated, but the idea is here).

So, in your case, you need to build your own value:

        .formParam("verify", Verification.processIntoValue("DATASTARTrequestid=${rqId}|otherstuff=xyz"))

to something like:

        .formParam("verify", Verification.processIntoValue(session => "DATASTARTrequestid=" + session("rqId").as[String] +|otherstuff=xyz"))

Source: https://gatling.io/docs/current/session/session_api/

Cheers - I had to change to this:

.formParam(“verify”, session => Verification.getHash(“DATASTARTrequestid=” + session(“rqId”).as[String] +…

but it now does exactly what I need. Many thanks for your help. Impressed with Gatling - first time I’ve used it, easy to use and gather results.

You’re right.

I edited the line directly in the mail and didn’t put the session => in the good parentheses.

Good to know it works for you ^^

If your usage needs metrics history, run comparison or more than one machine to run the load test, consider using FrontLine to support us ^^