How to pass command line input in Gatling using Scala script?

972 views
Skip to first unread message

pandya ronak

unread,
Jun 24, 2016, 1:51:17 AM6/24/16
to Gatling User Group

I want that user can input 'Count, repeatCount, testServerUrl and definitionId' from command line while executing from Gatling. Can anybody please help me? TIA.

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

    class TestCLI extends Simulation {


        val count = 50
        val wait = 2
        val repeatCount = 2

        val testServerUrl = "Some url" 


        val scn = scenario("testabcd")
            .repeat (repeatCount ) {
                exec(http("asdfg")
                .post("""/xyzapi""")
                .headers(headers_0)
                .body(StringBody("""{"definitionId":10200121}"""))) // I also want to get this value dynamic from CLI and put here
                .pause(wait) 
            }                   

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

    }

Tony Cruickshank

unread,
Jun 24, 2016, 4:08:08 AM6/24/16
to gat...@googlegroups.com
Hi Pandya,

Configuration for gatling is here: http://gatling.io/docs/2.0.0-RC2/general/configuration.html

The one you're looking for is probably JAVA_OPTS. I use this by modifying the gatling.sh script, setting
JAVA_OPTS="${JAVA_OPTS} -Dgatling_conf=${GATLING_CONF} -Dgatling_home=${GATLING_HOME}"
and then in the driver class
val gatlingHome = System.getProperty("gatling_home")

I hope that helps.

Kind regards,
Tony.

--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Intelligence is free. Wisdom comes at a cost.

pandya ronak

unread,
Jun 24, 2016, 5:39:35 AM6/24/16
to Gatling User Group
Hi Tony,

I have modified following code.

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

    class TestCLI extends Simulation {


        val count = Integer.getInteger("users", 50).toint
        val wait = 2
        val repeatCount = Integer.getInteger("repeatCount", 2).toInt

        val testServerUrl = System.getProperty("testServerUrl")
        val definitionId  = java.lang.Long.getLong("definitionId", 0L)

        val httpProtocol = http
.baseURL(testServerUrl)
.inferHtmlResources()
.acceptHeader("""*/*""")
.acceptEncodingHeader("""gzip, deflate""")
.acceptLanguageHeader("""en-US,en;q=0.8""")
.authorizationHeader(envAuthenticationHeaderFromPostman)
.connection("""keep-alive""")
.contentTypeHeader("""application/vnd+json""")
.userAgentHeader("""Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36""")

val headers_0 = Map(
"""Cache-Control""" -> """no-cache""",
"""Origin""" -> """chrome-extension://mmmmmmmmmmmm""")

val scn = scenario("testabcd")
            .repeat (repeatCount ) {
                exec(http("asdfg")
                .post("""/xyzapi""")
                .headers(headers_0)

                .body(StringBody("""{"definitionId":$definitionId}"""))) // I also want to get this value dynamic from CLI and put here
                .pause(wait) 
            }                   

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

    }

But gives this error while running,
D:\abc\gatling-charts-highcharts-bundle-2.1.7\bin>JAVA_OPTS="-DuserCount=50 -D flowRepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'"
gatling
.b at 'JAVA_OPTS' is not recognized as an internal or external command, operable program or batch file.

Tony Cruickshank

unread,
Jun 24, 2016, 7:54:58 AM6/24/16
to gat...@googlegroups.com
Hi Pandya,

I'm guessing by the truncated "gatling.b" that you're running this in Windows. I'm not entirely familiar with this (as I use a real operating system by choice ;) ), but try "set JAVA_OPTS=..."

Also, you have a space in "-D flowRepeatCount=..." that might cause problems.

At this point, I'll hand you on to any Windows people watching.

Kind regards,
Tony.

pandya ronak

unread,
Jun 24, 2016, 8:06:46 AM6/24/16
to Gatling User Group
Hii Tony,

That JAVA_OPTS error got solved by adding java before JAVA_OPTS (java JAVA_OPTS arguments...) but one more error shows which is it could not find main class (as I dont have main class in my script)
D:\Ronak\gatling-charts-highcharts-bundle-2.1.7\bin>java JAVA_OPTS="-DuserCount=
50 -DflowRepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'"
./ga
tling
.bat
Error: Could not find or load main class JAVA_OPTS=-DuserCount=50 -DflowRepeatCo
unt
=2 -DdefinitionId=10220301 -DtestServerUrl='something'

So, how can I add main class or any alternative solution without adding main class? script code I already added in previous thread.

Thanks.

Tony Cruickshank

unread,
Jun 24, 2016, 8:12:21 AM6/24/16
to gat...@googlegroups.com
Hi Pandya,

That's not solved the problem. Doing
> java "somelongstring" someotherstuff
just tells java to look for a class somelongstring, that has a main() method.

Kind regards,
Tony.

pandya ronak

unread,
Jun 25, 2016, 2:00:28 AM6/25/16
to Gatling User Group
Hii,

I want that user can input 'Count, repeatCount, testServerUrl and definitionId' from command line while executing from Gatling. From command line I execute
`
    > export JAVA_OPTS="-DuserCount=1 -DflowRepeatCount=1 -DdefinitionId=10220101 -DtestServerUrl='https://someurl.com'"
    > sudo bash gatling.sh`

but there is runtime error,
Errors --------------------------------------------------------------------
` url null/api/workflows can't be parsed into a URI: scheme `

**URL which I provide is shown null**

Here is the code snippet,


import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._        

class TestCLI extends Simulation {          
    val userCount = Integer.getInteger("userCount", 1).toInt   
    val holdEachUserToWait = 2 
    val flowRepeatCount = Integer.getInteger("flowRepeatCount", 2).toInt   
    val definitionId  = java.lang.Long.getLong("definitionId", 0L)     
    val testServerUrl = System.getProperty("testServerUrl")

    val httpProtocol = http
            .baseURL(testServerUrl)
            .inferHtmlResources()
            .acceptHeader("""*/*""")
            .acceptEncodingHeader("""gzip, deflate""")
            .acceptLanguageHeader("""en-US,en;q=0.8""")
            .authorizationHeader(envAuthenticationHeaderFromPostman)
            .connection("""keep-alive""")
            .contentTypeHeader("""application/vnd.v7811+json""")

            .userAgentHeader("""Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36""")

    val headers_0 = Map(
            """Cache-Control""" -> """no-cache""",
            """Origin""" -> """chrome-extension://faswwegilgnpjigdojojuagwoowdkwmasem""")



                    val scn = scenario("testabcd")
                        .repeat (flowRepeatCount) {
                            exec(http("asdfg")
                            .post("""/api/workflows""")

                            .headers(headers_0)
                            .body(StringBody("""{"definitionId":$definitionId}"""))) // I also want to get this value dynamic from CLI and put here
                            .pause(holdEachUserToWait)
                        }                  

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

                }

Thanks.
Reply all
Reply to author
Forward
0 new messages