Different unique numbers for different users

Hello,

for my test scenario with 10 or 100 users at once, I need for every user different numbers for my REST request.
User 1 gets the numbers 1 to 10 and User 2 needs to get higher numbers, for example 11-20 and so on.
How can I achieve that?
All of the 10 or 100 users are performing the same put request with different JSON Body and I need to get different numbers for my REST call.

Thanks for your help!

Use some exec(session function) and a global AtomicInteger to store an incremental number in the sessions.
Then turn your Body into a template, such as ElFileBody.

Cheers,

My code looks like this:

def pushFlowWithIPv4Match(switch: String, requests: Int) = repeat(requests, "counter") {
  exec(session =>
    session.set("FlowBody", JSON_RESTCONF_Body_Push_Flow.createFlowBodyWithIPv4(session("counter").validate[Int].map(i => i + 1).get, IPGenerator.generate(), null,true))
      .set("number", session("counter").validate[Int].map(i => i + 1).get).set("switch", switch))
    .exec(http("""Add Flow ${number} on ${switch}""")
    .put("""/restconf/config/opendaylight-inventory:nodes/node/${switch}/table/0/flow/${number}""")
    //.headers(headers)
    .body(StringBody("${FlowBody}")))
}

This is how my code looks like so far and I need change ${number} in every request and it should be unique over the different users I am injecting.
Is it possible to generate numbers in the range from 1 to 20 for 2 users, where each user gets different numbers? User 1 gets the numbers 1 to 10 and User 2 gets the numbers 11-20. 

You’d have to write a combo between some global AtomicInteger counter, and some per user counter stored in the session (e.g a repeat loop counter).

It is woking with AtomicInteger.

Thanks for your help.

Hi, I have the same challenge in my simulation. I am using ELFileBody to feed my scenario like this:

`

.exec(http(“createProfile”)
.post("""/s/c-bt.shitapi/MemberProfile.svc""")
.body(ELFileBody(“RecordedSimulation_request_0000.txt”))
.check(status.is(200)))

`

and my request body(RecordedSimulation_request_0000.txt) looks like this:

`

<ns0:CreateProfileRequest xmlns:ns0=“http://www.shit.com/cache/memberprofile/schemas/1.0”>
ns0:FirstnameJan</ns0:Firstname>
ns0:LastnameMuch</ns0:Lastname>
ns0:MobileNumber99939911</ns0:MobileNumber>
ns0:EmailAddressjm112@start.com</ns0:EmailAddress>
ns0:StreetStreet 12</ns0:Street>
ns0:Postcode1359</ns0:Postcode>
ns0:CountryNO</ns0:Country>
ns0:EmailConsenttrue</ns0:EmailConsent>
ns0:SMSConsenttrue</ns0:SMSConsent>
ns0:PasswordPass123</ns0:Password>
</ns0:CreateProfileRequest>

`

How can I wrap an atomicHeader around my .exec in order to increase the email by 1 / add 1, then 2 and so forth for each user and the same for MobileNumber?

Cheers

Think of the AtomicInteger as a memory address with an API. Right before you do your exec(), you would have an exec( session => { … } ) block which updates the session with the next unique ID using the AtomicInteger. For specific syntax, read up on AtomicInteger.

Thanks, but how do I get to put it into my request body as a parameter? Can I refer it as a regular parameter within the request body as I do when using a feeder?
And,
Can I combine a static value with the atomic integer value. I just need unique email addresses and I want to say combine thisadress with ${theAtomicValue} @ myemail.com
as thisadress${theAtomicValue}@myemail.com within the request body? What would be the syntax then?

Cheers!

This isn’t that complicated. The AtomicInteger is just that, an integer. You may do with it what you want.

HOWEVER: you are not going to reference the atomic integer directly from within EL. EL only pulls from the session. You need to create code that queries the AtomicInteger, and constructs an appropriate value, and then stores that in the session, so you can then reference the session value inside your request body.

Make sense?

Ok, putting some examples togheter from this forum I have this:

`

class openModelSimulation extends Simulation {

  val counter = new java.util.concurrent.atomic.AtomicInteger(1)
  
  def unique_email() : Map[String, String] = {
    var base_email = "email @ mail.com"
    var unique_id = counter.getAndIncrement.toString
    var transaction_id =  unique_id + base_email
    var unique_email = Map("tid" -> transaction_id)
    return unique_email
  }

val httpConf = http
 .baseURL("https://cdcapp.site.com")
  .acceptEncodingHeader("gzip,deflate")
  .headers(Map("Content-Type" -> "application/json", "charset" -> "UTF-8",  "User-Agent" -> "Android(4.4)/Coop(0.4)/0.1"))
  .disableUrlEncoding

val scn = scenario("Scenario")

.exec(session => {
println((session(unique_email()).as[String]))
session})

setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}

`

But I do get an error at the line with:

println((session(unique_email()).as[String]))

Type mismatch, expected String, actual Map (string, string),

Cheers

Think it through.

To set a value: session.set(‘key’,‘value’)
To get a value: session(‘key’).as[Type]

Where do you see a Map? So why does unique_email return a Map? Which part of the above is unique_email() intended to be substituted into? So, which type should it return?

Thinking it through I made it work like this:

`

package com.site

import java.util.concurrent.atomic.AtomicInteger
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class testSimulation extends Simulation {

  val httpConf = http
    .baseURL("https://site.com")
    .acceptEncodingHeader("gzip,deflate")
    .headers(Map("Content-Type" -> "application/json", "charset" -> "UTF-8",  "User-Agent" -> "Android(4.4)/Coop(0.4)/0.1"))
    .disableUrlEncoding

  val myGlobalVar = new java.util.concurrent.atomic.AtomicInteger(0)

  val scn = scenario("Scenario")

    .exec(session => session.set("my_var", myGlobalVar.getAndIncrement))

    .exec(session => {
    println((session("my_var").as[String]))
    session})

    .exec(
      http("getToken")
        .post("https://site.com/ids/connect/token")
        .body(ELFileBody("my_request_refering_a_value_in_session.txt"))
    )

  setUp(scn.inject(atOnceUsers(5)).protocols(httpConf))
}

`

However I do not understand what you mean by:
“You need to create code that queries the AtomicInteger, and constructs an appropriate value, and then stores that in the session, so you can then reference the session value inside your request body.”

Do I need to create separate code to get the “my_var” and then put that value into the session and then refer that (in session) value within the request body?

How can I create that code in my example?

I was deliberately vague, because this is one of those things where there are many different ways to accomplish the same goal. For example, if your payload is an EL file, you could do either of these:

OPTION1:

{

“email”: “${email_address}”

}

OPTION2:

{

“email”: “unique-email-${email_counter}@example.com

}

Which one you choose determines the code that you write. You can either query the AtomicInteger and set the email_counter session variable (the easiest solution), or you can do a more complicated expression that builds the whole email address and stores it in the session. It is six of one, half-dozen of the other, so it doesn’t MATTER which you choose. You may even get creative and come up with something entirely different.

Point is, know what you are trying to accomplish, understand the steps involved, and then code them. Right?

Thanx!