Saving an attribute value using Session

4,952 views
Skip to first unread message

Sameer Mainkar

unread,
Dec 2, 2014, 4:43:33 AM12/2/14
to gat...@googlegroups.com
Hello,

I am trying to save an attribute value to a variable, so that it can be used in further chains. Following is the snippet I am trying:


.exec(session => {
  val VariableSelect = session("Variable.random()").as[String]
  session.set("VariableSelect",VariableSelect)
})

Here "Variable" is the Vector containing multiple elements. If I use ${Variable.random()} in my request directly it returns one of the element, but since I am looking at doing some more pre-processing based on its value, need to get save it as a variable/attribute. I am getting following exception:



[ERROR] [12/02/2014 15:03:10.326] [GatlingSystem-akka.actor.default-dispatcher-11] [akka://GatlingSy
stem/user/$e] key not found: Variable.random()
java.util.NoSuchElementException: key not found: Variable.random()
        at scala.collection.MapLike$class.default(MapLike.scala:228)
        at scala.collection.AbstractMap.default(Map.scala:58)
        at scala.collection.MapLike$class.apply(MapLike.scala:141)
        at scala.collection.AbstractMap.apply(Map.scala:58)
        at io.gatling.core.session.SessionAttribute.as(Session.scala:40)
        at quickTest$$anonfun$1.apply(quickTest.scala:144)
        at quickTest$$anonfun$1.apply(quickTest.scala:143)
        at io.gatling.core.action.SessionHook.executeOrFail(SessionHook.scala:35)
        at io.gatling.core.action.Failable$class.execute(Actions.scala:71)
        at io.gatling.core.action.SessionHook.execute(SessionHook.scala:28)
        at io.gatling.core.action.Action$$anonfun$receive$1.applyOrElse(Actions.scala:29)
        at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:166)
        at akka.actor.Actor$class.aroundReceive(Actor.scala:465)
        at io.gatling.core.akka.BaseActor.aroundReceive(BaseActor.scala:23)
        at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
        at akka.actor.ActorCell.invoke(ActorCell.scala:487)
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)
        at akka.dispatch.Mailbox.run(Mailbox.scala:220)
        at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala
:393)
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

15:03:10.346 [ERROR] i.g.c.a.b.SessionHookBuilder$$anonfun$build$1$$anon$1 - Action anonfunbuild1ano
n1 crashed on session Some(Session(

Sameer Mainkar

unread,
Dec 2, 2014, 5:05:16 AM12/2/14
to gat...@googlegroups.com
I am saving the Variable Map with following:

.check(jsonPath("$.entries[*].id").findAll.saveAs("Variable")))

Also I am able to use with Variable.random as below

.get(uri + """/${Variable.random()}""")

and following session.set is also working,  so its problem when I am trying to extract an element from a vector and then save.


.exec(session => {
   val VariableSelect = session("test").as[String]
   session.set("VariableSelect",VariableSelect)
})

मंगलवार, 2 दिसम्बर 2014 3:13:33 pm UTC+5:30 को, Sameer Mainkar ने लिखा:

Excilys

unread,
Dec 2, 2014, 6:52:45 AM12/2/14
to gat...@googlegroups.com
Please read the Expression documentation: it only works in some specific places. You wouldn't expect JSP's EL to work out of the box in standard Java code.
--
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/d/optout.

Sameer Mainkar

unread,
Dec 2, 2014, 9:22:04 AM12/2/14
to gat...@googlegroups.com
Ok, is there a way to do this in Gatling.

मंगलवार, 2 दिसम्बर 2014 5:22:45 pm UTC+5:30 को, Stéphane Landelle ने लिखा:

Abhinav Gogna

unread,
Dec 2, 2014, 10:03:49 AM12/2/14
to gat...@googlegroups.com
Have you tried saving it as a vector in session and getting the element from index.

something like

.exec(session => {
        val VariableSelect = session("test").as[Vector[String]]
        session.set("VariableSelect",VariableSelect(2)) //whatever index your element is saved at.
        })

Unless I don't the question, it could be simple as that.

Stéphane Landelle

unread,
Dec 2, 2014, 11:17:33 AM12/2/14
to gat...@googlegroups.com
Yep. Just use ThreadLocalRandom to compute a random index.

Sameer Mainkar

unread,
Dec 2, 2014, 12:46:18 PM12/2/14
to gat...@googlegroups.com
Thanks Abhinav. Was not aware of ThreadLocalRandom that can be used in this case, hence had ruled out that option, since wanted to go with random selection. Tried ThreadLocalRandom and its working, though had to use different format.

Any idea why Approach 1 didn't worked:

Approach 1: Didn't worked. Got error while compiling had included "import scala.concurrent.forkjoin.ThreadLocalRandom"
not found: value Variable
23:14:10.759 [ERROR] i.g.a.ZincCompiler$ -       Variable <- session("Variable").validate[Seq[String]]
23:14:10.759 [ERROR] i.g.a.ZincCompiler$ -       ^
23:14:10.774 [ERROR] i.g.a.ZincCompiler$ - one error found

.exec(session => {
 for {
  
Variable <- session("Variable").validate[Seq[String]]
  val VariableSelect = Variable(ThreadLocalRandom.current.nextInt(Variable.size))
  }yield session.set("VariableSelect",VariableSelect)
})

Approach 2: -----------> This worked

.exec(session => {
      session("Variable").validate[Seq[String]] match {
        case Success(ids) => 
          val index = ThreadLocalRandom.current.nextInt(ids.size)
          session.set("VariableSelect", ids(index))
        case _ => session
      }
    })

Thanks.

मंगलवार, 2 दिसम्बर 2014 9:47:33 pm UTC+5:30 को, Stéphane Landelle ने लिखा:

Michael Goudzenko

unread,
Feb 13, 2018, 8:47:39 AM2/13/18
to Gatling User Group
Just bring set outside braces
var VariableSelect = ""
.....
.exec(session => {
   VariableSelect = session("Variable.random()").as[String]
   session
}.set("VariableSelect",VariableSelect))



вторник, 2 декабря 2014 г., 11:43:33 UTC+2 пользователь Sameer Mainkar написал:
Reply all
Reply to author
Forward
0 new messages