Table of content

What is a Session?

Technically speaking, session is the state of a virtual user. It is basically, a map with key strings. As, MAP[string, anystr]. Entries in a map is called Session attributes.

Session is the message that are passed along with a scenario workflow. It has to be kept in mind that, any Gatling scenario, is always backed by a Akka actor. In the process of injecting data, we mainly inject the states inside the virtual users. Below are the ways of doing that?

  • Using Feedeers.
  • Saving the extracted data from responses.
  • Using the session API manually.

The injected data may be also retrieved with the way called fetching data. There can be two ways for fetching the data. One is using the Gatling's Expression Language or also it can be done manually with the Session API.

Session API

Let us see how to set attributes. First, we see that the session has the following methods:

  • set(key: String, value: Any): Session :Used for adding or replacing any attribute.
  • setAll(newAttributes: (String, Any)*): Session :Bulk add or replace attributes.
  • setAll(newAttributes: Iterable[(String, Any)]): Session :Work as above, but takes an Iterable instead of a varags.
  • reset : Used to reset all attributes.

This is to keep in mind that Sessions are not mutable, because it's messages are dealt within a multi-threaded way of concurrency. And therefore the best way to deal with the state is to rely on immutability.

Below let us see, an example of a session:


									 
									 val session: Session = ??? // it is a wrong usages
									 session.set("foo", "FOO") // wrong: the result of this set call is just discarded
                                     session.set("bar", "BAR") // this is a proper use.
									 session.set("foo", "FOO").set("bar", "BAR")
									 
									 

Getting a attribute:

We create a session instance variable named session contains a String attribute named "foo".


									 val session: Session = ???
									  

Then we see what happens:


									 val attribute: SessionAttribute = session("foo")
									 

In the above procedure we shall see that the (session "foo") does not return any value, but it returns a wrapper. We can access the methods to retrieve the actual value in several ways.

For example, if we take the functions as session("foo").as[Int] This function will return a integer. And will throw a NoSuchElementException if the foo attribute is undefined. The function also throws a NumberFormatException only if the value is a String can't be parsed into a string.

Moreover, this function throws a ClassCastException , if and only if the value is not an integer.

In our next example, let us take our function as session("foo").asOption[Int]:. This function returns an Option[Int]. Now this can be none if the foo attribute is or case in undefined. And which is Some(value) in all other cases, and the value is an integer, or it can be a String which can be passed as a String.

The function throws a NumberFormatException if the value is a String which is not possible to parsed into s string. Lastly, this function throws a ClassCastException in all other cases.

Let us take the example of another function session("foo").validate[Int]. This function returns an validation integer.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions