我怎样才能把一个简单的迭代循环变成加特林中的馈线?

How can I turn a simple iterative loop into a feeder in Gatling?

为了在 运行 性能脚本之前用大量数据填充系统,我们理想的用例是使用 Gatling。除了具有唯一的主 ID 外,数据无需不同。

object Object {

 val create = repeat(4, "n")
 {
 exec(http("Create Object") 
 .post("/our/api/objects")
 .body(ELFileBody("CreateObject_0001_request.txt"))
 .check(status.is(201)))
 }
}

val createObjects = scenario("ModularSimulation").exec(CreateObjects.create);

setUp(
 createObjects.inject(atOnceUsers(1))
).protocols(httpProtocol)

上面的示例可以通过更改 repeat 的值来创建任意数量的对象,但是在大规模(例如 100,000 个对象)下,线性地执行此操作变得不切实际。所以我想做的是有一个共享的对象池,由 100 个用户创建。

这当然是馈线的用例。与其生成静态 .csv file 或使用 Redis,不如使用简单的迭代循环(例如 0100000)似乎最简单。

我知道(来自 documentation and other )Feeder 是 Iterator[Map[String, T]] 的类型别名,所以我认为这应该非常简单 - 但我似乎找不到 这种最基本情况的简单 示例。

如有任何帮助,我们将不胜感激。

我不确定您想要什么,但使用馈线很容易。 假设你有:

import scala.util.Random

// An infinite feeder generating random strings 
// accessible under "yourInfiniteSessionKey" from session  
val infiniteFeeder = Iterator.continually(
  Map("yourInfinteSessionKey" -> Random.alphanumeric.take(20).mkString)
)

// A finite feeder (in sense of possible values) 
// accessible under "yourFiniteSessionKey" from session
// it contains just 2 values for illustration purposes
val finiteFeeder = (for (i <- 0 until 2) yield {
  Map("yourFiniteSessionKey" -> s"I'm finite $i")
})

// A fixed feeder (again in sense of possible values)
// accessible under "yourFixedSessionKey" from session
// again it contains just 2 values for illustration purposes
val fixedFeeder = Array(
  Map("yourFixedSessionKey" -> s"I'm fixed 1"),
  Map("yourFixedSessionKey" -> s"I'm fixed 2")
)

val scn = scenario("Feeding")
  .feed(infiniteFeeder)
  .feed(finiteFeeder)
  .feed(fixedFeeder)
  .exec(http("root")
  .get("/${yourInfinteSessionKey}/${yourFiniteSessionKey}/${yourFixedSessionKey}"))

仅举个例子,我们的场景从我们所有的馈线中获取并使用这些值来组成针对“/”的 GET 请求。

在这种情况下 fixedFeederArray[Map[String, _]finiteFeederIndexedSeq[Map[String, _]。如果您的有限喂食器中的物品数量与您的设置相匹配,那么就可以了,您可以 运行 场景,例如喜欢:

setUp(
  scn.inject(atOnceUsers(2))
).protocols(httpConf)   

安装程序只有两个虚拟用户,因此 运行 没有问题。 当您的设置中有更多虚拟用户时,例如:

setUp(
  scn.inject(constantUsersPerSec(1) during(30.seconds)) // equals 30 virtual users
).protocols(httpConf)  

你会 运行 陷入 finite/fixed 喂食器的问题,Gatling 会抱怨它,你的模拟将像这样停止:

[error] java.lang.IllegalStateException: Feeder is now empty, stopping engine
[error]     at io.gatling.core.action.SingletonFeed.feed(SingletonFeed.scala:59)
[error]     at io.gatling.core.action.SingletonFeed$$anonfun$receive.applyOrElse(SingletonFeed.scala:28)
[error]     at akka.actor.Actor$class.aroundReceive(Actor.scala:467)

好消息是,您可以使用 RecordSeqFeederBuilder 的 Gatling API 方法使 finite/fixed 喂食器无限,例如:

// Now the feeder from fixed values is infinite
val fixedFeeder = Array(
  Map("yourFixedSessionKey" -> s"I'm fixed 1"),
  Map("yourFixedSessionKey" -> s"I'm fixed 2")
).circular // go back to the top of the array once the end is reached

您也可以直接在场景定义中使用这样的 API 方法调用,并且让您的 fixed/finite 馈线保持不变,例如:

val scn = scenario("Feeding")
  .feed(infiniteFeeder)
  .feed(finiteFeeder.random) // now is finiteFeeder infinite
  .feed(fixedFeeder.circular) // fixedFeeder is infinite too
  .exec(http("root")

尽情享受

很好的答案 Teliatko,当您需要一个基于有限馈线的无限馈线时,您可以添加 toArray.random 或这样的循环:

val infiniteFeeder = (for (i <- 0 until 2) yield {
  Map("yourFiniteSessionKey" -> s"I'm finite $i")
}).toArray.random