In most load testing use cases, it’s important that the virtual users don’t play the same data. Otherwise, you might end up not testing your application but your caches.
Moreover, if you’re running an application on a Java Virtual Machine, the Just In Time compiler (JIT) will make dramatic optimizations and your system will behave very differently from your actual one.
Though, you have to make your scenario steps dynamic, based on virtual user specific data.
Session is a virtual user’s state.
Basically, it’s a
Map[String, Any]: a map with key Strings.
In Gatling, entries in this map are called Session attributes.
Remember that a Gatling scenario is a workflow where every step is backed by an Akka Actor?
Session is actually the message that are passed along a scenario workflow.
The first step is to inject state into the virtual users.
There’s 3 ways of doing that:
Once you have injected data into your virtual users, you’ll naturally want retrieve and use it.
There’s 2 ways of doing that:
If Gatling complains that an attribute could not be found, check that:
Session has the following methods:
set(key: String, value: Any): Session: add or replace an attribute
setAll(newAttributes: (String, Any)*): Session: bulk add or replace attributes
setAll(newAttributes: Iterable[(String, Any)]): Session: same as above but takes an Iterable instead of a varags
reset: reset all attributes
Session instances are immutable!
Why is that so? Because Sessions are messages that are dealt with in a multi-threaded concurrent way, so immutability is the best way to deal with state without relying on synchronization and blocking.
A very common pitfall is to forget that
setAll actually return new instances.
val session: Session = ??? // wrong usage session.set("foo", "FOO") // wrong: the result of this set call is just discarded session.set("bar", "BAR") // proper usage session.set("foo", "FOO").set("bar", "BAR")
Let’s say a Session instance variable named session contains a String attribute named “foo”.
val session: Session = ???
val attribute: SessionAttribute = session("foo")
session("foo") doesn’t return the value, but a wrapper.
You can then access methods to retrieve the actual value in several ways:
NoSuchElementExceptionif the “foo” attribute is undefined,
ClassCastExceptionif the value is not a String
Noneif the “foo” attribute is undefined,
Some(value)otherwise and value is indeed a String
Failure(errorMessage)if the “foo” attribute is undefined
Failure(errorMessage)if the value is not a String
as will probably easier for most users.
It will work fine, but the downside is that they might generate lots of expensive exceptions once things starts going wrong under load.
We advise considering
validate once accustomed to functional logic as it deals with unexpected results in a more efficient manner.