Simple feeder in Gatling without using a csv file
Asked Answered
A

1

4

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.

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.

Alveolus answered 11/2, 2016 at 19:55 Comment(1)
Post a more complete example so people can help you. A feeder is just a thing that produces an infinte stream of values.Chink
S
15

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.

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)
}

This is the part that generates the feed:

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

And this is the search.csv file:

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

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)
Specie answered 12/2, 2016 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.