Gatling scala 脚本随机化场景
Gatling scala script randomize scenario
我使用下面的脚本,它应该发送一个 post 请求,其中包含一些从文件中读取的数据。我想随机化请求,所以有时它会发送 "R1_BINFile_0",有时会发送 "R2_Binfile_1"。由于某种原因,它不起作用,它总是发送相同的请求
package computerdatabase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class MYSimulation extends Simulation {
val feeder = csv("CsvConfigFile.csv").circular
val PATH = sys.env("GATLING_HOME")
val httpConf = http
.baseURL("https://HOST") // Here is the root for all relative URLs
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.acceptCharsetHeader("UTF-8")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
val headers_10 = Map("Content-Type" -> "application/octet-stream")
val rnd = new scala.util.Random
val scn = scenario("Scenario1")
.feed(feeder)
.doIfOrElse(rnd.nextInt(100) > 50) {
exec(http("R1")
.post("https://HOST/web/")
.headers(headers_10)
.body(RawFileBody(PATH + "\user-files\data\" + "${R1_BinFile}" + "_0")))
} {
.exec(http("R1")
.post("https://HOST/web/")
.headers(headers_10)
.body(RawFileBody(PATH + "\user-files\data\" + "${R1_BinFile}" + "_1")))
}
setUp(scn.inject(constantUsersPerSec(5) during (100 seconds)).protocols(httpConf))
}
问题是 doIfOrElse
的参数只执行一次,所以变成常量,每次都选择相同的分支。尝试传递一个函数,它将为每个请求执行:
.doIfOrElse(_ => rnd.nextInt(100) > 50)
您也可以使用 randomSwitch
而不是 doIfOrElse
,这为您的目标提供了更清晰的语法。
我使用下面的脚本,它应该发送一个 post 请求,其中包含一些从文件中读取的数据。我想随机化请求,所以有时它会发送 "R1_BINFile_0",有时会发送 "R2_Binfile_1"。由于某种原因,它不起作用,它总是发送相同的请求
package computerdatabase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class MYSimulation extends Simulation {
val feeder = csv("CsvConfigFile.csv").circular
val PATH = sys.env("GATLING_HOME")
val httpConf = http
.baseURL("https://HOST") // Here is the root for all relative URLs
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.acceptCharsetHeader("UTF-8")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
val headers_10 = Map("Content-Type" -> "application/octet-stream")
val rnd = new scala.util.Random
val scn = scenario("Scenario1")
.feed(feeder)
.doIfOrElse(rnd.nextInt(100) > 50) {
exec(http("R1")
.post("https://HOST/web/")
.headers(headers_10)
.body(RawFileBody(PATH + "\user-files\data\" + "${R1_BinFile}" + "_0")))
} {
.exec(http("R1")
.post("https://HOST/web/")
.headers(headers_10)
.body(RawFileBody(PATH + "\user-files\data\" + "${R1_BinFile}" + "_1")))
}
setUp(scn.inject(constantUsersPerSec(5) during (100 seconds)).protocols(httpConf))
}
问题是 doIfOrElse
的参数只执行一次,所以变成常量,每次都选择相同的分支。尝试传递一个函数,它将为每个请求执行:
.doIfOrElse(_ => rnd.nextInt(100) > 50)
您也可以使用 randomSwitch
而不是 doIfOrElse
,这为您的目标提供了更清晰的语法。