Gatling Performance Testing Tool

 Gatling




What is Gatling ?

Gatling is a stress tool. Development is currently focusing on HTTP support.


Gatling is an open-source load and performance testing framework based on ScalaAkka and Netty. The first stable release was published on January 13, 2012. In 2015, Gatling's founder, Stéphane Landelle, created a company (named "Gatling Corp"), dedicated to the development of the open-source project. Gatling officially presented Gatling FrontLine, Gatling's Enterprise Version with additional features.

The software is designed to be used as load testing tool for analyzing and measuring the performance of a variety of services, with a focus on web applications.


Gatling Corp develops the open-source performance testing tool for web applications, Gatling, and its enterprise version, Gatling FrontLine. The Project's aims include;

  • High performance

  • Ready-to-present HTML reports

  • Scenario recorder and developer-friendly DSL



Download And Installation :

Download URL : https://gatling.io/download/


Using the Recorder

To ease the creation of the scenario, we will use the Recorder, a tool provided with Gatling that allows you to record your actions on a web application and export them as a Gatling scenario.

The bundle structure is as following:

bin contains the launch scripts for Gatling and the Recorder.

conf contains the configuration files for Gatling, Akka and Logback.

lib contains the binaries used by Gatling

user-files:

simulations contains your Simulations scala files. Please respect package folder hierarchy.

data contains feeder files.

bodies contains templates for request bodies.

results contains simulation.log and reports generated in a sub directory.



This tool is launched with a script located in the bin directory:


On Linux : $GATLING_HOME/bin/recorder.sh

On Windows : %GATLING_HOME%\bin\recorder.bat


  1. Click recorder.bat 




  1. Set the port number in your browser and same port number should be in your gatling tool.

  2. Click Start button and another window will be open.



  1. Here gatling will record the all browser actions. And we can give the transaction naming.

  2. After completing the recording we can click Stop and Save.

  3. All our script will be saved in .scala format. This scala script file you can see in \gatling-charts-highcharts-bundle-3.0.3\user-files\simulations path.

  4. Here we have recorded basic login script in orange HRM application.

  5. Our script will be like below.



import scala.concurrent.duration._


import io.gatling.core.Predef._

import io.gatling.http.Predef._

import io.gatling.jdbc.Predef._


class OrangeAddUserScript extends Simulation {


val httpProtocol = http

.baseUrl("http://172.16.203.66:8089")

.inferHtmlResources()

.acceptHeader("image/webp,image/apng,image/*,*/*;q=0.8")

.acceptEncodingHeader("gzip, deflate")

.acceptLanguageHeader("en-US,en;q=0.9")

.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36")


val headers_0 = Map(

"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",

"Proxy-Connection" -> "keep-alive",

"Purpose" -> "prefetch",

"Upgrade-Insecure-Requests" -> "1")


val headers_1 = Map(

"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",

"Cache-Control" -> "max-age=0",

"Origin" -> "http://172.16.203.66:8089",

"Proxy-Connection" -> "keep-alive",

"Upgrade-Insecure-Requests" -> "1")


val headers_2 = Map("Proxy-Connection" -> "keep-alive")


val headers_8 = Map(

"Accept" -> "*/*",

"Proxy-Connection" -> "keep-alive",

"X-Requested-With" -> "XMLHttpRequest")


val headers_11 = Map(

"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",

"Proxy-Connection" -> "keep-alive",

"Upgrade-Insecure-Requests" -> "1")




val scn = scenario("OrangeAddUserScript")

// Launch

.exec(http("request_0")

.get("/orangehrm/")

.headers(headers_0))

.pause(13)

// Login

.exec(http("request_1")

.post("/orangehrm/symfony/web/index.php/auth/validateCredentials")

.headers(headers_1)

.formParam("actionID", "")

.formParam("hdnUserTimeZoneOffset", "5.5")

.formParam("_csrf_token", "d329ce0f72b833f225c1660185464f58")

.formParam("txtUsername", "Admin")

.formParam("txtPassword", "Admin@123")

.formParam("Submit", "LOGIN")

.resources(http("request_2")

.get("/orangehrm/symfony/web/webres_57eb84702e3066.16112222/themes/default/images/welcome-down.png")

.headers(headers_2),

            http("request_3")

.get("/orangehrm/symfony/web/webres_57eb84702e3066.16112222/themes/default/images/sub-nav-arrow-n.png")

.headers(headers_2),

            http("request_4")

.get("/orangehrm/symfony/web/webres_57eb84702e3066.16112222/themes/default/images/sub-nav.png")

.headers(headers_2),

            http("request_5")

.get("/orangehrm/symfony/web/webres_57eb84702e3066.16112222/themes/default/images/nav-bg.png")

.headers(headers_2),

            http("request_6")

.get("/orangehrm/symfony/web/webres_57eb84702e3066.16112222/orangehrmDashboardPlugin/images/loading.gif")

.headers(headers_2),

            http("request_7")

.get("/orangehrm/symfony/web/webres_57eb84702e3066.16112222/themes/default/images/h1-bg.png")

.headers(headers_2),

            http("request_8")

.get("/orangehrm/symfony/web/index.php/dashboard/employeeDistribution")

.headers(headers_8),

            http("request_9")

.get("/orangehrm/symfony/web/index.php/dashboard/pendingLeaveRequests")

.headers(headers_8)))

.pause(11)

// Logout

.exec(http("request_10")

.get("/orangehrm/symfony/web/webres_57eb84702e3066.16112222/themes/default/images/welcome-up.png")

.headers(headers_2))

.pause(1)

.exec(http("request_11")

.get("/orangehrm/symfony/web/index.php/auth/logout")

.headers(headers_11))


setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)

}



Correlation : 

In Gatling you have to use checks to extract a value from response.Below are the expression for correlation.

.check(css( selector = "input[name='_csrf_token']", "value").saveAs("authToken")) 



Request Response:

In Gatling tool for request response also we have to use coding. Below are the code for request response. So that we can easily find the values in response.

.check(bodyString.saveAs ( key = "BODY")))
.exec
{ session =>                
println(session("BODY").as[String])
session
}



Parametrization : 

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.

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:

  1. The created CSV Datasource should be in the following /user-files/data folder.

  2. First line of the source file must be the labels.


Sample: Create CSV Datasource as “login.csv”


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.

  • val csvFeeder = csv ( fileName = "data/login.csv")


Feed below expression in Login Page


.feed (csvFeeder


And pass the parameters in script.



Validation Rules (Assertions): 

Text Validation

check(regex("""<h1>Dashboardd</h1>""").find.is("<h1>Dashboardd</h1>"))




Increase User Load: Here we can give the user load.

setUp(scn.inject(atOnceUsers(5))).protocols(httpProtocol


Load test Types:

setUp(

  scn.inject(

    nothingFor(4 seconds), // 1

    atOnceUsers(10), // 2

    rampUsers(10) during (5 seconds), // 3

    constantUsersPerSec(20) during (15 seconds), // 4

    constantUsersPerSec(20) during (15 seconds) randomized, // 5

    rampUsersPerSec(10) to 20 during (10 minutes), // 6

    rampUsersPerSec(10) to 20 during (10 minutes) randomized, // 7

    heavisideUsers(1000) during (20 seconds) // 8

  ).protocols(httpProtocol)

)


Reference link : https://gatling.io/docs/current/general/simulation_setup/ 


Gatling Ramp Users:

setUp(scn.inject(rampUsersUsers(10) over (5 seconds))).protocols(httpProtocol)


Gatling 10 Concurrent Users:

setUp(scn.inject(atOnceUsers(3))).protocols(httpProtocol)


Gatling St the Load Test Duration :

setUp(scn.inject(constantUsersPerSec(100) during (30 minutes))).throttle(
  reachRps(100) in (10 seconds),
  holdFor(1 minute),
  jumpToRps(50),
  holdFor(2 hours)
)


Maximum duration

Finally, you can configure the maximum duration of your simulation with the method maxDuration. It is useful if you need to bound the duration of your simulation when you can’t predict it.

setUp(scn.inject(rampUsers(1000) over (20 minutes))).maxDuration(10 minutes)



Gatling Assertions :

setUp(scn).assertions(

  global.responseTime.max.lt(50),

  global.successfulRequests.percent.gt(95)

)



// Assert that the max response time of all requests is less than 100 ms

setUp(scn).assertions(global.responseTime.max.lt(100))


// Assert that every request has no more than 5% of failing requests

setUp(scn).assertions(forAll.failedRequests.percent.lte(5))


// Assert that the percentage of failed requests named "Index" in the group "Search"

// is exactly 0 %

setUp(scn).assertions(details("Search" / "Index").failedRequests.percent.is(0))


// Assert that the rate of requests per seconds for the group "Search"

setUp(scn).assertions(details("Search").requestsPerSec.between(100, 1000))


Correlation:

.check(regex("content=\\\"(?<bidkid>[0-9A-Z]*)\\\"").find.saveAs("token"))


Comments

Popular posts from this blog

LoadRunner Tool

Selenium Performance Testing with JMeter and Selenium Grid

JMeter Integration with AppDynamics