如何在Gatling中将简单的迭代循环转换为馈线?

Eil*_*idh 2 scala 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)
Run Code Online (Sandbox Code Playgroud)

上面的例子可以通过改变它的值来创建任意数量的对象repeat,但是在大规模(例如100,000个对象)下,线性地这样做是不切实际的.所以我想做的是拥有一个由100个用户创建的共享对象池.

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

我知道(从文档和其他问题),Feeder是一个类型别名,Iterator[Map[String, T]]所以我认为这应该是非常简单的 - 但我似乎找不到这个最基本的案例的简单例子.

任何帮助将非常感激.

Tel*_*tko 8

我不确定你想要达到的目标,但使用送料器很容易.假设你有:

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}"))
Run Code Online (Sandbox Code Playgroud)

仅仅为了示例,我们的场景来自所有馈线,并使用值来对"/"组成GET请求.

在这种情况下fixedFeederArray[Map[String, _]finiteFeederIndexedSeq[Map[String, _].如果有限馈线中的项目数与您的设置相匹配,那么就可以运行该场景,例如:

setUp(
  scn.inject(atOnceUsers(2))
).protocols(httpConf)   
Run Code Online (Sandbox Code Playgroud)

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

setUp(
  scn.inject(constantUsersPerSec(1) during(30.seconds)) // equals 30 virtual users
).protocols(httpConf)  
Run Code Online (Sandbox Code Playgroud)

你会遇到有限/固定馈线的问题,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$1.applyOrElse(SingletonFeed.scala:28)
[error]     at akka.actor.Actor$class.aroundReceive(Actor.scala:467)
Run Code Online (Sandbox Code Playgroud)

好消息是,您可以使用RecordSeqFeederBuilder的Gatling API方法使有限/固定馈线无限,例如:

// 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
Run Code Online (Sandbox Code Playgroud)

您也可以直接在场景定义中使用此类API方法调用,并保持固定/有限馈线不受影响,例如:

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

请享用