本文介绍了不使用CSV文件的加特林简单供料器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用csv文件的情况下在Gatling中创建简单的供稿器?我已经尝试了Gatling文档中的脚本.我在文档

How can I create a simple feeder in Gatling without using a csv file?I have tried scripts from the Gatling documentation. I have seen one example in the documentation

val random = new util.Random
val feeder = Iterator.continually(Map("email" -> random.nextString(20) + "@foo.com"))

我不理解上面的代码.

I don't understand the above code.

我尝试了使用带有csv文件的供稿器的脚本,并成功执行了该脚本.而不是从csv文件中馈送数据,我该如何编写可以采用定义值的馈送器.

I tried a script with a feeder that uses a csv file and was executed successfully. Instead of feeding data from a csv file, how do I write a feeder that can take a defined value.

推荐答案

作为 docs 状态,Feeder只是Iterator[Map[String, T]]的别名.您只需要确保Feeder可以提供RüdigerKlaehn强调的无限值流.

As the docs state, a Feeder is just an alias for Iterator[Map[String, T]]. You just need to make sure your feeder provides an infinite stream of values as highlighted by Rüdiger Klaehn.

由于您说过您已经能够使用内置的csv feeder来运行示例,所以让我们将其转换为我们自己的feeder,以便更清楚地了解上述自定义feeder代码的作用.

Since you said you already was able to run an example using the builtin csv feeder, let's convert it to our own feeder so it becomes more clear what the above custom feeder code does.

让我们看看来自高级教程的示例:

Let's look at the example that comes from the advanced tutorial:

object Search {

  val feeder = csv("search.csv").random // 1, 2

  val search = exec(http("Home")
    .get("/"))
    .pause(1)
    .feed(feeder) // 3
    .exec(http("Search")
    .get("/computers?f=${searchCriterion}") // 4
    .check(css("a:contains('${searchComputerName}')", "href").saveAs("computerURL"))) // 5
    .pause(1)
    .exec(http("Select")
    .get("${computerURL}")) // 6
    .pause(1)
}

这是生成提要的部分:

  val feeder = csv("search.csv").random // 1, 2

这是search.csv文件:

searchCriterion,searchComputerName
Macbook,MacBook Pro
eee,ASUS Eee PC 1005PE

让我们用新的自定义Feeder替换它:

Let's replace that with our new custom feeder:

/* This is our list of choices, we won't ready from csv anymore */
val availableComputers = List(
  Map("searchCriterion" -> "MacBook", "searchComputerName" -> "Macbook Pro"),
  Map("searchCriterion" -> "eee", "searchComputerName" -> "ASUS Eee PC 1005PE")
)

/* Everytime we call this method we get a random member of availableComputers */
def pickARandomComputerInfo() = {
  availableComputers(Random.nextInt(availableComputers.size))
}

/* Continually means every time you ask feeder for a new input entry, 
   it will call pickARandomComputerInfo to gerenate an input for you.
   So iterating over feeder will never end, you will always get
   something */
val feeder = Iterator.continually(pickARandomComputerInfo)

这很难在您提供的示例中看到,但是您可以将其拆分以更好地理解它:

This is harder to see in your provided example, but you could split it to better understand it:

def getRandomEmailInfo() = Map("email" -> random.nextString(20) + "@foo.com")
val feeder = Iterator.continually(getRandomEmailInfo)

这篇关于不使用CSV文件的加特林简单供料器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 20:43