我希望用户在从Gatling执行时可以从命令行输入'Count,repeatCount,testServerUrl和definitionId'。从命令行我执行
> export JAVA_OPTS="-DuserCount=1 -DflowRepeatCount=1 -DdefinitionId=10220101 -DtestServerUrl='https://someurl.com'" > sudo bash gatling.sh
但是出现以下错误:
url null / api / workflows无法解析为URI:scheme
基本上空值传递到那里。 “definitionId”也是如此。以下是代码。你可以试试任何网址。你只需要检查命令行提供的值是否显示?
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)
}
答案 0 :(得分:3)
这里没有定义主方法,所以我认为在这里传递命令行参数很困难。但是,围绕你可以做的工作是从环境变量中读取属性。
为此你可以在这里找到一些帮助! How to read environment variables in Scala
如果是加特林,请参见此处:http://gatling.io/docs/2.2.2/cookbook/passing_parameters.html
我认为这会让你完成:
text-shadow: 4px 3px blue;
在命令行上首先导出JAVA_OPTS环境变量 直接在终端使用此命令。
export JAVA_OPTS =" -Duse rCount = 50 -DflowRepeatCount = 2 -DdefinitionId = 10220301 -DtestServerUrl =''"
答案 1 :(得分:1)
Windows 10解决方案:
使用内容创建简单的 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
其中:
因此,在您的计算机数据库.BJRSimulation_lite 文件中,您可以通过以下方式使用变量 users 和 games :
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))
}
之后,您只需调用'my_gatling_with_params.bat -Dusers = 2 -Dgames = 1'即可将您的参数传递给测试