Gatling2: How can I read from the virtual user's session

756 Views Asked by At

I realise that I can save the response body in the virtual user's session:

  val session: Session = Session("MySCN", "123")
  val scn = scenario("MySCN")
    .exec(http("my_request")
    .post(serverURL)
    .headers(headers)
    .body(InputStreamBody(Helper.getByteArrayInputStream))
    .check(status.is(200), bodyBytes.saveAs("responseBody")))

   //key not found...
   session("responseBody").as[ByteArray]

How can I read that responseBody from this (implicit?) Sesssion? I created an explicit Session as well...

Edit:

Based on the answer I have clarified my scenario. In the answer I do not know how the function transformBytes works.

2

There are 2 best solutions below

0
On BEST ANSWER

Based on the answer form Stephane this is what worked for me:

 val scn = scenario("MySCN")
    .exec(http("my_request")
    .post(serverURL)
    .headers(headers)
    .body(InputStreamBody(Helper.getByteArrayInputStream))
    .check(status.is(200), bodyBytes.saveAs("responseBody")))

    .exec(session=>{
    val theResponse = session("responseBody").validate[Array[Byte]]
    //Analyse theResponse...
    val bas = new ByteArrayInputStream(theResponse.get)
    //... and make sure to return the session
    session
  })

So no explicit user session is required.

0
On

Either pass a function to a Gatling DSL method:

.body(ByteArrayBody(session => session("responseBody").validate[Array[Byte]].map(transformBytes)))

or do this in an exec block and store back the transformation result in a new attribute

exec { session =>

  session("responseBody").validate[Array[Byte]]
    .map(transformBytes)
    .map(newBytes => session.set("newBytes", newBytes))
}

Beware that you might want to remove original bytes from the session so they don't linger too long in memory and end up in the old gen.