retrieve json value from one session and pass to simulation

Hi there,

I need to retrieve a session ID from one http call and pass it into the simulation (either as part of the URL or as part of the message body).

After some google research, I gave up on using two scenario and turned to using before hook to retrieve the session ID. I followed the Stéphane Landelle’s suggestion of using AsyncHttpClient and was able to get the json message body. But I am stuck in

  1. parsing json data in the before hook
  2. passing the session ID from before to scenario

Can someone show me the path? Or tell me if it’s not doable?

Many thanks,
Derek

++++++++++ my test code ++++++++++
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import org.asynchttpclient.{DefaultAsyncHttpClient, Realm}

class test extends Simulation {

val username = “+++”
val password = “+++”

before {
val client = new DefaultAsyncHttpClient()
val auth = new Realm.Builder({username}, {password})
.setScheme(Realm.AuthScheme.BASIC)
.build()
val future = client.prepareGet(“http://localhost:42181/url1”)
.addHeader(“Content-Type”, “application/json”)
.addHeader(“Accept”, “application/json”)
.setRealm(auth)
.execute()
val respond = future.get().getResponseBody()
client.close()
// NEED to retrieve SESSION_ID and pass to scenario below
}

val httpConf = http.baseURL(“http://localhost:42181”)
val scn = scenario("gatling before test ")
.exec(http(“request_1”)
.get(“url2/” + SESSION_ID) // need SESSION_ID from before hook
.header(“Content-Type”, “application/json”)
.header(“Accept”, “application/json”)
.basicAuth({username}, {password})
.check(status.is(200))
)

setUp(
scn.inject(rampUsers(100) over (10 seconds)).protocols(httpConf)
)
}

I play with it a little more and found that the before block executes after the simulation scenario is built. If this is true, my method will not work.

I tried multiple scenarios and the result is similar: all scenarios are built first and then execute; so I can’t use the http result from earlier scenario to construct later scenario.

Please let me know if I am wrong. Also, let me know if there is another way to work around the problem.

Derek

You have to understand that everything you write into the Simulation class body is executed in the constructor, except the before and after hook that are delayed later before starting injecting, and after finishing injecting.
So if you want the result of some operations to be available in the constructor, you must perform them…

Thanks for your reply. I’m new to gatling and I am not sure what you mean “perform”.

Basically, I need to call an URL, retrieve a json value from the respond message body and use that value to construct a new scenario for performance testing. Am I on the right path?

Derek

“So if you want the result of some operations to be available in the constructor, you must perform them…”

You’re the one that has to complete the sentence.
I provided enough information so you can figure it out yourself.
My moto is “teach a man out to fish”.

Thanks! I know what you mean now. I don’t need the before hook and just execute the lines. Then, my next question is how do I parse the json data in the constructor? I don’t think I can use jsonpath. Do I use Jackson?

Derek

Thanks! I know what you mean now. I don't need the before hook and just
execute the lines.

Exactly :slight_smile:

Then, my next question is how do I parse the json data in the constructor?
I don't think I can use jsonpath. Do I use Jackson?

Or Boon, both are shipped.

*Stéphane Landelle*
*GatlingCorp CEO*
slandelle@gatling.io