1

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 gives following error:

url null/api/workflows can't be parsed into a URI: scheme

Basically null value pass there. Same happens to 'definitionId'. Following is the code. you can try with any url. you just have to check the value which you provides by commandline is shown or not?

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)

                }
Peter
  • 855
  • 2
  • 15
  • 35

2 Answers2

5

Here no main method is defined so I think it would be difficult to pass the command line argument here. But for the work around what you can do is Read the property from the Environment variables.

For that you can find some help here ! How to read environment variables in Scala

In case of gatling See here : http://gatling.io/docs/2.2.2/cookbook/passing_parameters.html

I think this will get you done :

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

    class TestCLI extends Simulation {



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

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


        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)

    }

On the command line firstly export the JAVA_OPTS environment variable by using this command directly in terminal.

export JAVA_OPTS="-Duse rCount=50 -DflowRepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'"

Community
  • 1
  • 1
Shivansh
  • 3,454
  • 23
  • 46
  • any example how to get that value from user? Like this, System.getenv("Count") System.getenv("repeatcount") System.getenv("testServerUrl") ? – Peter Jun 24 '16 at 06:08
  • 1
    See I have updated the answer as how it is done in Gatling ! Take a look and upvote ! :D – Shivansh Jun 24 '16 at 06:28
  • yea reading the same link,but dont know how to implement in my project. if possible can u please help with implementing in above code? TIA – Peter Jun 24 '16 at 06:45
  • Getting compilation error ZincCompiler$ - D:\Ronak\gatling-charts-highcharts-bundle-2.1.7\user-file s\simulations\runrecord\TestCLI.scala:38: type mismatch; found: Integer required: io.gatling.core.session.Expression[Int] (which expands to) io.gatling.core.session.Session => io.gatling.core.validation.Validation[Int ] 12:34:34.259 [ERROR] i.g.c.ZincCompiler$ - .repeat (flowRepeatCount) { 12:34:34.260 [ERROR] i.g.c.ZincCompiler$- ^ 12:34:34.560 [ERROR] i.g.c.ZincCompiler$-one error found 12:34:34.560 [DEBUG] i.g.c.ZincCompiler$-Compilation failed – Peter Jun 24 '16 at 07:07
  • Have you done this? export JAVA_OPTS="-Dcount=50 -DrepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'" //If error is generated try removing single qoutes too. – Shivansh Jun 24 '16 at 07:13
  • open the terminal export JAVA_OPTS="-Dcount=50 -DrepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'" And then run the Gatling Scripts ! – Shivansh Jun 24 '16 at 07:39
  • any idea why I am getting this error? 'JAVA_OPTS' is not recognized as an internal or external command, operable program or batch file. – Peter Jun 24 '16 at 10:30
  • it works when I add 'java' before JAVA_OPTS but throws one more error 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' – Peter Jun 24 '16 at 11:04
  • use the export command " export JAVA_OPTS="-Dcount=50 -DrepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'" I hope that you are using LINUX – Shivansh Jun 24 '16 at 12:03
  • executed on Linux gives the same error main class not found. /gatling-charts-highcharts-2.0.3/bin$ java export JAVA_OPTS="-Duse rCount=50 -DflowRepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'" ./gatling.sh Error: Could not find or load main class export – Peter Jun 24 '16 at 12:51
  • Do not use java command starts from export : export JAVA_OPTS="-Duse rCount=50 -DflowRepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'" – Shivansh Jun 24 '16 at 12:53
  • 1
    I have updated the answer.I hope it helps and if it does please accept it so that other may not face the same issue – Shivansh Jun 24 '16 at 12:56
  • Thanks for the help but no luck for me Executed the same: export JAVA_OPTS="-Duse rCount=50 -DflowRepeatCount=2 -DdefinitionId=10220301 -DtestServerUrl='something'" Gives above Error: Could not find or load main class export :-( – Peter Jun 24 '16 at 14:08
  • Okay Can you provide me a github repository so that i might help ! – Shivansh Jun 24 '16 at 17:20
  • Hi, Checked. That command works but one more error :-( Errors -------------------------------------------------------------------- > url null/api/workflows can't be parsed into a URI: scheme The URL which we provide goes null over there. I have edited the question with full details, u can refer. – Peter Jun 25 '16 at 05:30
  • Actually it is not taking the parameters which we provide thru cmd.. If I write JAVA_OPTS="-Duseraskjafsjafajnfwet=5" gatling.bat -s TestCLI instead of JAVA_OPTS="-DuserCount=5" gatling.bat -s TestCLI it will accept it and runs default value(however it taked default value only not parameters we passed) val userCount: Int = Integer.getInteger("userCount", 1).toInt It takes '1' not 5(which we pass thru cmd) – Peter Jun 25 '16 at 09:08
2

Windows 10 solution:
create simple my_gatling_with_params.bat file with content, e.g.:

@ECHO OFF
@REM You could pass to this script JAVA_OPTS in cammandline arguments, e.g. '-Dusers=2 -Dgames=1'

set JAVA_OPTS=%*

@REM Define this variable if you want to autoclose your .bat file after script is done
set "NO_PAUSE=1"

@REM To have a pause uncomment this line and comment previous one
rem set "NO_PAUSE="

gatling.bat -s computerdatabase.BJRSimulation_lite -nr -rsf c:\Work\gatling-charts-highcharts-bundle-3.3.1\_mydata\
exit

where:

  • computerdatabase.BJRSimulation_lite - your .scala script
  • users and games params that you want to pass to script

So in your computerdatabase.BJRSimulation_lite file you could use variables users and games in the following way:

package computerdatabase

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import scala.util.Random
import java.util.concurrent.atomic.AtomicBoolean


class BJRSimulation_lite extends Simulation {

val httpProtocol = ...

val nbUsers = Integer.getInteger("users", 1).toInt
val nbGames = Integer.getInteger("games", 1).toInt

val scn = scenario("MyScen1")
    .group("Play") {
//Set count of games
        repeat(nbGames) { 
        ...
        }   
    }   
// Set count of users
setUp(scn.inject(atOnceUsers(nbUsers)).protocols(httpProtocol))
}

After that you could just invoke 'my_gatling_with_params.bat -Dusers=2 -Dgames=1' to pass yours params into test