Passing Data Into Gatling from the Command Line

A few months back I covered Gatling in a basic review. This time around we will dive a bit deeper into creating a Gatling (3.0) test simulation.

Basic Setup

Since writing the previous article about Gatling back in July the 3.0-RC4 version has been released. As far as I can tell the process for recording traffic has not changed at all. The majority of changes seem to be internal to the request/response engine and some function renaming. Checkout the official change log here.

To get us up and running quickly I created a load simulation file that is used in the rest of the article. The file contents are below.

import scala.concurrent.duration._

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

class PassingDataIntoGatling extends Simulation {

	val target = System.getProperty("target")

	val httpProtocol = http
		.baseUrl("http://" + target)
		.inferHtmlResources()
		.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
		.acceptEncodingHeader("gzip, deflate")
		.acceptLanguageHeader("en-US,en;q=0.5")
		.doNotTrackHeader("1")
		.userAgentHeader("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0")

	val headers_0 = Map(
		"Pragma" -> "no-cache",
		"Upgrade-Insecure-Requests" -> "1")

	val scn = scenario("PassingDataIntoGatling")
		.exec(
            http("request_0").get("/").headers(headers_0)
        )

	setUp(
		scn.inject(
			constantUsersPerSec(10) during (5 seconds)
		).protocols(httpProtocol)
	)
}

Save this file as /path/to/gatling/user-files/simulations/PassingDataIntoGatling.scala . This way Gatling will find the simulation class automatically; making the rest of the process much easier.

Create the Global Variable

Let’s start with the highest level of abstraction. In order to pass data to Gatling we first need to provide it to JAVA. This is accomplished by using the JAVA_OPTS global variable (Galting is a Java program that uses Scala scripting to execute HTTP requests with HTML/JS output. #abstraction) The syntax to set a JAVA_OPTS  is as follows:

JAVA_OPTS="-DVARIABLE_NAME=VARIABLE_VALUE"

Take note of the -D inside the quotes. That was not on accident. This logic creates a global JAVA OPTions key/value pair. This becomes accessible by Gatling during execution.

Next, inside the Gatling Simulation class, we then need to access the global variable.

Adding Value to the Class

To access the JAVA_OPTS variable value the command is:

val variable_name = System.getProperty("VARIABLE_NAME")

That was easy! Now we can access the value anywhere within the class function. …

...
val httpProtocol = http
.baseUrl("http://" + target)
...

Testing It Out

With everything in place. Time to test out the load simulation execution.

JAVA_OPTS="-Dtarget=google.com" ./gatling.sh -s PassingDataIntoGatling -nr
...
================================================================================
2018-11-08 18:31:03 6s elapsed
---- Requests ------------------------------------------------------------------
Global (OK=200 KO=0 )
request_0 (OK=50 KO=0 )
request_0 Redirect 1 (OK=50 KO=0 )
request_0 Redirect 2 (OK=50 KO=0 )
googlelogo_color_272x92dp.png (OK=50 KO=0 )
---- PassingDataIntoGatling ----------------------------------------------------
[##########################################################################]100%
waiting: 0 / active: 0 / done: 50

That seemed to work alright. Let’s change the target value and make sure.

JAVA_OPTS="-Dtarget=yahoo.com" ./gatling.sh -s PassingDataIntoGatling -nr
...
================================================================================
2018-11-08 18:29:50 10s elapsed
---- Requests ------------------------------------------------------------------
Global (OK=3093 KO=0 )
request_0 (OK=50 KO=0 )
request_0 Redirect 1 (OK=50 KO=0 )
request_0 Redirect 2 (OK=50 KO=0 )
...
---- PassingDataIntoGatling ----------------------------------------------------
[##########################################################################]100%
waiting: 0 / active: 0 / done: 50

Yep, working as expected! Nice. Now we can specify variable values during execution of a Gatling simulation.

That’s a Wrap

When knee deep in development every second that can keep you in the ‘flow’ is worth the effort. With this knowledge now we can rapidly execute load tests against targets without loosing context.

Hope you enjoyed this quick article, drop a comment below if you have questions, comments, or would like to talk more about Automated processes, software engineering, or DevOps concepts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.