简体   繁体   中英

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

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)

                }

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'"

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

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM