简体   繁体   English

如何使用 Scala 脚本在 Gatling 中传递命令行输入?

[英]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.我希望用户可以在从 Gatling 执行时从命令行输入“Count、repeatCount、testServerUrl 和 definitionId”。 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 url null/api/workflows 无法解析为 URI:scheme

Basically null value pass there.基本上空值传递到那里。 Same happens to 'definitionId'. '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.这里没有定义 main 方法,所以我认为在这里传递命令行参数会很困难。 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 如何在 Scala 中读取环境变量

In case of gatling See here : http://gatling.io/docs/2.2.2/cookbook/passing_parameters.html在加特林的情况下见: 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.在命令行中,首先在终端中直接使用此命令导出 JAVA_OPTS 环境变量。

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

Windows 10 solution: Windows 10 解决方案:
create simple my_gatling_with_params.bat file with content, eg:创建带有内容的简单my_gatling_with_params.bat文件,例如:

@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 computerdatabase.BJRSimulation_lite - 你的 .scala 脚本
  • 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:因此,在您的computerdatabase.BJRSimulation_lite文件中,您可以通过以下方式使用变量用户游戏

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之后,您可以调用'my_gatling_with_params.bat -Dusers=2 -Dgames=1'将您的参数传递给测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加特林:如何在scala脚本中传递用户定义的变量(值)? - Gatling: How do you pass userdefined variables (values) in scala script? 如何在 Scala 中为 Gatling 模拟创建随机场景 - How to create a Random Scenario in Scala for a Gatling Simulation 如何在 Gatling session 中从“exec”块传递和获取属性 - How to pass and get attributes in Gatling session from and to "exec" blocks 在Scala中使用Gatling创建动态POST / users调用 - Creating dynamic POST /users calls with Gatling in Scala VSCode 编译速度较慢 C++ 即使我使用的是相同的命令行脚本 - VSCode compiles slower C++ even though I'm using the same command line script 如何在不使用 Gradle 命令的情况下使用 shell 脚本启动 leakcanary - How to start leakcanary with shell script without using Gradle command 从 10 个用户开始,每 5 分钟添加 50 个用户加特林 scala - Start with 10 user and add every 5 min 50 user gatling scala 如何使用来自 Gatling 的 HTTP 代理记录获得 Header 响应 - How can I get Header Response using HTTP Proxy Recording from Gatling 使用加特林将条件放入HTTP请求中 - Put condition in HTTP request using gatling 如何有效地在Racket中逐行读取输入? - How to read input line by line in Racket efficiently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM