
Introduction
Gatling is a free and open source load testing tool to analyse and measure the performance of services that we use in web applications. Using Gatling, we can simulate the presence of over a 1000 users on a single machine with high concurrency scenarios. To describe simulation, we can use light weight Domain Specific Language (DSL) written in Scala. Simulations are for the load test. It is constituted of varied scenarios, each scenario representing specific user behavior.
Gatling Tree Diagram
The Gatling installation folder is organized according to the following tree diagram:
- /results: contains the benchmark results in web format
- /bin: contains the scripts for launching Gatling
- /target: contains the files resulting from the compilation of our scenarios + cache
- /conf: contains configuration files (log level)
- /user-files: contains the .scala scenario definition files
- /lib: Gatling jar
Features ensuring effective simulation in Gatling:
- DSL for simulation written in Scala to treat performance as a code.
- Support of HTTP(S) protocols & can also be used for JDBC & JMS load testing.
- Produces higher load by using an asynchronous non-blocking approach.
- Supports input files CSV, TSV, SSV to access random data for Data-Driven tests.
- Generation of graphical reports using Graphite in an understandable manner.
Use Case
Let’s try to test the performance of the services by parameterizing input data taken from a CSV Datasource and run automatically defined scenarios with multiple numbers of simultaneous players.
What we need to do
- Prerequisites
- Create CSV Datasource input file in the /user-files/data folder.
- Simulate 100 services, to enable execution of each of the services with their own credentials.
Solution
Prerequisites
- Install Scala language: language that is used to describe Simulation in Gatling.
- Location: http://www.scala-lang.org/download/
- Install JDK – Version: jdk1.7.0_60
- Download and Run Gatling
- Download Gatling version 2.0.1 from their website and then unpack it:
- Location: http://gatling.io/download/
Parameterize API service with Feeder
To Parameterize the API service with more input data using CSV Datasources, Feeder is used. Feeder is an Iterator [Map[String, T]] , which injects data into the session, every time it reaches the service Map[String, T] and shares data with all the users.
Step 1: Create CSV Datasource
- Data sources are normally Array[Map[String, T]] – Map keys are names of attributes.
- Gatling provides support for CSV separated values file. The CSV ( filename: String ) method, can be used to parse the files.
Note:
- The created CSV Datasource should be in the following /user-files/data folder.
- First line of the source file must be the labels.
Sample: Create CSV Datasource as “token.csv”
username,password,tokens username1,password1,token1 username2,password2,token2
Step 2: Generating Feeders from the Datasource
- Gatling provides Built-ins for converting Data Source into Feeders.
- We can use Random Strategy to avoid feeder starvation. In this strategy, a random record is taken out from the feeder and injected into the session.
Sample:
val token_feeder=csv("tokens.csv").random
Step 3: Inject Datasource values into the User Session
- Use the Feed method to inject user created CSV Datasources. Structure for the feed method is .feed (feeder_source).
- Every time the user reaches this step, he takes record from the feeder and injects it into the user scenario to create a new session instance.
Sample:
//User scenario starts val scn = scenario("User Service") //a record is popped from feeder and injected into the user's session .feed(token_feeder) //Executing the service .exec(http("Service_Request") //defines the label name, to get the data from the header .get("""/service/token/"${tokens}"?version=v2""")) //"tokens" is the label name in the csv file.
Step 4: Load Scenarios for execution with multiple number of simultaneous players.
- User data injection is done with the Inject method. This method takes an argument as a sequence of injection steps, that will be processed sequentially.
Sample:
//Injects users at a constant rate, defined in users per second, during a given duration. Users will be injected at regular intervals. setUp(scn.inject(constantUsersPerSec(10) during(15 seconds)).protocols(httpProtocol))
Define Expected Results
Step 5: Creating Global Assertions
- Assertions are used to verify if global statistics, like service response time or number of failed requests match expectations for a whole simulation.
Sample:
assertThat( global.responseTime.max.lessThan(5550) global.successfulRequest.percent.greaterThan(95) )
Step 6: Run the specific simulation file and get the pretty reports.
Conclusion
One of the important advantages of Gatling is the generation of high quality graphic reports at the end of each simulation. It helps testers view reports immediately, without having to do any additional work.Feeders is another good feature of the Gatling tool which helps parsing of parameters in separate files.Overall, the Gatling DSL tool is designed for ease of use, high performance, enabling us handle complex scenarios in an efficient manner, along with maintainability.
Reference
- Feeders in Gatling:
http://gatling.io/docs/2.0.0-RC2/session/feeder.html
- Configuration of Virtual users:
http://gatling.io/docs/2.0.2/advanced_tutorial.html?highlight=configure%20virtual%20users
- Reports in Gatling:
http://gatling.io/docs/2.0.2/general/reports.html?highlight=reports