Need to pass the arguments from the command line while running gatling.sh in Gatling Performance testing

3.1k Views Asked by At

I have a scenario where while executing gatling. sh, I need to pass the authorization from the terminal instead of going and changing the situation.scala file always, because authorization is specified only for particular and once a user logs out we need to provide new login Auth key. Basically can we pass the arguments from the command line while running the gatling.sh in Gatling Performance testing. Please, check the simulation.scala file for reference. ""authorization" -> "Need to pass value from terminal while running the gatling.sh","

import scala.concurrent.duration._

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

class RecordedSimulation extends Simulation {

    val httpProtocol = http
        .baseURL("baseURL")
        .inferHtmlResources()

    val headers_0 = Map(
        "accept" -> "*/*",
        "accept-encoding" -> "gzip, deflate, br",
        "accept-language" -> "en-US,en;q=0.9",
        "access-control-request-headers" -> "access-control-allow-origin,authorization,content-type",
        "access-control-request-method" -> "GET",
        "origin" -> "URL_LINK",
        "user-agent" -> "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")

    val headers_1 = Map(
        "accept" -> "*/*",
        "accept-encoding" -> "gzip, deflate, br",
        "accept-language" -> "en-US,en;q=0.9",
        "access-control-allow-origin" -> "*",
        **"authorization" -> "Need to pass value from the terminal while running the gatling.sh",**
        "content-type" -> "application/json",
        "origin" -> "ORIGIN_URL",
        "referer" -> "REFERER_URL",
        "user-agent" -> "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")
2

There are 2 best solutions below

0
On

Your scala file

class abcdClass extends Simulation { val environment = System.getProperty("environment", "Performance Env.") // here "Performance Env." is default value .... .... }

Console:

For Linux

export JAVA_OPTS="-Denvironment=LoadTest Env"

./gatling.sh

For Windows

set JAVA_OPTS="-Denvironment=LoadTest Env"

gatling.bat

2
On

According to the docs, you can pass additional JAVA_OPTS either by editing the launch script (gatling.sh) or specifying them in the command line before calling the launch script.

I'd advise to specify them via the command line as this seems clearer and is explicitly visible.


Specifying JAVA_OPTS via the command line:

Assuming your on a Unix based system (guessing from your user agent you're on OS X) you can define additional JAVA_OPTS before calling gatling.sh like described here:

JAVA_OPTS="-Dauthorization=foobar" bin/gatling.sh

Assuming the value you want to access is a String, you should be able to use the additional value like this:

"authorization" -> System.getProperty("authorization")

You can also edit the launch script itself. Therefore open gatling.sh with the text editor of your choice and add the above JAVA_OPTS there.