Grouping Feeder records

Group different records from a Gatling Feeder.

Use Case

Assuming you have a feeder file that contains data where records must be grouped by virtual users, such as:

username,url
user1,url1
user1,url2
user2,url3
user2,url4

You want to make sure user1 will pick url1 and url2 while user2 will pick url3 and url4.

Suggested Solution

The idea here is to use readRecords to load all the csv file records in memory so you can group them the way you want.

       
List<Map<String, Object>> records = csv("file.csv").readRecords();

Map<String, List<Map<String, Object>>> recordsGroupedByUsername =
  records
    .stream()
    .collect(java.util.stream.Collectors.groupingBy(record -> (String) record.get("username")));

Iterator<Map<String, Object>> groupedRecordsFeeder =
  recordsGroupedByUsername
    .values()
    .stream()
    .map(groupedRecords -> Map.of("userRecords", (Object) groupedRecords))
    .iterator();

ChainBuilder chain =
  feed(groupedRecordsFeeder)
    .foreach("#{userRecords}", "record").on(
      exec(http("request").get("#{record.url}"))
    );
Not supported by Gatling JS.
val records: List<Map<String, Any>> = csv("file.csv").readRecords()

val recordsGroupedByUsername =
  records
    .stream()
    .collect(Collectors.groupingBy { record: Map<String, Any> -> record["username"] as String })

val groupedRecordsFeeder =
  recordsGroupedByUsername
    .values
    .stream()
    .map { Collections.singletonMap("userRecords", it as Any) }
    .iterator()

val chain =
  feed(groupedRecordsFeeder)
    .foreach("#{userRecords}", "record").on(
      exec(http("request")["#{record.url}"])
    )
val records: Seq[Map[String, Any]] =
  csv("file.csv").readRecords

val recordsGroupedByUsername =
  records.groupBy(record => record("username").toString)

val groupedRecordsFeeder =
  recordsGroupedByUsername
    .values
    .iterator
    .map(groupedRecords => Map("userRecords" -> groupedRecords))

val chain =
  feed(groupedRecordsFeeder)
    .foreach("#{userRecords}", "record") {
      exec(http("request").get("#{record.url}"))
    }

Edit this page on GitHub