Why Scala for Gatling?

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.

Concepts of Scala

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.

Scala Syntax:

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}

Data Types:

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?

  • Int :32 bit signed value. Range -2147483648 to 2147483647 // change
  • Byte:8 bit signed value. Range from -128 to 127
  • Short: 16 bit signed value. Range -32768 to 32767
  • Long:64 bit signed value. -9223372036854775808 to 9223372036854775807
  • Float:32 bit IEEE 754 single-precision float
  • Double:64 bit IEEE 754 double-precision float
  • Char:16 bit unsigned Unicode character. Range from U+0000 to U+FFFF

Strings and Arrays

Strings and arrays in Scala are not very different from Java. In addition, in Scala, it is possible to define a multi-line literal.

Variables :

Variable declaration is done in the following way:

val or val VariableName : DataType [= Initial Value]

Access modifiers :

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.


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:

  • Arithmetic Operator : +, -, *, /, %
  • Relational Operators :==, !=, >, <, >=, <=
  • Logical Operators :&&, |, !
  • Bitwise Operators :&, |, ^, ~, <<, >>, >>>
  • Assignment Operators :=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

Conditional statement

if (condition) {

if (condition) {
} else {

if (condition1) {
} else if (condition2) {
} else {

Loop Statements

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),
  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:

  • reachRps(target) in (duration): It says that, target a throughput with a ramp over a given duration.
  • jumpToRps : It says, jump immediately to a given targeted throughput.
  • holdFor(duration): It means, holds the current throughput for a given duration.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions