Session variable / query API in first call and use result in subsequent calls

Hi all,

Gatling 2.0.0-M3a

I have a REST API that is called with variable set to a default value of 0. The returned data contains the current value of that variable on the server.

Each subsequent call to that API has to use the new value of the variable.

I was able to successfully extract the current value from a REST call and save it in a session variable. Printing the session shows the session variable has the correct value.

What I want to accomplish is:

a. Initialize the session variable with value 0

b. Make first REST call and set the session variable with the value returned by server. This can be done once per simulation and that value set in each session (each user).

c. Start the main loop with a repeat/during block that generates the load on the server.

I am thinking along the lines of:

val scn = scenario(“Get Generation”)
.feed(csv(“user_information.csv”)) // contains userid,passwd
.repeat(1) {
exec(session => {session.set(“generation_current”, 0)})
.exec(
http(“user_config”)
.get("/svc/api/v1/2/users/${userid}/user_config?generation=${generation_current}")
.basicAuth("${userid}", “${passwd}”)
.check(jsonPath("$…generation").find.saveAs(“generation_current”))
.check(status.is(200))
)
}

val loopscn = scenario(“Create Config”)
.feed(csv(“user_information.csv”)) // same feed as before
.repeat(100) {
.exec(
http(“user_config”)
.get("/svc/api/v1/2/users/${userid}/user_config?generation=${generation_current}")
.basicAuth("${userid}", “${passwd}”)
.check(status.is(200))
)
}

setup(scn.inject(ramp(1000 users) over (100 seconds))

.loopscn.inject(ramp(1000 users) over (100 seconds))).protocols(httpProtocol)

What I do not know is if the 1000 user sessions created for the first scenario will be reused for the second scenario.

Is there a better way to construct this simulation?

Thanks
Sajjad

A session is something to a specific to a virtual user … so, setting a value in the session of a user won’t affect the session of an other user.
Also, when you have two scenarios, it’s different users going thru them, so the session won’t be shared.
Would something like that work for you ?

@volatile var generationCurrent = 0

val scn = scenario(“Get Generation”)
.feed(csv(“user_information.csv”).circular) // contains userid,passwd
.exec(
http(“user_config”)
.get("/svc/api/v1/2/users/${userid}/user_config?generation=0")
.basicAuth("${userid}", “${passwd}”)
.check(jsonPath("$…generation").find.saveAs(“generation_current”))
.check(status.is(200))
)
.exec(session => {
generationCurrent = session(“generation_current”).as[Int]
session
})

val loopscn = scenario(“Create Config”)
.feed(csv(“user_information.csv”).circular) // same feed as before
.exec(_.set(“generation_current”, generationCurrent))
.repeat(100) {
.exec(
http(“user_config”)
.get("/svc/api/v1/2/users/${userid}/user_config?generation=${generation_current}")
.basicAuth("${userid}", “${passwd}”)
.check(status.is(200))
)
}

setup(scn.inject(ramp(1 users) over (1 seconds))
.loopscn.inject(nothingFor(10 seconds), ramp(1000 users) over (100 seconds))).protocols(httpProtocol)

I execute the first call, set the global variable … wait for 10 seconds and then start the second scenario.
So, this works as long as the first call take less than 10 seconds.

cheers
Nicolas

Thanks, Nicolas. Your proposed solution would help.

After sending the last message, I tried out the following, which might also do what is expected:

val scn = scenario(“config”)
.feed(csv(“agent_information.csv”))
.exec(session => {session.set(“generation_current”, 0)})
.exec(
http(“user_config”)
.get("/svc/api/v1/2/users/${id}/user_config?generation=${generation_current}")
.basicAuth("${id}", “${token}”)
.check(jsonPath("$…generation").find.saveAs(“generation_current”))
.check(status.is(200))
)
.pause(1 seconds)
.repeat(10) { // or during()
exec(
http(“user_config”)
.get("/svc/api/v1/2/users/${id}/firewall_config?generation=${generation_current}")
.basicAuth("${id}", “${token}”)
.check(status.is(200))
)
.pause(1 seconds)
}

Thanks,
That was useful for me.