Passing Data Into Gatling from the Command Line
Tags: devops, testing, gatling, howto
Published: 2018-11-08 23:36:02

---
title: Passing Data Into Gatling from the Command Line
published: true
description: Passing data into a Gatling load simulation is super easy, barely an inconvenience.
tags: #devops, #testing, #gatling , #howto
cover_image: https://blog.davidjeddy.com/wp-content/uploads/2018/07/Gatling-logo.png
---

<!-- wp:paragraph -->
<p>A few months back I covered <a href="https://gatling.io">Gatling </a>in a basic review. This time around we will dive a bit deeper into creating a <a href="https://gatling.io">Gatling </a>(3.0) test simulation.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Basic Setup</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Since <a href="https://blog.davidjeddy.com/2018/07/06/gatling-a-load-testing-tool-with-a-difference/">writing the previous article about Gatling</a>&nbsp;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. <a href="https://gatling.io/category/new-release/">Checkout the official change log here.</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">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)
	)
}</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Save this file as <strong>/path/to/gatling/user-files/simulations/PassingDataIntoGatling.scala</strong> . This way Gatling will find the simulation class automatically; making the rest of the process much easier.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Create the Global Variable</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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&nbsp; is as follows:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><code>JAVA_OPTS="-DVARIABLE_NAME=VARIABLE_VALUE"</code></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, inside the Gatling Simulation class, we then need to access the global variable.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Adding Value to the Class</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To access the JAVA_OPTS variable value the command is:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">val variable_name = System.getProperty("<code>VARIABLE_NAME</code>")</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>That was easy! Now we can access the value anywhere within the class function. ...</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">...<br>	val httpProtocol = http<br>		.baseUrl("http://" + target)<br>...</pre>
<!-- /wp:preformatted -->

<!-- wp:heading -->
<h2>Testing It Out</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With everything in place. Time to test out the load simulation execution.</p>
<!-- /wp:paragraph -->

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

<!-- wp:paragraph -->
<p>That seemed to work alright. Let's change the target value and make sure.</p>
<!-- /wp:paragraph -->

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

<!-- wp:paragraph -->
<p>Yep, working as expected! Nice. Now we can specify variable values during execution of a Gatling simulation.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>That's a Wrap</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->