Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Understanding Gatling Scripts: A Use Case

DZone's Guide to

Understanding Gatling Scripts: A Use Case

In the last tutorial, we taught you how to record your Gatling script, and in this article, we provide a breakdown of what it means.

· Open Source Zone ·
Free Resource

DON’T STRESS! Assess your OSS. Get your free code scanner from FlexeraFlexNet Code Aware scans Java, NuGet, and NPM packages.

After we learned how to record a Gatling script, this blog post will dive into the details of that script and explain it. The purpose is to understand the different parts so you will be able to write your own load testing script in Gatling without having to record it. We will cover four parts of the script: setup, HTTP configuration, scenario, and simulation.

Let's get started. We will explain the script we recorded in the previous blog:

package blazemeter

import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class BlazemeterPricing extends Simulation {

	val httpProtocol = http
		.baseURL("http://www.demoblaze.com")
		.inferHtmlResources()
		.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
		.acceptEncodingHeader("gzip, deflate")
		.acceptLanguageHeader("vi,en-US;q=0.9,en;q=0.8")
		.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")

	val headers_0 = Map("Upgrade-Insecure-Requests" -> "1")

	val headers_1 = Map(
		"Accept" -> "*/*",
		"Origin" -> "http://www.demoblaze.com")

	val headers_2 = Map("Accept" -> "image/webp,image/apng,image/*,*/*;q=0.8")

    val uri1 = "https://ajax.googleapis.com/ajax/libs/jquery"
    val uri2 = "https://unpkg.com"
    val uri3 = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"

	val scn = scenario("BlazemeterPricing")
		.exec(http("request_0")
			.get("/")
			.headers(headers_0)
			.resources(
				http("request_1")
					.get("/css/fonts/Lato-Regular.woff2")
					.headers(headers_1),
				http("request_2")
					.get("/imgs/front.jpg")
					.headers(headers_2)))
		.pause(8)
		.exec(http("request_3")
			.get("/cart.html")
			.headers(headers_0)) 
	setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
	
}


If you don't know Scala, this script might be intimidating. But don't worry. Let's look into how the script is structured. A closer look shows us the script is divided into four sections:

  • Setup - preconfiguration and class declaration
  • HTTP Configuration - configuring HTTP and variables
  • Scenario - the story executed
  • Simulation - the test configurations

This step configures the package name to hold the scala class, indicates the required imported libraries and shows the class declaration.

In this script, the package has the name "blazemeter". This is an optional requirement.

package blazemeter

import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class BlazemeterPricing extends Simulation {
 


Then, import common libraries:

  • scala.concurrent.duration._
  • io.gatling.core.Predef._
  • io.gatling.http.Predef._
  • io.gatling.jdbc.Predef._

These libraries are stored in the Lib directory of Gatling. Each library contains commands that we use in the script. If you don't import libraries, the script will fail to compile because the compiler does not know where to define commands.

For example, assume we don't import the library scala.concurrent.duration._ that contains the  (.pause())  method. So, when compiling, the script will fail because it does not recognize the  (.pause())  method.

The next line we define is a class named BlazemeterPricing with the extended Gatling class Simulation class BlazemeterPricing extends Simulation.

This step we will define the HTTP configurations and variables for the script, like: the protocol, URL, headers. These will be used as the headers of the API endpoint. We will clarify them line by line.

val httpProtocol = http
		.baseURL("http://www.demoblaze.com")  //1
		.inferHtmlResources() //2
		.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") //3
		.acceptEncodingHeader("gzip, deflate")  //4
		.acceptLanguageHeader("vi,en-US;q=0.9,en;q=0.8") //5
		.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")  //6

	val headers_0 = Map("Upgrade-Insecure-Requests" -> "1") //7

	val headers_1 = Map(
		"Accept" -> "*/*",
		"Origin" -> "http://www.demoblaze.com")  //8

	val headers_2 = Map("Accept" -> "image/webp,image/apng,image/*,*/*;q=0.8")   //9

    val uri1 = "https://ajax.googleapis.com/ajax/libs/jquery"   //10
    val uri2 = "https://unpkg.com"  //11
    val uri3 = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"  //12


  • 1 - The main URL that will prepend all URLs.
  • 2 - Parses HTML to find embedded resources and load them asynchronously.
  • 3,4,5,6 - Common headers that will be sent with all requests. For more details on HTTP header fields, please click here.
  • 7,8,9 - Defines headers to pass to the individual request. We will see how they can be used in the scenario section.
  • 10,11,12 - Some constant variables. These variables are auto created by recording. They are not necessary in this script so we can remove them.

The option you will often change is  baseURL(//1), the others are kept as default for most cases.

Scenario

In this section we will define the actual scenario that will be executed. Let's take a look at it line by line.

val scn = scenario("BlazemeterPricing")  //1
		.exec(http("request_0")  //2
			.get("/")  //3
			.headers(headers_0)  //4
			.resources(  //5
				http("request_1")  //6
					.get("/css/fonts/Lato-Regular.woff2")  //7
					.headers(headers_1),  //8
				http("request_2")  //9
					.get("/imgs/front.jpg")  //10
					.headers(headers_2)))  //11
		.pause(8)  //12
		.exec(http("request_3")  //13
			.get("/cart.html") //14
			.headers(headers_0))  //15
 


  • 1 - Defines a variable named scn. It is assigned to the syntax scenario that has name BlazemeterPricing.
  • 2 - Calls  .exec() method. It comes with the syntax http and has the name  request_0. This name will appear in the report.
  • 3 - Executes the  .get()method and calls the endpoint with the URL http://www.demoblaze.com.
  • 4,8,11,15- Sends defined headers to individual requests that are mentioned in the http configuration section above.
  • 5- Fetches resources in parallel to emulate the behavior of a real web browser.
  • 6,9- New requests named request_1, request_2  
  • 7,10- Starts a new .get() method and calls endpoints with the URL http://www.demoblaze.com/css/fonts/Lato-Regular.woff2 and http://www.demoblaze.com/imgs/front.jpg
  • 12 - Pauses 8 secs
  • 13 - A new call with the  .exec()  method with the name  request_3
  • 14 - A new  .get()method that calls the endpoint with the URL http://www.demoblaze.com/cart.html

The scenario describes two actions of user behavior. First, the user browses to the page http://www.demoblaze.com. Second, the users clicks on the "Cart" text link http://www.demoblaze.com/cart.html after waiting 8 secs. The browser will send 2 GET method API endpoints to the server along with the headers declaration in the HTTP configuration section.

Simulation

According to your scenario, you can edit/change the code to suit the behavior you want to test.

In the final section of the Gatling script, we introduce a scenario profile that will be executed in the load test. The configuration for the scenario profile indicates parameters for the load test like which protocol to use, how many concurrent users to run, the ramp-up period, etc.

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
  •  setUp : Gatling syntax for this part
  •  scn : The variable that we created in the scenario section
  •  inject(atOneUsers(1)): Configures the users for the load test. In this example, it configures one user for load test
  •  protocols : Gatling syntax
  •  httpProtocol : The variable that we defined in the HTTP configuration section

You can customize parameters to meet your load test purpose. For more simulation configuration information please click here.

I hope this blog is helpful for you to write your own Gatling script for load testing!

Running Your Gatling Script in BlazeMeter

After creating your Gatling script, you can achieve massive scalability by running your Scala file in BlazeMeter and ramping up to 1,000s or 1,000,000s of users from scores of geo-locations worldwide. You will also get more advanced reporting that you can drill down into and compare over time, sharing and collaboration on tests and reports, and the ability to shift left.

Try FlexNet Code Aware Today! A free scan tool for developers. Scan Java, NuGet, and NPM packages for open source security and license compliance issues.

Topics:
load testing ,gatling script ,open source ,gatling framework ,performance testing ,scala file ,gatling package ,gatling ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}