# Use unique value per user execution

**URL:** <https://community.gatling.io/t/use-unique-value-per-user-execution/25>\
**Category:** Gatling (Open-Source)\
**Created:** [January 31, 2012, 5:47pm UTC](https://community.gatling.io/t/use-unique-value-per-user-execution/25 "2012-01-31T17:47:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Carrier](https://avatars.discourse-cdn.com/v4/letter/c/dc4da7/32.png) [@Chris\_Carrier](https://community.gatling.io/u/Chris_Carrier)\
**Post date:** [January 31, 2012, 5:47pm UTC](https://community.gatling.io/t/use-unique-value-per-user-execution/25/1 "2012-01-31T17:47:40Z")

</div>

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) =\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http("request\_45")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.post("some/url")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.headers(headers\_45)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.fileBody("20120131002614\_request\_45.txt")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 31, 2012, 6:19pm UTC](https://community.gatling.io/t/use-unique-value-per-user-execution/25/2 "2012-01-31T18:19:25Z")

</div>

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](mailto:ctcarrier@gmail.com)\>

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [January 31, 2012, 7:42pm UTC](https://community.gatling.io/t/use-unique-value-per-user-execution/25/3 "2012-01-31T19:42:53Z")

</div>

Just to let you know: I’ve fixed [https://github.com/excilys/gatling/issues/388](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](mailto:slandelle@excilys.com)\>

---

<div class="post-metadata">

**Author:** ![Chris\_Carrier](https://avatars.discourse-cdn.com/v4/letter/c/dc4da7/32.png) [@Chris\_Carrier](https://community.gatling.io/u/Chris_Carrier)\
**Post date:** [January 31, 2012, 11:55pm UTC](https://community.gatling.io/t/use-unique-value-per-user-execution/25/4 "2012-01-31T23:55:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 1, 2012, 5:12am UTC](https://community.gatling.io/t/use-unique-value-per-user-execution/25/5 "2012-02-01T05:12:01Z")

</div>

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/Concepts#wiki-feeders)

[https://github.com/excilys/gatling/wiki/Basic-Usage](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](mailto:ctcarrier@gmail.com)\>
