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

1,689 views
Skip to first unread message

Sajjad Lateef

unread,
Dec 11, 2013, 6:16:43 PM12/11/13
to gatling
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
--
Sajjad Lateef  sajjadlateef (a) acm.org
slateef.gc (a) gmail.com

Nicolas Rémond

unread,
Dec 11, 2013, 6:38:47 PM12/11/13
to gat...@googlegroups.com
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









--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Sajjad Lateef

unread,
Dec 11, 2013, 8:13:30 PM12/11/13
to gatling

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)
            }


Ivan S

unread,
Sep 17, 2017, 7:44:33 AM9/17/17
to Gatling User Group
Thanks,
That was useful for me.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages