Performance 如何使用Scala脚本在Gatling中传递命令行输入?
我希望用户可以在从Gatling执行时从命令行输入“Count、repeatCount、testServerUrl和definitionId”。我从命令行执行 但给出了以下错误: url null/api/工作流无法解析为URI:scheme 基本上是空值传递到那里。“definitionId”也是如此。下面是代码。您可以尝试使用任何url。您只需检查命令行提供的值是否显示Performance 如何使用Scala脚本在Gatling中传递命令行输入?,performance,scala,performance-testing,load-testing,gatling,Performance,Scala,Performance Testing,Load Testing,Gatling,我希望用户可以在从Gatling执行时从命令行输入“Count、repeatCount、testServerUrl和definitionId”。我从命令行执行 但给出了以下错误: url null/api/工作流无法解析为URI:scheme 基本上是空值传递到那里。“definitionId”也是如此。下面是代码。您可以尝试使用任何url。您只需检查命令行提供的值是否显示 import io.gatling.core.Predef._ import io.gatling.http.Predef
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)
}
这里没有定义main方法,因此我认为很难在这里传递命令行参数。但是为了解决这个问题,您可以从环境变量中读取属性 为此,你可以在这里找到一些帮助! 如果是加特林,请参见此处: 我认为这会帮你完成:
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)
}
在命令行上,首先导出JAVA_OPTS环境变量
通过直接在终端中使用此命令
export JAVA_OPTS=-Duse rCount=50-DflowRepeatCount=2-DdefinitionId=10220301-DtestServerUrl='something'这里没有定义main方法,所以我认为在这里传递命令行参数很困难。但是为了解决这个问题,您可以从环境变量中读取属性 为此,你可以在这里找到一些帮助! 如果是加特林,请参见此处: 我认为这会帮你完成:
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)
}
在命令行上,首先导出JAVA_OPTS环境变量
通过直接在终端中使用此命令
导出JAVA_OPTS=-Duse rCount=50-DflowRepeatCount=2-DdefinitionId=10220301-DtestServerUrl='something'Windows 10解决方案:
使用带有内容的_params.bat文件创建简单的my_gatling_,例如:
@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
其中:
computerdatabase.BJRSimulation_lite-您的.scala脚本
要传递给脚本的用户和游戏参数
因此,在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))
}
在此之后,您只需调用'my_gatling_with_params.bat-Dusers=2-Dgames=1'即可将您的参数传递到测试中Windows 10解决方案:
使用带有内容的_params.bat文件创建简单的my_gatling_,例如:
@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
其中:
computerdatabase.BJRSimulation_lite-您的.scala脚本
要传递给脚本的用户和游戏参数
因此,在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))
}
在此之后,您可以调用'my_gatling_with_params.bat-Dusers=2-Dgames=1'将您的参数传递到测试中任何示例如何从用户处获取该值?像这样,System.getenvCount System.getenvrepeatcount System.getenvtestServerUrl?我已经更新了答案,说明它是如何在Gatling中完成的!看一看,投上一票!:Dyea正在阅读相同的链接,但不知道如何在我的项目中实现。如果可能的话,你能帮助实现上述代码吗?TiacCompiler$-D:\Ronak\gatling-charts-highcharts-bundle-2.1.7\user file s\simulations\runrecord\TestCLI.scala:38:类型不匹配;找到:必需的整数:io.gatling.core.session.Expression[Int],它扩展为io.gatling.core.session=>io.gatling.core.validation.validation[Int]12:34:34.259[ERROR]i.g.c.zincompiler$-.repeat flowRepeatCount{12:34:34.260[ERROR]i.g.c.zincompiler$-^12:34:34.560[ERROR]i.g.c.Zincompiler$-发现一个错误12:34:34.560[调试]i.g.c.ZincCompiler$-编译失败我已经更新了答案。我希望它有帮助,如果有帮助,请接受它,这样其他人就不会面临同样的问题。任何示例如何从用户处获取该值?如下图所示,System.getenvCount System.getenvrepeatcount System.getenvtestServerUrl?请参阅我已经更新了答案,正如在Gatling中所做的那样查看并向上投票!:Dyea正在读取相同的链接,但不知道如何在我的项目中实现。如果可能,请您帮助在上述代码中实现?Tiaging编译错误Zincompiler$-D:\Ronak\gatling-charts-highcharts-bundle-2.1.7\user file s\simulations\runrecord\TestCLI.scala:38:类型不匹配;发现:需要整数:io.ga扩展为io.gatling.core.session.session=>io.gatling.core.validation.validation[Int]12:34:34.259[ERROR]i.g.c.Zincompiler$-.repeat flowRepeatCount{12:34:34.260[ERROR]i.g.c.Zincompiler$-^12:34:34.560[ERROR]i.g.c.Zincompiler$-发现一个错误12:34:34.560[DEBUG]i.g.c.Zincompiler$-编译失败我已更新了答案。我希望它有帮助,如果有帮助,请接受它,这样其他人就不会面临同样的问题