Use unique value per user execution

Hey I'm trying to create a test where each user execution POSTs a form
with a unique email address. I was thinking I could maybe plug into
the session and use the userId for this (assuming this value is unique
across all users in an execution). Is that a valid assumption? I
tried this:

val postId = chain.exec( (s: Session) =>
                     http("request_45")
                     .post("some/url")
                     .headers(headers_45)
                     .fileBody("20120131002614_request_45.txt")
                     .queryParam("email", "LOADTESTEMAIL" +
s.userId.toString + "@someDomain.com"))

The first problem I'm getting is this error:

error: type mismatch;
found : com.excilys.ebi.gatling.core.Predef.Session =>
com.excilys.ebi.gatling.http.request.builder.PostHttpRequestBuilder
required:
com.excilys.ebi.gatling.core.action.builder.AbstractActionBuilder
        val postId = chain.exec( (s: Session) =>

I adapted this code from the loop usage example on this page:

https://github.com/excilys/gatling/wiki/Scenario-Components

But I'm obviously doing something wrong. What's the proper way to
access the Session in this way?

Thanks!
Chris

Hi Chris,

You have to use functions just for the parts that you want to be resolved at runtime.
So, for what you’re trying to do, you have to write :

val postId = chain.exec(
http(“request_45”)
.post(“some/url”)
.headers(headers_45)
.fileBody(“20120131002614_request_45.txt”)
.queryParam((s: Session) => “email”, (s: Session) => “LOADTESTEMAIL” + s.userId.toString + “@someDomain.com”)
)

Having to write a function for the queryParam name shouldn’t be forced like it is now.
I’m going to fix this so you only have to write :

val postId = chain.exec(
http(“request_45”)
.post(“some/url”)
.headers(headers_45)
.fileBody(“20120131002614_request_45.txt”)
.queryParam(“email”, (s: Session) => “LOADTESTEMAIL” + s.userId.toString + “@someDomain.com”)
)

Sincerely,

Stephane

2012/1/31 Chris Carrier <ctcarrier@gmail.com>

Just to let you know: I’ve fixed https://github.com/excilys/gatling/issues/388

Depending on critical bugs that might arise, this will be in the 1.0.4 or the 1.1.0.

Sincerely,

Stephane

2012/1/31 Stéphane Landelle <slandelle@excilys.com>

Do you have any suggestions for the best practice for generating a
semi-random value for each user execution? Is the userId a good
choice? Or should I use a random generator? I don't need crypto
level random or anything just something that's very likely to be
random for each user execution.

And as an extension to that is there a good way to inject an arbitrary
value into the session without a check? For instance I want to create
a fake unique email for each user. It would be nice if at the
beginning of each execution I could inject an arbitrary value into the
session something like:

scenario("Some scenario").prepare((s: Session) => saveAs("userEmail",
"test" + s.userId + "@someDomain..com"))

Is there a way to do something like this? Or maybe I'm making it too
complicated and there's a simple way to generate a unique value per
execution.

Chris

The userId is a simple sequence and is unique across users. I let you decide if it’s a quick win for your needs.

The proper way for injecting external data into the session is the feeder API:https://github.com/excilys/gatling/wiki/Concepts#wiki-feeders

https://github.com/excilys/gatling/wiki/Basic-Usage → Dynamic values with Feeders

A feeder will inject data in a given session every time the corresponding “user” runs it. So if you want to inject data once per user run, simply set the feeder at the beginning of the scenario.
In 1.0.x, there’s only 1 implementation of FeederSource that reads from a file. There’s another implementation in the master (so for 1.1.0) that reads from a database.

Depending on your level with Scala and what you’re trying to achieve, the easiest solution would be :

  • either to pre-generate your data, dump it into a file and use a standard file feeder
  • write your own feeder implementation, like :

import com.excilys.ebi.gatling.core.feeder.Feeder
import scala.util.Random

val customFeeder = new Feeder(null) {
val random = new Random
def next = Map(“myRandomValue” → random.nextInt)
}

(the null parameter won’t be necessary in 1.1.0)

Stephane

2012/2/1 Chris Carrier <ctcarrier@gmail.com>