Gatling, the load and performance testing tool is solely based in Scala. There is a myth that to use Gatling no Scala knowledge is required. But neither it's fully true nor fully correct. If we are naive users, and just require the html report, it's okay not to know about Scala.
But as we know, that Gatling provides us a Scala report also, along with the html. Therefore, it is required to study the basics of Scala at least to be able to understand the Scala script and use some functionalities such as assertions, feeders, etc.
In this study, we will confine our studies, only to the basics of Scala programming and discuss those functions, which are somehow related to Gatling.
To use Scala, it's compiler needs to be installed from the official website of Scala. Scala source code is compiled to produce Java Byte Code and run on Java Virtual Machine.
Scala is an object-oriented programming language. All the things found in Scala is considered as an object. No primitive types exist, as exists in Java or in other programming languages. All the functions here are considered to be objects.
The syntax of Scala is the same as that of Java. Scala is statistically types but checks are done at compile-time, and sometimes we do not require explicitly declare variable types also, and the Scala compiler knows it. Scala is case sensitive, class names are CamelCase with the first letter being capital, the method names are also in camel case, which starts with a lower case letter.
It should be noted that semicolon ";" is not required if there is only one expression in a line. If there is more than one expression in a line, then those can be separated with a semicolon.
The class file name should be the same name, as the class itself. The import of classes is done from its packages and ends with an underscore. Let us see an example of an import:
import io.gatling.core.Predef_
Let us also see how we can import several classes form a package:
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter}
As already mentioned anything in Scala is considered as an object and so is in the case of the data types also. All the data types here is considered as an object. Let us see some frequently used data types in Scala?
Strings and arrays in Scala are not very different from Java. In addition, in Scala, it is possible to define a multi-line literal.
Variable declaration is done in the following way:
val or val VariableName : DataType [= Initial Value]
There are three access modifiers present in Scala, which are public, private, and protected. It may be noted that there is no explicit keyword public. By default, if no modifier is used then the access level is automatically considered as public. On the other hand, private and protected can be defined for a specific scope.
Let us see, in the below example, where we see how to declare anything with a specific scope.
private[com.automationrhpasody.gatling]
As shown the in above example, a modifier is applied to the class or method, may be considered private for all other elements in the program except for the class or method itself, to which the modifier is applied.
Operators in Scala are not very different from Java. They are summarized below:
if (condition) {
statement
}
if (condition) {
statement1
} else {
statement2
}
if (condition1) {
statement1
} else if (condition2) {
statement2
} else {
statement3
}
Loop statements in Scala are also for, while and do/while. Let us see below a simple example of a for loop:
for (i <- 1 until 10) {
println("i=" + i)
}
For example:
val scn = scenario("test01")
This statement has a user-defined variable name scn which holds the entire properties of the scenario.
setUp(scn.inject(constantUsersPerSec(100) during (30 minutes))).throttle(
reachRps(100) in (10 seconds),
holdFor(1 minute),
jumpToRps(50),
holdFor(2 hours)
)
In the above code, the setUp function injects into the scenario scn, the return value of the functions constantUsersPerSec , during, throttle, reachRps, holdFor and jumpToRps respectively.
This simulation will go up to 100 requests per second with a ramp of 10 seconds, then it should hold the throughput for 60 seconds, that is 1 minute, then jump to 50 requests per second, and lastly hold this throughput for two hours.
Below is a short explanation of the functions: